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?

8
  • 7
    1.2 is not an integer. Commented Jun 6, 2013 at 1:36
  • What's wrong with a for-loop? Commented Jun 6, 2013 at 1: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? Commented Jun 6, 2013 at 1: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 ~ Commented Jun 6, 2013 at 1:40
  • 1
    This may seem like grammar nit picking, but x is a List<double[]> 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? Commented Jun 6, 2013 at 1:43

4 Answers 4

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));
Sign up to request clarification or add additional context in comments.

Comments

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?

1 Comment

How is this different? You will still incur the same inaccuracies
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.

Comments

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);
    }
}

4 Comments

If you compare a double with another double using == you will have a bad time...
If I don't know 1.2 element in this List. how to find 1.2 element? thanks.
@LuiggiMendoza , can you please explain
Ok, I am lost with the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.