List<double[]> x = new ArrayList<double[]>();
x.add(new double[] { 1, 1.2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
如何使用java代码在x List中查找不是整数类型(1.2)?如何找到列表数组中的int元素
List<double[]> x = new ArrayList<double[]>();
x.add(new double[] { 1, 1.2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
如何使用java代码在x List中查找不是整数类型(1.2)?如何找到列表数组中的int元素
试试这个:
for (double d : x.get(0)) {
System.out.println("Not Integer:" + ((int) (d * 10)/10 != d));
}
更新(这应该是足够的和):
System.out.println("Not Integer:" + ((int) d != d));
非常感谢你 – dickfala
EDIt:我刚刚明白了这个问题。因此,这里是更新的答案
List<double[]> x = new ArrayList<double[]>();
x.add(new double[] { 1, 1.2, 2.9, 3.9, 4.1, 5.5, 6, 7, 8, 9, 10, 11, 12 });
List<Double> foundDoubles = new ArrayList<Double>();
int i = 0;
for(double d : x.get(0)) {
i = (int)d;
if(i != d) {
foundDoubles.add(d);
}
}
for(double d : foundDoubles) {
System.out.println(d);
}
}
你可以尝试以下方法:
for(double i : x) {
String total2 = String.valueOf(i);
if(i.contains(".")){
// ...
}
}
,或者您可以使用if(Math.floor(i) > 0)
改变为String
aproarch。
非常感谢你〜 – dickfala
'1.2'不是整数。 – Blorgbeard
“for-loop”有什么问题? – MadProgrammer
您是否希望找到1.2利用它已排序的事实,或者您没有保证排序? – Patashu