This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
An array has a fixed size that's determined when it's created. You can think of an ArrayList as an array that re-sizes itself as needed. Unlike an array, an ArrayList cannot hold primitive values (without wrapping them as objects).
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: Oct 08, 2006
Thanks Marc... if my object got 2 attribute, one is countryName and another is countryPopulation.....i want to retrieve this object to store the value into arrayList.....it is right to use arrayList???this is my first time using java and jsp and db4o database as my project....i very new in all this language.....
I think it's OK to use ArrayList. For example, you first retrieve the data from database and wrap the countryName and countryPopulation attributes into a Country object, then you can add this country object into a list of countries.
Originally posted by Mag Yeoh: ...i still cannot print out the object value...i only print out the object address only.... can i put String to replace Object???Actually what is Object this mean.....another question is what is ArrayList<String> this mean???
An Object is an array that hold references to Objects. This means that when you add something to an array, the reference is upcast to type Object. So in order to use something in in an Object array, you normally need to downcast the reference back to its true type (for example, (MyType)array[x]...).
Typically, arrays are created with the desired type in mind. So yes, if your array is intended to hold Strings, then you should probably use String instead of Object.
In Collections (for example, an ArrayList), all references are automatically upcast to type Object. Prior to the introduction of generics in Java 5, it was left to the programmer to know what was being added to a collection and ensure that it was being correctly downcast when used. But with generics, the type can be specified. For example, ArrayList<String> is an ArrayList that holds Strings. (Behind the scenes, the references are still upcast to type Object. But the generic type allows the compiler to check what's actually going in, and automaticaly insert the correct downcasting.)
If your references are printing as class names and hashCodes, this suggests that the toString() method has not been overridden.
But the question here is what type of objects does your ObjectSet contain?