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 01-27-2008, 10:32 PM
Member
 
Join Date: Dec 2007
Posts: 4
Ace_Of_John is on a distinguished road
Vector problem
hi all. in this section of code i add elements to the vector(within the inside for loop).by the time the outside loop stars again the information is lost and i have a blank vector and i cant return any of the generated information. why is this and how can i fix it?? if anyone can help i would be very happy. thankyou

Code:
Vector v = new Vector(); for (double i = 0; i <= 360; i += segmentsH) { for (double n = 0; n <= 180; n += segmentsV) { p.x = Math.sin(Math.toRadians(n)) * rad; p.y = Math.cos(Math.toRadians(n)) * rad; p3.x = p.x; p3.y = p.y; p.set(p3.x, 0); p = GMath.rotatePoint(p, i); p3.x = p.x; p3.z = p.y; v.add(p3); //anis.plot(p3.x - 12.5, p3.y-12.5); //anis.plot(p3.x + 12.5, p3.z-12.5); //anis.plot(p3); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-27-2008, 10:53 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
New keyword
Hello Ace_Of_John

I don't see you using the "new" keyword. If you have some object, say A, and you modify it to be your "new" object and then add it to a vector, you actually keep adding the same object by reference. So, in affect, you have a vector of identical objects, A. To fix this you must create an instance of each Object. I'll assume that you are working with java.awt.Point objects. Fill your vector with Point instances, similar to this:
Code:
// Create vector Vector<Point> points = new Vector<Point>(); for (int i = 0; i < 10; i++){ Point point = new Point(i, i * i); points.add(point); } // Display vector for (Point point : points){ System.out.println(point.toString()); }
This will give the output:
Code:
java.awt.Point[x=0,y=0] java.awt.Point[x=1,y=1] java.awt.Point[x=2,y=4] java.awt.Point[x=3,y=9] java.awt.Point[x=4,y=16] java.awt.Point[x=5,y=25] java.awt.Point[x=6,y=36] java.awt.Point[x=7,y=49] java.awt.Point[x=8,y=64] java.awt.Point[x=9,y=81]
__________________
If your ship has not come in yet then build a lighthouse.
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
Vector help king_arthur New To Java 3 01-22-2008 09:33 PM
Vector create Warren New To Java 3 11-19-2007 10:13 PM
vector problem mambo_jumbo New To Java 1 11-18-2007 12:44 AM
array vs Vector paty New To Java 1 08-02-2007 09:07 PM
Problem with vector, java.lang.ClassCastException paul New To Java 1 07-16-2007 06:31 PM


All times are GMT +3. The time now is 02:30 PM.


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