0

Suppose, I have a vector with following values in it

id = 100
latitude = 2 .33
longitude = 4.55

id = 200
latitude = 1.00
longitude = 3.00

id = 300
latitude = 2.33
longitude = 4.55 

and I have following variables

int item = 10;
double x = 2.33;
double y = 4.55 

1.) Now, I want to compare the values of 2 variables (x,y) with that of (latitude,Longitude) in the vector.

2.) If (x,y) value is equal to (latitude,Longitude) then I add value of item variable to the vector

My logic to achieve it is as follows

Vector al;
int size = al.size() //need to adjust the size as and when i add/remove items from al

for(int i=0;i<sz-1;i++)
{
  if(al.get(i).getClass().equals(Double.TYPE)) //Need to ignore id in Vector

   if(a.get(i)==x & a.get(i+1)==y)
    {
      //add the sensor id at i-2 and adjust the size of vector
    }
   else {
          //remove the items 200,1.00,3.00
          // adjust the size of vector
        }

But the above code is not giving me expected result.Can anybody provide me with working code to get the final output as follows

10,100,2.33,4.55
10,300,2.33,4.55

Note that contents 200,1.00,3.00 are not present in the expected o/p, 'coz value of (x,y) does not match with (latitude, longitude)

Thanks in advance.

user745475
  • 95
  • 1
  • 3
  • 11

5 Answers5

4

Don't use == with Double objects. You are comparing them to be the same instance, not to have the same value.

With doubles, it is best to compare the difference to be below a certain threshold, as there could be different rounding artifacts.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
3

I think the use of bitwise AND is a mistake:

if(a.get(i)==x & a.get(i+1)==y)

use boolean AND:

if(a.get(i)==x && a.get(i+1)==y)

See Anony-Mousse answer relating to use of == with objects.

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • Note that a single `&` [isn't necessarily a "bitwise" operator](http://stackoverflow.com/questions/5564410/difference-in-and). It can apply to booleans too, so wouldn't cause a problem here (though there is no reason to use it). – Shaun the Sheep Mar 09 '12 at 21:22
0

Since you know the vector is storing ids at position 0, 3, ..., (0 mod 3), you should start your loop at i = 1 and increment by 3 each time:

for(int i=1;i<sz;i+=3)

Then you don't have to check that the item is a double

arc
  • 584
  • 2
  • 5
0

When working with collection I would say: use Apache Collections - They have a very useful CollectionUtils utility object:

http://commons.apache.org/collections/apidocs/org/apache/commons/collections/CollectionUtils.html

рüффп
  • 5,172
  • 34
  • 67
  • 113
0

This is the way to solve the above problem. I am including code snippet

public void manip() {

    System.out.println("ArratList (Before) :" + Array);
    int sz = Array.size();
    for (int i = 0; i < sz - 1; i++) {
        Object o = Array.get(i);
        if (o instanceof Double) {
            if (Array.get(i).equals(x) & Array.get(i + 1).equals(y)) {
                if (i == 1) {

                    Array.add(0, item);
                } else {

                    Array.add(i - 1, item);
                }
                i = i + 3;
            } else {
                Array.remove(i - 1);
                Array.remove(i - 1);
                Array.remove(i - 1);
                i = i - 1;
            }
            sz = Array.size();
        }
    }

    System.out.println("ArratList (After) :" + Array);
 }
user745475
  • 95
  • 1
  • 3
  • 11