关于android:如何转换arraylist< string>

How to Convert arraylist<string> to string[]

本问题已经有最佳答案,请猛点这里访问。
1
2
3
4
5
6
7
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的数组列表,但当我看到GGGGGGG时,它变为05-09 [Ljava.lang.String;@b125cf00


可以将arraylist转换为string[]。

1
2
3
4
5
6
7
8
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));
}

或者你也可以这样做-

1
2
3
4
5
6
7
8
9
10
11
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
2
3
4
5
List<String> list = new ArrayList<String>();
        list.add("Item 1");
        list.add("Item 2");
        String joined = TextUtils.join(",", list);
        Log.d("tanim", joined);

我在这篇文章中找到了这个代码。它可以帮助你点击


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

来自Java文档http://DOCS.Oracle .COM/JavaSe/ 7 /DOCS/API/Java/UTL/ARAYLIST. HTML HTML to数组(T[])

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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