Results 1 to 8 of 8
  1. #1
    anthony.rich is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Vector of Object not returning data

    Hi,

    I have a vector of object that doesn't return the data - can anyone spot my school boy error?

    Java Code:
    import java.util.Vector;
    
    public class Test {
    
    	static public class Thing {
    		public StringBuilder name;
    	}
    	
    	static StringBuilder testName = new StringBuilder();
    	static Thing myThing = new Thing();
    	static Thing myOtherThing = new Thing();
    	static Vector<Thing> vThing = new Vector<Thing>();
    
    	static int j = 0;
    
    	public static void main(String[] args) {
    		testName.append("Hello World");
    		myThing.name = testName;
    		
    		vThing.addElement(myThing);
    		System.out.println("In:"+myThing.name.toString());
    
        	j = 0;
        	while (j < vThing.size()){
            	myOtherThing = (Thing)vThing.elementAt(j);
            	System.out.println("Out:"+myOtherThing.name.toString());
            	j++;
        	}
    
    	}
    }
    Output:

    In:Hello World
    Out:Hello World
    Last edited by anthony.rich; 02-17-2012 at 06:16 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Vector of Object not returning data

    Please explain where the problem is and what you see when you execute the program.

  3. #3
    anthony.rich is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: Vector of Object not returning data

    I have put some comments in the code above, to hopefully clearly explain that the data I put into the vector (name is a StringBuilder field containing data) isn't there when I query the vector in the loop.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Vector of Object not returning data

    Please post what the program prints on the console so we can see it.
    Add some comments to that describing what should be printed on the console.

    Try debugging the code by adding printlns to show the contents of the Vector immediately after you add items to it and then print out the contents any time you call any of the Vector's methods. The print out will help you understand what the program is doing.

    The code you posted does not compile for testing.
    Last edited by Norm; 02-17-2012 at 05:40 PM.

  5. #5
    anthony.rich is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: Vector of Object not returning data

    There are already printlns in the code - because exactly as you suggest I can't figure out the issue - the output is as follows;

    In: test
    Out:

    the first Println shows the data going into the vector, the second shows nothing coming out - does that help?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Vector of Object not returning data

    Can you post code that compiles, executes and shows the problem. The code you posted does not compile

    Remember the Vector holds a reference to an object, it does not make a copy of that object. If you change the contents of the object the old contents will not be there.

  7. #7
    anthony.rich is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default [SOLVED] Re: Vector of Object not returning data

    Well I put the code into its own little program, the full code was too long to copy paste - and guess what it works - so it must be something stupid I've done.

    Let's mark this as solved for now.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Vector of Object not returning data

    Do you change the contents of the StringBuilder? Get a new one to use. Don't reuse the one in the Vector.

    Remember the Vector holds a reference to an object, it does not make a copy of that object. If you change the contents of the object the old contents will not be there.

Similar Threads

  1. Replies: 1
    Last Post: 06-06-2011, 02:47 PM
  2. Returning an object
    By dom12 in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 12:30 PM
  3. Method, returning reference to an object
    By Saletra in forum New To Java
    Replies: 3
    Last Post: 08-23-2010, 09:22 PM
  4. Vector object into array
    By tyang in forum New To Java
    Replies: 1
    Last Post: 03-20-2010, 03:27 AM
  5. returning an object from a method
    By bigj in forum New To Java
    Replies: 7
    Last Post: 01-08-2010, 01:39 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
  •