如何将ArrayList<string>转换为string[]数组

3
public SampleGridViewAdapter(Context context,ArrayList<String> urls) {
    this.context = context;
    this .urls=urls;
    Log.i("DDDDDDDDDD",String.valueOf(urls));
    simpleArray = urls.toArray(new String[urls.size()]);
    Log.i("GGGGGGGGGGG",String.valueOf(simpleArray));
}

当我在日志中打印DDDDDDD时,输出的是一个URL的arraylist,但是当我看到GGGGGGG时,它变成了05-09 [Ljava.lang.String;@b125cf00


尝试打印:Log.i("GGGGGGGGGGG",Arrays.toString( simpleArray )); - WannaBeGeek
3个回答

2
你可以将ArrayList转换为String[]。
public SampleGridViewAdapter(Context context,ArrayList<String> urls) {
    this.context = context;
    this .urls=urls;
    Log.i("DDDDDDDDDD",String.valueOf(urls));
    String[] simpleArray = new String[urls.size()];
    simpleArray = urls.toArray(simpleArray);
    Log.i("GGGGGGGGGGG",String.valueOf(simpleArray));
}

您可以采用以下方式进行操作 -
public SampleGridViewAdapter(Context context,ArrayList<String> urls) {
    this.context = context;
    this .urls=urls;
     Log.i("DDDDDDDDDD",String.valueOf(urls));
     Object[] simpleArray = urls.toArray();

     for(int i = 0; i < simpleArray.length ; i++){
     Log.d("string is",(String)simpleArray[i]);
    }
  Log.i("GGGGGGGGGGG",String.valueOf(simpleArray));
}   

@Driod 我尝试了两种方式,但答案仍然相同。 - Naveen
@Naveen,请检查这个 - Log.i(“GGGGGGGGGGG”,Arrays.toString(simpleArray)); - Narendra Singh
当我从for循环中获取字符串时,我得到了存储在strBook中的URL,但是当我从我的片段中使用gridView.setAdapter(new SampleAdapter(getActivity(),Arraylist<string> urls))时,它会抛出空指针异常。 - Naveen
好的,请您确切地检查一下什么是空值?您确定这个字符串数组是空的吗? - Narendra Singh
调试并检查您收到的日志...还要在适配器的构造函数中放置try-catch块,以便您可以了解确切的问题。 - Narendra Singh

0
ArrayList<String> list = new ArrayList() ;
String[] array = list.toArray(new String[list.size()]);

来自Java文档
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#toArray(T[])

toArray
public <T> T[] 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.
If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
Specified by:
     toArray in interface Collection<E>
Specified by:
    toArray in interface List<E>
Overrides:
    toArray in class AbstractCollection<E>
Parameters:
    a - 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.
Returns:
     an array containing the elements of the list
Throws:
     ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this list
    NullPointerException - if the specified array is null

我已经多次使用它,而且它总是对我有效。但是我也看到有人给出了一个大小为0的数组,因为正如Java文档所说,如果数组不够大,则允许使用具有正确容量的新数组。 - EpicPandaForce
我已经做了这个,但仍然存在空指针异常。 - Naveen
那么你的ArrayList为空。 - EpicPandaForce

0
你可以尝试这个:
List<String> list = new ArrayList<String>();
        list.add("Item 1");
        list.add("Item 2");
        String joined = TextUtils.join(", ", list);
        Log.d("tanim", joined); 

我从这篇文章中找到了这段代码。它可能会对你有所帮助点击


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接