Results 1 to 2 of 2

Thread: Vector problem

  1. #1
    Ace_Of_John is offline Member
    Join Date
    Dec 2007
    Posts
    4
    Rep Power
    0

    Default 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);
    
    	    }
    	}

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Thumbs up 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:
    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());
    }
    This will give the output:
    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

  1. Vector create
    By Warren in forum New To Java
    Replies: 4
    Last Post: 03-02-2010, 03:42 AM
  2. Vector help
    By king_arthur in forum New To Java
    Replies: 3
    Last Post: 01-22-2008, 08:33 PM
  3. vector problem
    By mambo_jumbo in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 11:44 PM
  4. array vs Vector
    By paty in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 08:07 PM
  5. Replies: 1
    Last Post: 07-16-2007, 05:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •