发布日期:2018-03-26
如何使用Java将列表转换为数组+ 查看更多
如何使用Java将列表转换为数组
+ 查看更多
发布日期:2018-03-10 10:44
分类:JAVA
浏览次数:95
在Java中我如何能将一个列表类型转换为一个数组类型?
检查下面的代码:
检查下面的代码:
ArrayListtiendas; List tiendasList; tiendas = new ArrayList (); Resources res = this.getBaseContext().getResources(); XMLParser saxparser = new XMLParser(marca,res); tiendasList = saxparser.parse(marca,res); tiendas = tiendasList.toArray(); this.adaptador = new adaptadorMarca(this, R.layout.filamarca, tiendas); setListAdapter(this.adaptador);
我需要用tiendasList的值来填充数组tiendas。
最佳答案
你可以这样:Foo[] array = list.toArray(new Foo[list.size()]);或者:
Foo[] array = new Foo[list.size()]; list.toArray(array); // fill the array注意,这只适用于引用类型数组。 对于原始类型数组,使用传统方式:
Listlist = ...; int[] array = new int[list.size()]; for(int i = 0; i < list.size(); i++) array[i] = list.get(i);