# Vector problem

• 01-27-2008, 09:32 PM
Ace_Of_John
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);             }         }```
• 01-27-2008, 09:53 PM
tim
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 = [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()); }```
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]```