# compare newly added Vector Element with previous elements

• 09-09-2008, 04:42 PM
nidhirastogi
compare newly added Vector Element with previous elements
Hi all
I am adding Point2D elements to a vector "Numbers" in such a way that every newly randomly created element that is at a distance of 0.3 from all the previous points is added else not.
I have written a code that only checks from the last element but not all the previous elements.
Can someone check what wrong am I doing.
The code is runnable.
Code:

```import java.awt.geom.Point2D; import java.util.*;   public class Distance {         Vector<Point2D> numbers = new Vector<Point2D>();         Random rnd = new Random();         void getList()         {         double xCoord,yCoord;                 for(int i=0;i<5;i++)// is is the no. of coordinates stored later only "b" to be used         {         xCoord = rnd.nextDouble();                // X-coord of AP         yCoord = rnd.nextDouble();                // Y coord of AP                 if(i<2)         {                 numbers.addElement(new Point2D.Double(xCoord,yCoord));                 System.out.println("0th n 1st element added : "+i+ "  "+numbers.get(i));         }                 else         {         for(int j=i-1;j>i-2;j--)         {                 if((numbers.get(j).distance(xCoord,yCoord))>0.3)                 {                                numbers.addElement(new Point2D.Double(xCoord,yCoord));                         System.out.println("next element added : "+i+ "  "+numbers.get(j)+"  with distance: "                                         +(numbers.get(j).distance(xCoord,yCoord))+"from "+ xCoord + " "+ yCoord);                 }                         }         }         }           Iterator it = numbers.iterator ();           while (it.hasNext ())           {           System.out.println(it.next());           } }                 public static void main(String[] argv)         {                 Distance d = new Distance();                 d.getList();         } }```
Thanks a lot.
• 09-09-2008, 05:22 PM
Norm
Does the program give correct results? If not, what is wrong? Can you post the program's output and describe what is wrong with it?
Quote:

only checks from the last element but not all the previous elements.
Why only the last element?

Quote:

for(int j=i-1;j>i-2;j--)
What are you trying to do in the above loop?
Can you explain the logic of your loops and if statements?

Another design idea:
I'd embed the Point2D object within your own class with a method: boolean isCloseTo(Point2D) that returns true if the new point is close to the other.
Then when adding a point, you can scan thru all the elements in the Vector testing if the new object isCloseTo any ones in the Vector.
• 09-09-2008, 06:33 PM
nidhirastogi
Quote:

Originally Posted by Norm
Does the program give correct results? If not, what is wrong? Can you post the program's output and describe what is wrong with it? Why only the last element?

--> The program compares the newly created element only with the last element and not all the last elements.So the output is wrong

Quote:

Originally Posted by Norm
for(int j=i-1;j>i-2;j--)
What are you trying to do in the above loop?
Can you explain the logic of your loops and if statements?

--> I am running the loop from the last element created to the first so that each element is compared with the current element.This will help in checking that the new element is at a minimum distance from all of the Point2D elements.

Quote:

Originally Posted by Norm
Another design idea:
I'd embed the Point2D object within your own class with a method: boolean isCloseTo(Point2D) that returns true if the new point is close to the other.
Then when adding a point, you can scan thru all the elements in the Vector testing if the new object isCloseTo any ones in the Vector.

--> I am very much going to try this now.

Thankyou very much Norm.
• 09-09-2008, 06:41 PM
Supamagier
my outputs:
Code:

```0th n 1st element added : 0  Point2D.Double[0.6094098019020386, 0.9838941381683657] 0th n 1st element added : 1  Point2D.Double[0.9106562806196608, 0.7847578243124705] Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 2  at java.util.Vector.get(Vector.java:694)  at Distance.getList(Distance.java:27)  at Distance.main(Distance.java:48)```
Code:

```0th n 1st element added : 0  Point2D.Double[0.896698639364791, 0.9499883975395484] 0th n 1st element added : 1  Point2D.Double[0.2738432301781768, 0.20839170127748607] next element added : 2  Point2D.Double[0.2738432301781768, 0.20839170127748607]  with distance: 0.35512275998569814 from 0.365903118208346 0.5513744292328927 next element added : 3  Point2D.Double[0.365903118208346, 0.5513744292328927]  with distance: 0.6504405536372115 from 0.012782587243660215 0.005133818854004213 next element added : 4  Point2D.Double[0.012782587243660215, 0.005133818854004213]  with distance: 0.9490164105252665 from 0.6874161106112764 0.6725929985757736 Point2D.Double[0.896698639364791, 0.9499883975395484] Point2D.Double[0.2738432301781768, 0.20839170127748607] Point2D.Double[0.365903118208346, 0.5513744292328927] Point2D.Double[0.012782587243660215, 0.005133818854004213] Point2D.Double[0.6874161106112764, 0.6725929985757736]```
As you see, it throws the error if one of the points has a distance greater than 0.3.
Code:

```if((numbers.get(j).distance(xCoord,yCoord))>0.3)         {             numbers.addElement(new Point2D.Double(xCoord,yCoord));             System.out.println("next element added : "+i+ "  "+numbers.get(j)+"  with distance: "                     +(numbers.get(j).distance(xCoord,yCoord))+" from "+ xCoord + " "+ yCoord);         }```
I reckon you fix this, because, obviously there's the error.

Sorry, can't be asked to fix it for you, you gotta do it yourself.
• 09-09-2008, 06:48 PM
nidhirastogi
Quote:

Originally Posted by Supamagier
my outputs:
Code:

```0th n 1st element added : 0  Point2D.Double[0.896698639364791, 0.9499883975395484] 0th n 1st element added : 1  Point2D.Double[0.2738432301781768, 0.20839170127748607] next element added : 2  Point2D.Double[0.2738432301781768, 0.20839170127748607]  with distance: 0.35512275998569814 from 0.365903118208346 0.5513744292328927 next element added : 3  Point2D.Double[0.365903118208346, 0.5513744292328927]  with distance: 0.6504405536372115 from 0.012782587243660215 0.005133818854004213```

That's exactly my output is.
However, what is required ,ALL previous elements are compared with the newly added element and not just the last element.
• 09-09-2008, 06:55 PM
Norm
If you are not comparing against all the elements in the Vector, then the indexes for the for loop must not be correct.
Quote:

running the loop from the last element created to the first
Why go backwards? Why not start at the beginning and go to the end?
• 09-09-2008, 06:56 PM
Supamagier
Because he wants to compare the new element with the last element, I think. Because he just adds it, he needs to go backwards. Though, I don't really see the use/problem(s) of that...
• 09-09-2008, 07:04 PM
nidhirastogi
Quote:

Originally Posted by Supamagier
Because he wants to compare the new element with the last element, I think. Because he just adds it, he needs to go backwards. Though, I don't really see the use/problem(s) of that...

ALL the previous and not just the last one.

Although, it shouldnt' matter wherether we go last to first or first to last when comparing.
• 09-09-2008, 07:16 PM
Supamagier
Than why use
Code:

`for(int j=i-1;j>i-2;j--)`
?

j = i-1;
while j > i-2 you compare, than you decrease j by one. This obviously always happens only one time...

use
Code:

`for(int j=i-1;j>0;j--)`
should work, I think.
• 09-09-2008, 07:32 PM
Norm
To debug how your loop is working add a println() to show the values of i and j.
• 09-10-2008, 01:32 AM
nidhirastogi
Quote:

Originally Posted by Norm
To debug how your loop is working add a println() to show the values of i and j.

yeah did that.
Thanks for the debugging tip.
got the code running in the way it should.

Code:

```for(int i=0;i<5;i++)// is is the no. of coordinates stored later only "b" to be used { xCoord = rnd.nextDouble();                // X-coord of AP yCoord = rnd.nextDouble();                // Y coord of AP if(i==0)         {                 numbers.addElement(new Point2D.Double(xCoord,yCoord));                 System.out.println("Element added : "+i+ "  "+numbers.get(i));                 System.out.println("i: "+i);         }         else         {         for(int j=0;j<i;j++)//(int j=i-1;j>i-2;j--)         {         if((numbers.get(j).distance(xCoord,yCoord))>0.5)         {                        numbers.addElement(new Point2D.Double(xCoord,yCoord));                 System.out.println("next element added : "+xCoord + " "+ yCoord+ "  "+"  with distance: "                                 +(numbers.get(j).distance(xCoord,yCoord))+"from "+ numbers.get(j));                         System.out.println("i: "+i+", j: "+j);         }                 }         } }```
Many many thanks Norm and Supamagier.