Results 1 to 4 of 4
Thread: Declaring an ArrayList
- 01-31-2008, 02:04 PM #1
Senior Member
- Join Date
- Nov 2007
- Posts
- 111
- Rep Power
- 0
- 01-31-2008, 02:12 PM #2
Same instances
Hello bugger.
arrayList1 and arrayList2 are both instances of ArrayList, but they are of different type. arrayList1 can contain a Collection object. Although arrayList1 is actually an ArrayList, the compiler will see it as an object of type Collection. To use arrayList1 as an ArrayList you must first cast it. arrayList2 is an object of type ArrayList and no cast is necessary. But, in essence they are both still ArrayList objects.
Does that help? :DEyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-31-2008, 05:46 PM #3
Senior Member
- Join Date
- Nov 2007
- Posts
- 111
- Rep Power
- 0
Thanks Tim. It means both can only store ArrayLists. Even arrayList1 cannot store other collections. Is this so?
- 01-31-2008, 07:36 PM #4
That is not true. arrayList1 can store any instance of Collection, but arrayList2 can only store instances of ArrayList or any subclasses of it. For example:
The following is okayJava Code:Collection <String> test1 = null; ArrayList <String> test2 = null;
The following is NOT okayJava Code:test1 = new ArrayList<String>(); test1 = new Vector<String>(); test2 = new ArrayList<String>();
These are polymorphism concepts. I am sorry if I confused you. :o I'm trying my best to explain this.Java Code:test1 = new Integer(10); test2 = new Vector<String>();
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
Similar Threads
-
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
Declaring a Queue
By rhm54 in forum New To JavaReplies: 1Last Post: 03-21-2008, 05:02 AM -
Declaring Vector
By mew in forum New To JavaReplies: 1Last Post: 12-05-2007, 08:14 PM -
Declaring Interface
By Java Tip in forum Java TipReplies: 0Last Post: 11-08-2007, 08:41 AM -
Declaring Enumeration
By Java Tip in forum Java TipReplies: 0Last Post: 11-04-2007, 05:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks