ArrayList到java中的字符串数组

时间:2021-04-22 15:40:33
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);
}

5 个解决方案

#1


26  

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).

toArray()以适当的顺序(从第一个元素到最后一个元素)返回包含此列表中所有元素的数组。

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.

toArray(T [] a)以适当的顺序(从第一个元素到最后一个元素)返回包含此列表中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。如果列表适合指定的数组,则返回其中。否则,将为新数组分配指定数组的运行时类型和此列表的大小。

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. 列表的大小是度量。
  3. (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.
  4. (a)如果列表大小小于提供的数组的大小,则创建作为参数提供的类型的新数组。 (b)否则,列表将被转储到指定的数组中。

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

这样做的唯一好处就是你避免施法。两种形式是相同的。如果使用Object数组。即

     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:

现在,如果传递未初始化的数组,则会出现NullPointerException。最好的方法是:

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

Please read the document

请阅读该文件

#2


36  

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. 我得到了一个我想要的类型的数组。
  3. I don't have to declare the array before calling Collection.toArray(T[]).
  4. 在调用Collection.toArray(T [])之前,我不必声明数组。

#3


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.

第二个选项返回一个Object [] - 因此您必须将其强制转换为使用String方法。

#4


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]);

#5


2  

In plain java i'm use something like

在简单的java中,我使用类似的东西

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

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

用于将列表转换为数组。不知道在android中。

#1


26  

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).

toArray()以适当的顺序(从第一个元素到最后一个元素)返回包含此列表中所有元素的数组。

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.

toArray(T [] a)以适当的顺序(从第一个元素到最后一个元素)返回包含此列表中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。如果列表适合指定的数组,则返回其中。否则,将为新数组分配指定数组的运行时类型和此列表的大小。

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. 列表的大小是度量。
  3. (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.
  4. (a)如果列表大小小于提供的数组的大小,则创建作为参数提供的类型的新数组。 (b)否则,列表将被转储到指定的数组中。

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

这样做的唯一好处就是你避免施法。两种形式是相同的。如果使用Object数组。即

     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:

现在,如果传递未初始化的数组,则会出现NullPointerException。最好的方法是:

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

Please read the document

请阅读该文件

#2


36  

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. 我得到了一个我想要的类型的数组。
  3. I don't have to declare the array before calling Collection.toArray(T[]).
  4. 在调用Collection.toArray(T [])之前,我不必声明数组。

#3


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.

第二个选项返回一个Object [] - 因此您必须将其强制转换为使用String方法。

#4


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]);

#5


2  

In plain java i'm use something like

在简单的java中,我使用类似的东西

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

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

用于将列表转换为数组。不知道在android中。