Results 1 to 3 of 3
  1. #1
    kleave is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default 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.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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);
        }
    }

  3. #3
    kleave is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  2. ArrayList
    By ramitmehra123 in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 12:47 AM
  3. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 08:48 PM
  4. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 11
    Last Post: 12-05-2007, 07:30 PM
  5. Help ArrayList.add()
    By eNine in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 01:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •