如何将 arraylist<string> 转换为 string[] [重复]
Posted
技术标签:
【中文标题】如何将 arraylist<string> 转换为 string[] [重复]【英文标题】:How to Convert arraylist<string> to string[] [duplicate] 【发布时间】:2015-07-20 03:21:41 【问题描述】: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("GGGGGGGGGG",Arrays.toString( simpleArray )); 【参考方案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));
【讨论】:
@Driod 我尝试了两种方式,但答案仍然相同 @Naveen 检查这个 - Log.i("GGGGGGGGGG",Arrays.toString(simpleArray )); 当我从 for 循环中获取字符串时,当我从我的片段中使用 gridView.setAdapter(new SampleAdapter(getActivity(),ArraylistArrayList<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 文档所说,如果数组不够大,则允许使用具有正确容量的新数组。 我做了这个 bt 仍然有空指针异常 那么你的 ArrayList 为空。【参考方案3】:你可以试试这个:
List<String> list = new ArrayList<String>();
list.add("Item 1");
list.add("Item 2");
String joined = TextUtils.join(", ", list);
Log.d("tanim", joined);
我从这篇文章中找到了这段代码。它可以帮助你click
【讨论】:
以上是关于如何将 arraylist<string> 转换为 string[] [重复]的主要内容,如果未能解决你的问题,请参考以下文章
如何从 HashMap 中提取 ArrayList 并在 Java 中循环遍历它?
如何将 arraylist<string> 转换为 string[] [重复]
如何将 HashMap<String, ArrayList<String>> 存储在列表中?
将 ArrayList<String> 转换为 String[] [重复]