Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: converting array into arraylist

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default converting array into arraylist

    Currently i have:
    int[] test = {1,2,3,4,5};
    ArrayList<Integer> ar = new ArrayList<Integer>(Arrays.asList(test));

    this gives me the error:
    error: no suitable constructor found for ArrayList(List<int[]>)
            ArrayList<Integer> ar = new ArrayList<Integer>(Arrays.asList(test));
                                    ^
        constructor ArrayList.ArrayList(Collection<? extends Integer>) is not applicable
          (actual argument List<int[]> cannot be converted to Collection<? extends Integer> by method invocation conversion)
        constructor ArrayList.ArrayList() is not applicable
          (actual and formal argument lists differ in length)
        constructor ArrayList.ArrayList(int) is not applicable
          (actual argument List<int[]> cannot be converted to int by method invocation conversion)
    1 error

    What i want to do is make a method that takes an array as parameter and returns an arraylist with the same elements.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: converting array into arraylist

    See the API for Arrays.asList...it accepts an array of objects - not primitives (in many cases the compiler is smart enough to interchange through something called autoboxing - in this case not). Using a simple loop you can add the items of the array to the List.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: converting array into arraylist

    bleh, thanks anyway

  4. #4

    Default Re: converting array into arraylist

    Create the method yourself. Just iterate through the array and return the elements in a new list.

    public ArrayList<T> arrayToArrayList(<T> array)
    {
         ArrayList<T> list = new ArrayList<T>();
         for(int i=0; i<array.length; i++)
              list.add(array[i]);
         return list;
    }
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: converting array into arraylist

    Quote Originally Posted by Harry Blargle View Post
    What i want to do is make a method that takes an array as parameter and returns an arraylist with the same elements.
    Then do what kenster421 said. Iterate through the array and add it all to the ArrayList.

    One small fix though. Make the parameter an "E" array. (E[] array)

    public ArrayList<E> test(E[] array) {
        //Iterating code
    }

    Be careful when using generic types with plain arrays though. They hold primitives as well as objects, which can cause problems.

  6. The Following User Says Thank You to bgroenks96 For This Useful Post:

    kenster421 (October 6th, 2011)

  7. #6

    Default Re: converting array into arraylist

    Quote Originally Posted by bgroenks96 View Post
    Then do what kenster421 said. Iterate through the array and add it all to the ArrayList.

    One small fix though. Make the parameter an "E" array. (E[] array)

    public ArrayList<E> test(E[] array) {
        //Iterating code
    }

    Be careful when using generic types with plain arrays though. They hold primitives as well as objects, which can cause problems.
    Whoops, thanks for the clarification.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  8. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: converting array into arraylist

    Hi,
    You have to use:
    Integer[] test for this.

    bean factory
    Last edited by abani; December 18th, 2011 at 02:00 AM.

Similar Threads

  1. [SOLVED] Problem copying an arraylist to an array
    By allhalf425 in forum Collections and Generics
    Replies: 4
    Last Post: September 20th, 2011, 10:25 AM
  2. Comparing elements of an arrayList with an array?
    By DudeJericho in forum Collections and Generics
    Replies: 2
    Last Post: April 21st, 2011, 12:39 PM
  3. Urgent help required - converting image to array
    By Paulious in forum Algorithms & Recursion
    Replies: 0
    Last Post: September 1st, 2010, 06:24 AM
  4. Converting an array
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2010, 09:58 AM
  5. Converting a method from ArrayList so it is capable with an Array
    By BlueJ1 in forum Collections and Generics
    Replies: 2
    Last Post: July 8th, 2009, 05:22 PM