Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 11-19-2007, 03:33 PM
Member
 
Join Date: Nov 2007
Posts: 2
kleave is on a distinguished road
New to arraylist
Hi guy,

I am very confuse about the arraylist and I would like to check with you guy with my understanding with arraylist.

1) I understand that arraylist only hold object types and they did not hold primitive types right?

2) If you want to convert object type to primitive type you have to use wrapper class or casting is it?

3) Must data type be declared for creating new ArrayList?

4) If I use the arraylist get() method with or without data type declaration is it return object type?

Thank you for reading my post, I hope I can able to clear my confuse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-19-2007, 06:24 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.util.*; public class ArrayListTest { public static void main(String[] args) { // I understand that arraylist only hold object types and // they did not hold primitive types right? // Yes. In j2se 1.5 autoboxing was introduced so you // can treat them as if they will accept primitives. List<Integer> list1 = new ArrayList<Integer>(); list1.add(Integer.valueOf(1)); list1.add(2); System.out.printf("list1.get(0) = %d list1.get(1) = %d%n", list1.get(0), list1.get(1)); // If you want to convert object type to primitive type you // have to use wrapper class or casting is it? // Before j2se 1.5 - yes. You can still do this after v1.5 List<Integer> list2 = new ArrayList<Integer>(); list2.add(Integer.valueOf(21)); int day1 = ((Integer)list2.get(0)).intValue(); int day2 = list2.get(0).intValue(); System.out.printf("day = %d day2 = %d%n", day1, day2); // Must data type be declared for creating new ArrayList? // In j2se 1.5+ it is recommended but not required. If you // don't you can get "Xlint:unchecked" compiler warnings // which can be suppressed in later versions. List list3 = new ArrayList(); // compiler warning List<String> list4 = new ArrayList<String>(); // If I use the arraylist get() method with or without data // type declaration is it return object type? list3.add("hello"); list4.add("world"); System.out.println("list3.get(0) type = " + list3.get(0).getClass().getName()); System.out.println("list4.get(0) type = " + list4.get(0).getClass().getName()); // You don't have to cast from Object to specific type if // you use generic types. You do need to cast if you do not // use generic types. //String sx = list3.get(0); // compile error String s3 = (String)list3.get(0); String s4 = list4.get(0); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-19-2007, 08:45 PM
Member
 
Join Date: Nov 2007
Posts: 2
kleave is on a distinguished road
thank alot for your reply. Now I understand better.
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 01:43 PM
ArrayList ramitmehra123 New To Java 1 02-07-2008 02:47 AM
ArrayList kizilbas1 New To Java 1 01-12-2008 10:48 PM
ArrayList kizilbas1 New To Java 11 12-05-2007 09:30 PM
Help ArrayList.add() eNine New To Java 2 08-06-2007 03:13 PM


All times are GMT +3. The time now is 03:36 AM.


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