17
ArrayList<String> newArray = new ArrayList<String>();
newArray = urlList.getUrl();
for( int i = 0 ; i < newArray.size();i++)
{
    System.out.println(newArray.get(i));
}

newArray.toArray(mStrings );// is this correct 
mStrings = newArray.toArray();//  or this to convert ArrayList ot String array here

for( int i = 0 ; i < mStrings.length;i++)
{
    System.out.println(mStrings[i]);
}

EDIT: when i try as below, I get null pointer exception:

try
{
    newArray.toArray(mStrings );
    for(int i = 0 ; i < mStrings.length; i++)
    {
        System.out.println(mStrings[i]);
    }
} catch( NullPointerException e )
{
    System.out.println(e);
}
Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
pradeep
  • 3,005
  • 12
  • 42
  • 65
  • can you please include the full code block in the edited area so we can see the initialisation? – laura Mar 05 '11 at 08:30
  • and also tell us which line the exception is thrown at – laura Mar 05 '11 at 08:31
  • possible duplicate of [Convert ArrayList containing Strings to an array of Strings in Java?](http://stackoverflow.com/questions/4042434/convert-arraylist-containing-strings-to-an-array-of-strings-in-java) – Sruit A.Suk Aug 05 '15 at 07:41

5 Answers5

43

Usually I write

String[] array = collection.toArray(new String[collection.size()]);

because this way

  1. I get an array of the type that I want.
  2. I don't have to declare the array before calling Collection.toArray(T[]).
rlibby
  • 5,931
  • 20
  • 25
  • 6
    Point 2 is in contrast to something like `String[] array = new String[collection.size()]; array = collection.toArray(array);`. Other answers suggest `new String[0]`, which is okay, but creates an extra temporary object. – rlibby Sep 29 '11 at 23:35
33

Depends on what you want to do. Both are correct

toArray() Returns an array containing all of the elements in this list in proper sequence (from first to last element).

Refer here

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. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

Refer here

In former, you want to get an array. In latter you have an array, you just wanted to fill it up.

In your case, first form is preferred as you just want to get an array without bothering size or details.


Basically this is what happens in 2nd case:

  1. List's size is measures.
  2. (a) If list size is less than that of the array provided, new Array of the type provided as argument is created.

    (b)Else, the list is dumped in the specified array.

The only benefit of doing so, is you avoid casting. The two form are the same. If you use Object array. i.e.

     myList.toArray() <==> toArray(new Object[0])

Now, If you pass an uninitialized array, you will get a NullPointerException. The best way to do it is:

 String[] y = x.toArray(new String[0]);

Please read the document

Nishant
  • 54,584
  • 13
  • 112
  • 127
  • try { newArray.toArray(mStrings ); }catch( NullPointerException e ) { System.out.println(e); } i get nullpointer exception here – pradeep Mar 05 '11 at 07:27
3

The first option is better as it allows you to pass in a typed array, which is then populated inside the method.

The second option returns an Object[] - so you would have to cast it to use String methods.

laura
  • 2,951
  • 9
  • 44
  • 61
  • try { newArray.toArray(mStrings ); for( int i = 0 ; i < mStrings.length;i++) { System.out.println(mStrings[i]); } }catch( NullPointerException e ) { System.out.println(e); } i get nullPointer exception here – pradeep Mar 05 '11 at 07:30
  • you could try asList() instead of toArray if you want an ArrayList - or declare newarray as new String[mstring.size()] – laura Mar 05 '11 at 07:34
  • @rajivpradeep, [`Collection.toArray()`](http://download.oracle.com/javase/6/docs/api/java/util/Collection.html#toArray(T\[\])) throws a `NullPointerException` if you pass it a null reference. – rlibby Mar 05 '11 at 07:45
3

In plain java i'm use something like

rolesList.toArray(new Integer[rolesList.size()]);

for converting list to array. Don't know in android.

Alexey Sviridov
  • 3,360
  • 28
  • 33
3

How about this

 List a = new ArrayList();
   a.add("wer");
   a.add("sff");
   String[] f= (String[]) a.toArray(new String[0]);
   System.out.println(f[0]);
Dead Programmer
  • 12,427
  • 23
  • 80
  • 112