aspose file tools*
The moose likes Beginning Java and the fly likes ArrayList class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList class" Watch "ArrayList class" New topic
Author

ArrayList class

memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Hello,
I noticed that ArrayList class does not have toString() method. But, in spite of this, when we use get() method in loops it behaves like that it has a toString method().
Why???

Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
All class definitions extends Object, and since Object has the public method toString, every class definition contains the public method toString
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Note that the get method simply returns an element contained in the ArrayList, so the toString implementation depends on the element type. It has nothing to do with how toString is implemented in ArrayList.

But as a side note, the toString implementation in ArrayList is inherited from AbstractCollection.


"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
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Thanks your replies but I have still some problems. Although, we do not override toString method , there is no any problem derived. Why??
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by memati bas:
... Although, we do not override toString method , there is no any problem derived. Why??

Derived classes can override toString, but they are not required to (because the method is not abstract). Without overriding, the inherited implementation is used.

Does that answer the question?
[ February 02, 2006: Message edited by: marc weber ]
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Oh , thaanks.
I think I understood it.
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Latly , even though we only use the members of an object while using get() method, I think we can also use instance variables which do not belong to the objevt class indirectly, is not it??
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by memati bas:
Latly , even though we only use the members of an object while using get() method, I think we can also use instance variables which do not belong to the objevt class indirectly, is not it??

An ArrayList can hold any type of object reference. In fact, when a reference is added to the List, its type is automatically upcast to Object, which is the base class of all objects.

Prior to Java 1.5, when getting an element from a List, you needed to explicitly downcast the reference back to the desired type.

Since Java 1.5, Lists ensure type safety with generics. So you can create an ArrayList that will only allow specific types to be added, and the downcast is handled automatically.

Here's an illustration...
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Oh,
Thanks, thanks and thanks for your explainatory replies Marc.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList class
 
Similar Threads
Why I get the List content when I print the List object?
"-1 if this list does not contain the element." problem using arraylist
ArrayList test not working?
Can we get an arraylist from String.Please check the code.
Class objects into ArrayList