Results 1 to 5 of 5
  1. #1
    katturv is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default generic in arraylist

    hi
    i have an arraylist
    ArrayList<String> data= new ArrayList<String>();
    data.add("goodbye");
    Iterator<String> it = data.iterator();
    while (it.hasNext())
    {
    String s = it.next();
    System.out.println(s);
    }

    then i iterate it and print the value
    now if i change my generic to int type what will happen
    basically what is use of generic in collections????

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by katturv View Post
    now if i change my generic to int type what will happen
    What happens when you try this? I'll ask you another question -- Can you ever put an int into an ArrayList even if it's not generic? If you think that this is in fact possible, then you should create a small program that does this and then check the actual type of the object held by the ArrayList.

    basically what is use of generic in collections????
    It is useful in that it allows for compile-time type checking. What problems are you having specifically?

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    you cannot do this:
    Java Code:
    List<int> myList=new ArrayList<int>(); // wont compile
    int inty=10; // wont ever get here
    myList.add(inty); // or here
    you can do this:
    Java Code:
    List<Integer>myList=new ArrayList<Integer>();
    int inty=10; 
    myList.add(inty); // Auto boxing
    research the reason's why

  4. #4
    katturv is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default generic in arraylist

    hi
    i have an arraylist
    ArrayList<String> data= new ArrayList<String>();
    data.add("goodbye");
    Iterator<String> it = data.iterator();
    while (it.hasNext())
    {
    String s = it.next();
    System.out.println(s);
    }

    then i iterate it and print the value
    now if i change my generic to Integer type what will happen
    initially the code gets compiled but
    after that i am changing it to
    ArrayList<Integer> data= new ArrayList<Integer>();
    i was asked in an interview this questn......
    basically what is use of generic in collections????

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    first of all if you declare the data variable again you will get an error like "Duplicate local variable data". but even if you try this

    data= new ArrayList<Integer>();

    you will get an error like "Type mismatch: cannot convert from ArrayList<Integer> to ArrayList<String>"

    i can answer your short question "basically what is use of generic in collections????" with a short answer: to make collections type safe!

Similar Threads

  1. generic code
    By mac in forum New To Java
    Replies: 4
    Last Post: 05-27-2010, 05:06 PM
  2. generic types
    By jon80 in forum New To Java
    Replies: 6
    Last Post: 06-12-2009, 10:29 PM
  3. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  4. A generic interface example
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:42 PM
  5. Generic array
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 12:12 AM

Posting Permissions

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