Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-09-2008, 06:42 PM
Member
 
Join Date: Jul 2008
Posts: 14
nidhirastogi is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-09-2008, 07:22 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
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.

Last edited by Norm : 09-09-2008 at 07:27 PM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-09-2008, 08:33 PM
Member
 
Join Date: Jul 2008
Posts: 14
nidhirastogi is on a distinguished road
Quote:
Originally Posted by Norm View Post
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 View Post
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 View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-09-2008, 08:41 PM
Senior Member
 
Join Date: Aug 2008
Posts: 186
Supamagier is on a distinguished road
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.
__________________
check out
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
, 100% made by me.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by Supamagier : 09-09-2008 at 08:52 PM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 09-09-2008, 08:48 PM
Member
 
Join Date: Jul 2008
Posts: 14
nidhirastogi is on a distinguished road
Quote:
Originally Posted by Supamagier View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 09-09-2008, 08:55 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 09-09-2008, 08:56 PM
Senior Member
 
Join Date: Aug 2008
Posts: 186
Supamagier is on a distinguished road
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...
__________________
check out
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
, 100% made by me.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 09-09-2008, 09:04 PM
Member
 
Join Date: Jul 2008
Posts: 14
nidhirastogi is on a distinguished road
Quote:
Originally Posted by Supamagier View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 09-09-2008, 09:16 PM
Senior Member
 
Join Date: Aug 2008
Posts: 186
Supamagier is on a distinguished road
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.
__________________
check out
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
, 100% made by me.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 09-09-2008, 09:32 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
To debug how your loop is working add a println() to show the values of i and j.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 09-10-2008, 03:32 AM
Member
 
Join Date: Jul 2008
Posts: 14
nidhirastogi is on a distinguished road
Quote:
Originally Posted by Norm View Post
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to compare the elements of the two arraylists al1,al2 raj reddy Web Frameworks 27 04-28-2008 06:13 AM
how to compare the elements of these two arraylists raj reddy Web Frameworks 0 04-17-2008 05:12 PM
Finding elements in a vector Java Tip java.lang 0 04-14-2008 10:37 PM
Adding a double element to a vector peachyco New To Java 5 11-25-2007 08:07 PM
how to compare 2 vector lists? oregon New To Java 2 07-25-2007 10:25 PM


All times are GMT +3. The time now is 09:01 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org