Results 1 to 2 of 2
Thread: Vector problem
- 01-27-2008, 08:32 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 4
- Rep Power
- 0
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
Java 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); } }
- 01-27-2008, 08:53 PM #2
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:
This will give the output:Java Code:// Create vector Vector<Point> points = [COLOR="Blue"]new [/COLOR]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()); }
Java 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]
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
Similar Threads
-
Vector create
By Warren in forum New To JavaReplies: 4Last Post: 03-02-2010, 02:42 AM -
Vector help
By king_arthur in forum New To JavaReplies: 3Last Post: 01-22-2008, 07:33 PM -
vector problem
By mambo_jumbo in forum New To JavaReplies: 1Last Post: 11-17-2007, 10:44 PM -
array vs Vector
By paty in forum New To JavaReplies: 1Last Post: 08-02-2007, 07:07 PM -
Problem with vector, java.lang.ClassCastException
By paul in forum New To JavaReplies: 1Last Post: 07-16-2007, 04:31 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks