0
    List<double[]> x = new ArrayList<double[]>();
    x.add(new double[] { 1, 1.2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12  }); 

How can I use java code find not integer type(1.2) in x List?

dickfala
  • 3,246
  • 3
  • 31
  • 52
  • 7
    `1.2` is not an integer. – Blorgbeard Jun 06 '13 at 01:36
  • What's wrong with a `for-loop`? – MadProgrammer Jun 06 '13 at 01:37
  • Are you hoping to find 1.2 taking advantage of the fact that it's sorted, or do you not have a guarantee that it's sorted? – Patashu Jun 06 '13 at 01:40
  • I know use for loop. But I don't kwnow what's wrong below: for( int i=0; i< x.get(0).length; i++) { Log.e("log", (x.get(0)[i] instanceOf int) ); } thank you ~ – dickfala Jun 06 '13 at 01:40
  • 1
    This may seem like grammar nit picking, but `x` is a `List` so you won't be able to find anything in it but `double[]` objects. Do you actually mean find a value (likely `double` not `int`) in one of the `double[]` objects in `x`? – Craig Jun 06 '13 at 01:43
  • sorry. My express is not good. I meaning how to use find 1.2 element in x list. If I get some double[] array. how to find 1.2 element have a decimal point element. thank you response my question. – dickfala Jun 06 '13 at 01:49
  • @dickfala i think i understood what u say, i answer u – nachokk Jun 06 '13 at 02:18
  • oh man. someone who understood the question can also update the question so we can also understand it. – ata Jun 06 '13 at 02:28

4 Answers4

1

Try this:

for (double d : x.get(0)) {
System.out.println("Not Integer:" + ((int) (d * 10) / 10 != d));
}

Update (this should be enough as well):

System.out.println("Not Integer:" + ((int) d != d));
Alex
  • 11,451
  • 6
  • 37
  • 52
0

EDIt: I just understood the question. So here is the updated answer

    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);
    }
}
ata
  • 8,853
  • 8
  • 42
  • 68
0

You can try the following:

for(double i : x) {
   String total2 = String.valueOf(i);
   if(i.contains(".")){
     // ...
   }
}

or you can use if(Math.floor(i) > 0) alter for String aproarch.

Rong Nguyen
  • 4,143
  • 5
  • 27
  • 53
0

I'm new to Java :P But I think direct comparison wouldn't be good?

double a = 1;
double b = 1;
double c = a-b;
if (Math.abs(c) == 0) {...}

Basically set another primitive to the value your looking for. Subtract that value from the list. If one of the index's contains 1.2 than 1.2-1.2 = 0? Than you know where it is?

Var_Matt1
  • 19
  • 5