Results 1 to 11 of 11
 09092008, 04:42 PM #1Member
 Join Date
 Jul 2008
 Posts
 16
 Rep Power
 0
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.
Java 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(); // Xcoord 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=i1;j>i2;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(); } }
 09092008, 05:22 PM #2
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?
only checks from the last element but not all the previous elements.
for(int j=i1;j>i2;j)
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.Last edited by Norm; 09092008 at 05:27 PM.
 09092008, 06:33 PM #3Member
 Join Date
 Jul 2008
 Posts
 16
 Rep Power
 0
> The program compares the newly created element only with the last element and not all the last elements.So the output is wrong
> 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.
> I am very much going to try this now.
Thankyou very much Norm.
 09092008, 06:41 PM #4Senior Member
 Join Date
 Aug 2008
 Posts
 384
 Rep Power
 7
my outputs:
Java 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)
Java 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]
Java 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); }
Sorry, can't be asked to fix it for you, you gotta do it yourself.Last edited by Supamagier; 09092008 at 06:52 PM.
I die a little on the inside...
Every time I get shot.
 09092008, 06:48 PM #5Member
 Join Date
 Jul 2008
 Posts
 16
 Rep Power
 0
 09092008, 06:55 PM #6
If you are not comparing against all the elements in the Vector, then the indexes for the for loop must not be correct.
running the loop from the last element created to the first
 09092008, 06:56 PM #7Senior Member
 Join Date
 Aug 2008
 Posts
 384
 Rep Power
 7
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...
I die a little on the inside...
Every time I get shot.
 09092008, 07:04 PM #8Member
 Join Date
 Jul 2008
 Posts
 16
 Rep Power
 0
 09092008, 07:16 PM #9Senior Member
 Join Date
 Aug 2008
 Posts
 384
 Rep Power
 7
Than why use
Java Code:for(int j=i1;j>i2;j)
j = i1;
while j > i2 you compare, than you decrease j by one. This obviously always happens only one time...
use
Java Code:for(int j=i1;j>0;j)
I die a little on the inside...
Every time I get shot.
 09092008, 07:32 PM #10
To debug how your loop is working add a println() to show the values of i and j.
 09102008, 01:32 AM #11Member
 Join Date
 Jul 2008
 Posts
 16
 Rep Power
 0
yeah did that.
Thanks for the debugging tip.
got the code running in the way it should.
Java Code:for(int i=0;i<5;i++)// is is the no. of coordinates stored later only "b" to be used { xCoord = rnd.nextDouble(); // Xcoord 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=i1;j>i2;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); } } } }
Similar Threads

how to compare the elements of the two arraylists al1,al2
By raj reddy in forum Web FrameworksReplies: 33Last Post: 11252009, 05:48 PM 
how to compare the elements of these two arraylists
By raj reddy in forum Web FrameworksReplies: 1Last Post: 03252009, 10:55 PM 
Finding elements in a vector
By Java Tip in forum java.langReplies: 0Last Post: 04142008, 08:37 PM 
Adding a double element to a vector
By peachyco in forum New To JavaReplies: 5Last Post: 11252007, 06:07 PM 
how to compare 2 vector lists?
By oregon in forum New To JavaReplies: 2Last Post: 07252007, 08:25 PM
Bookmarks