ArrayList中以字符串的Java数组 [英] ArrayList to Array of Strings in java

查看:116
本文介绍了ArrayList中以字符串的Java数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 的ArrayList<字符串> newArray =新的ArrayList<字符串>();
    newArray = urlList.getUrl();
    的for(int i = 0; I< newArray.size();我++)
    {
        的System.out.println(newArray.get(一));
    }

    newArray.toArray(mStrings)是//这是正确的
    mStrings = newArray.toArray(); //或者这此转换的ArrayList OT String数组

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



编辑:当我尝试如下,我得到空指针异常

尝试
        {
                newArray.toArray(mStrings);
                的for(int i = 0; I< mStrings.length;我++)
                {
                    的System.out.println(mStrings [I]);
                }
        }赶上(NullPointerException异常E)
        {
            的System.out.println(E);
        }
 

解决方案

取决于你想要做的事。两者都是正确的。

  

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

  

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

请参照<一href="http://download.oracle.com/javase/6/docs/api/java/util/List.html#toArray%28T%5b%5d%29">here

在前者,你想获得一个数组。在后者,你有一个数组,你只是想填补它。

在你的情况,第一种形式是preferred你只想得到一个数组而没有大小或细节。


基本上,这是第2次的情况下会发生什么:

  1. 在列表的大小措施。
  2. (a)如果列表大小小于所述阵列的设置,对提供作为参数的类型的创建新阵列。

    (二)否则,该名单被倾倒在指定数组中

这样做的唯一好处,就是避免铸造。这两个表格是相同的。如果您使用的对象数组。即。

  myList.toArray()&LT; ==&GT;的toArray(新对象[0]​​)
 

现在,如果你传递一个未初始化数组,你会得到一个NullPointerException异常。做到这一点的最好办法是:

 的String [] Y = x.toArray(新的String [0]);
 

请仔细阅读文件

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

解决方案

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

这篇关于ArrayList中以字符串的Java数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆