4

I was just wondering what is the difference between following two approach of converting List to Array.

List<String> test = new ArrayList<String>();
test.add("AB");
test.add("BC");
test.add("CD");
test.add("DE");
test.add("EF");

String[] testarray = test.toArray(new String[0]); // passing 0 as Array size

And below one :

List<String> test = new ArrayList<String>();
test.add("AB");
test.add("BC");
test.add("CD");
test.add("DE");
test.add("EF");

String[] testarray = test.toArray(new String[test.size()]); // passing list's size

I got the same output for testarray on console.

17
  • Which java version are you using? Commented May 27, 2015 at 11:22
  • This is what the docs for JDK7 have to say about this: toArray(T[] a) Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. Commented May 27, 2015 at 11:22
  • Performance wise the second method will be slower you are pre-allocating memory Commented May 27, 2015 at 11:23
  • 4
    @VigneshKalai the first one will actually be marginally slower, since you create a useless empty array and force toList() to create a second one of the right size. Commented May 27, 2015 at 11:31
  • 1
    @TheLostMind Arrays#copyOf is intrinsic in hotspot so that code is not really run (so is System.arraycopy) - see line 748 of this file. Commented May 27, 2015 at 11:56

1 Answer 1

11
public <T> T[] toArray(T[] a)

a - This is the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose. So in first case a new array is being created while in second case, it is using the same array.

Sample Code :

Case -1 : array being passed can hold elements of list

public static void main(String[] args) {
        List<String> test = new ArrayList<String>();
        test.add("AB");
        test.add("BC");
        test.add("CD");
        test.add("DE");
        test.add("EF");
        String[] s= new String[10];
        String[] testarray = test.toArray(s); 
        System.out.println(s==testarray);
    }

O/P :

true

Case-2 : Array being passed cannot hold elements of list

public static void main(String[] args) {
        List<String> test = new ArrayList<String>();
        test.add("AB");
        test.add("BC");
        test.add("CD");
        test.add("DE");
        test.add("EF");
        String[] s= new String[0];
        String[] testarray = test.toArray(s); 
        System.out.println(s==testarray);

    }

O/P :

false
Sign up to request clarification or add additional context in comments.

2 Comments

@TheLostMind - Now, it's pretty clear from the code snippet...!! Thanks
additional 1 for improving my answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.