Results 1 to 8 of 8
- 02-16-2012, 06:57 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
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++; } } }
In:Hello World
Out:Hello WorldLast edited by anthony.rich; 02-17-2012 at 06:16 PM. Reason: added code tags
- 02-16-2012, 07:11 PM #2
Re: Vector of Object not returning data
Please explain where the problem is and what you see when you execute the program.
- 02-17-2012, 05:03 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
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.
- 02-17-2012, 05:22 PM #4
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.
- 02-17-2012, 05:35 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
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?
- 02-17-2012, 05:43 PM #6
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.
- 02-17-2012, 06:17 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
[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.
- 02-17-2012, 06:20 PM #8
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
-
Problems with returning a TargetDataLine object..
By Addez in forum New To JavaReplies: 1Last Post: 06-06-2011, 02:47 PM -
Returning an object
By dom12 in forum New To JavaReplies: 3Last Post: 11-02-2010, 12:30 PM -
Method, returning reference to an object
By Saletra in forum New To JavaReplies: 3Last Post: 08-23-2010, 09:22 PM -
Vector object into array
By tyang in forum New To JavaReplies: 1Last Post: 03-20-2010, 03:27 AM -
returning an object from a method
By bigj in forum New To JavaReplies: 7Last Post: 01-08-2010, 01:39 PM
Bookmarks