This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Apr 11, 2006 11:25 AM by 807592 RSS

Use of an ArrayList

807592 Newbie
Currently Being Moderated
I am unsure of how to retrieve values from an ArrayList using the get() method. e.g
Object []array;
          ArrayList list;
          int[] intArr = {1,2,3,4,5,6,7,8};
          list = new ArrayList();
           list.add(intArr);
           array = list.toArray();
how do I retrieve the individual int values,
  • 1. Re: Use of an ArrayList
    796447 Newbie
    Currently Being Moderated
    list.get(0) would return the first (0-based) item in the list.
    list.get(1) would return the next one, etc.
    Should be pretty intuitive.
  • 2. Re: Use of an ArrayList
    796440 Guru
    Currently Being Moderated
    You have added one single element to your ArrayList. That element is an array.

    You want to add each of the integer elements. You can loop over the array, calling add for each element, one by one, or you may be able to use Arrays.asList.

    Note that you cannot put primitives into a Collection such as an ArrayList. You'd have to add Integer wrapper objects. Autoboxing in 1.5 can make this transparent to you, such that you seem to be adding ints, but I'm not sure if Arrays.asList handles this.

    http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html
  • 3. Re: Use of an ArrayList
    807592 Newbie
    Currently Being Moderated
    I know the toArray() method returns an object array, but how do I use the get() method to retrieve the individual int values, do I have to cast each to an int value (something similar to intValue for vectors) ?
  • 4. Re: Use of an ArrayList
    796440 Guru
    Currently Being Moderated
    http://java.sun.com/docs/books/tutorial/collections/
  • 5. Re: Use of an ArrayList
    796447 Newbie
    Currently Being Moderated
    Oh wait a minute. You're trying to add an integer array to the list? That won't work. That just makes the list contain one item, an integer array - not several items, each an integer.
  • 6. Re: Use of an ArrayList
    796440 Guru
    Currently Being Moderated
    Oh wait a minute. You're trying to add an integer
    array to the list? That won't work.
    Well, it will work. It just might not work in the way the OP wants it to. :-)
  • 7. Re: Use of an ArrayList
    807592 Newbie
    Currently Being Moderated
    Thanks Jverd, heres my code, Im just using it as practice to get use to using arrayList, please tell me if its sensible.

    import java.util.*;
    public class array_List{
         public static void main(String[]args){
    
               ArrayList list;
              int[] intArr = {1,2,3,4,5,6,7,8};
    
              list = new ArrayList();
              Integer myInt;
    
              for(int i = 0; i<intArr.length; i++){
                   myInt = new Integer(intArr);
                   list.add(myInt);
                   System.out.print(list.get(i)+" ");
              }


         }
    }
  • 8. Re: Use of an ArrayList
    807592 Newbie
    Currently Being Moderated
    What you are doing with your code is turning an ArrayList of arrays into an Array of arrays. I think what you are wanting to do is the following but I may be wrong.
    Object []array;
              ArrayList list;
              int[] intArr = {1,2,3,4,5,6,7,8};
              list = new ArrayList();
               list.add(intArr);
    
    Iterator listIter = list.iterator();
    while(listIter.hasNext())
    {
         Object[] tmpArray = (Object[])iter.next();
          // Get the values int the array
          int val1 = tmpArray[0];
          int val2 = tmpArray[1];
          etc....
    }
  • 9. Re: Use of an ArrayList
    796447 Newbie
    Currently Being Moderated
    Thanks Jverd, heres my code, Im just using it as
    practice to get use to using arrayList, please tell
    me if its sensible.
    Apart from some minor style issues, it looks ok or "sensible".
    I'd declare and initialize/instantiate variables at the nearest scope, so rather than declaring "list" on one line, only a few lines later to instantiate it, I'd do that on one line. And the "myInt" variable isn't used outside the loop, so move that inside. Actually you don't even need that variable at all. You could just do: list.add(new Integer(intArr[ i ]))
  • 10. Re: Use of an ArrayList
    796440 Guru
    Currently Being Moderated
    Thanks Jverd, heres my code, Im just using it as
    practice to get use to using arrayList, please tell
    me if its sensible.
    Looks okay at a quick glance.

    Does it print out what you expect?

    One thing to be aware of: Usually with collections, you walk through their elements with an Iterator, rather than with get(). (Not all collections even have a get()). With an ArrrayList, since it implements RandomAccess (it's backed by an array), you can get an element from an arbitrary index position with get() with O(1) performance, but if you had a LinkedList instead of an ArrayList, you wouldn't want to do that.
  • 11. Re: Use of an ArrayList
    807592 Newbie
    Currently Being Moderated
    Thanks Jverd, I appreciate your help, you've been really helpful, I will make use of the the listIterator mentioned, and modify my code so its more efficient.
  • 12. Re: Use of an ArrayList
    796447 Newbie
    Currently Being Moderated
    Thanks Jverd, I appreciate your help, you've been
    really helpful, I will make use of the the
    listIterator mentioned, and modify my code so its
    more efficient.
    You're welcome, and by the way I'm not part of the collective you are calling "Jverd".
  • 13. Re: Use of an ArrayList
    796440 Guru
    Currently Being Moderated
    Thanks Jverd, I appreciate your help, you've been
    really helpful, I will make use of the the
    listIterator mentioned, and modify my code so its
    more efficient.
    Most of the time iterator should be sufficient. The main benefit of ListIterator I think is that you can go backwards. I don't think I've ever needed that.
  • 14. Re: Use of an ArrayList
    807592 Newbie
    Currently Being Moderated
    hi , I am also facing some problem in exaclty same type of problem.

    List list = Arrays.asList(intArr);

    all the following statements are giving same output:

    System.Out.Println(list); // [[I@82ba41]
    System.Out.Println(list.get(0)); // [I@82ba41

    for(Iterator itr=list.iterator();itr.hasNext();)
    {
         Object key = itr.next();
         System.out.println("NN"+key); //[I@82ba41
    }

    i know this is becoz of printing an object ... but i am getting error on converting an obect to an integer....
    plz tell how to print this kind of list that i created by converting an integer array......

    Thanks & regards
1 2 Previous Next