Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-31-2008, 03:04 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Declaring an ArrayList
HI,

Please review the code below:

Code:
Collection <String>arrayList1 = new ArrayList<String> (); ArrayList <String>arrayList2 = new ArrayList<String> ();
What is the real difference between arrayList1 and arrayList2? Both works the same ????

Cheers.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-31-2008, 03:12 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 296
tim is on a distinguished road
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?
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-31-2008, 06:46 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Thanks Tim. It means both can only store ArrayLists. Even arrayList1 cannot store other collections. Is this so?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-31-2008, 08:36 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 296
tim is on a distinguished road
Quote:
Originally Posted by bugger View Post
Thanks Tim. It means both can only store ArrayLists. Even arrayList1 cannot store other collections. Is this so?
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:
Code:
Collection <String> test1 = null; ArrayList <String> test2 = null;
The following is okay
Code:
test1 = new ArrayList<String>(); test1 = new Vector<String>(); test2 = new ArrayList<String>();
The following is NOT okay
Code:
test1 = new Integer(10); test2 = new Vector<String>();
These are polymorphism concepts. I am sorry if I confused you. I'm trying my best to explain this.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Project Trouble: Searching one ArrayList with another ArrayList BC2210 New To Java 2 04-21-2008 12:43 PM
Declaring a Queue rhm54 New To Java 1 03-21-2008 06:02 AM
Declaring Vector mew New To Java 1 12-05-2007 09:14 PM
Declaring Interface Java Tip Java Tips 0 11-08-2007 09:41 AM
Declaring Enumeration Java Tip Java Tips 0 11-04-2007 06:59 PM


All times are GMT +3. The time now is 12:09 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org