ArrayList newArray = new ArrayList();
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);
}
取决于你想做什么两者都是正确的
toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
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.
在前者,你想得到一个数组.后者你有一个数组,你只是想填补它.
在您的情况下,首选形式是首选,因为您只希望得到一个数组,而不必担心大小或细节.
基本上这是第二种情况发生的情况:
列表的大小是措施.
>(a)如果列表大小小于提供的数组的大小,则创建作为参数提供的类型的新数组.(b)否则,列表将转储到指定的数组中.
这样做的唯一好处是避免投射.两种形式是一样的.如果使用Object数组.即
myList.toArray() <==> toArray(new Object[0])
现在,如果你传递一个未初始化的数组,你会得到一个NullPointerException.最好的办法是:
String[] y = x.toArray(new String[0]);
请阅读文件