如何将arraylist <string>转换为string []

时间:2015-05-09 06:39:23

标签: android

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时,输出是网址的arraylist,但是当我看到GGGGGGG时,它会更改为05-09 [Ljava.lang.String;@b125cf00

3 个答案:

答案 0 :(得分:1)

您可以将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));
}   

答案 1 :(得分: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

答案 2 :(得分:0)

你可以试试这个:

List<String> list = new ArrayList<String>();
        list.add("Item 1");
        list.add("Item 2");
        String joined = TextUtils.join(", ", list);
        Log.d("tanim", joined); 

我从这篇文章中找到了这段代码。它可以帮助你{{3}}