用Java将列表转换为数组<div id="result"></div>const $source = document.querySelector('#source');const $result = document.querySelector('#result');const typeHandler = function(e) { $result.innerHTML = e.target.value;}$source.addEventListener('input', typeHandler) // register for oninput$source.addEventListener('propertychange', typeHandler) // for IE8// $source.addEventListener('change', typeHandler) // fallback for Firefox for <select><option>, for <input> oninput is enough<input id="source" /><div id="result"></div>如何转换List转到Array在爪哇?检查下面的代码:ArrayList<Tienda> tiendas;List<Tienda> tiendasList; tiendas = new ArrayList<Tienda>();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); 我需要填充数组tiendas带着.的价值tiendasList.
3 回答
![?](http://img1.sycdn.imooc.com/545862770001a22702200220-100-100.jpg)
叮当猫咪
TA贡献1776条经验 获得超12个赞
Foo[] array = list.toArray(new Foo[0]);
Foo[] array = new Foo[list.size()];list.toArray(array); // fill the array
List<Integer> list = ...;int[] array = new int[list.size()];for(int i = 0; i < list.size(); i++) array[i] = list.get(i);
最新情况:
list.toArray(new Foo[0]);
list.toArray(new Foo[list.size()]);
.
来自JetBrains IntelliJ IDEA的检查:
将集合转换为数组有两种样式:要么使用预先大小的数组(如 c.toArray(新字符串[C.size()]))或使用空数组(如 c.toArray(新字符串[0]).
在较早的Java版本中,建议使用预大小数组,因为创建适当大小的数组所必需的反射调用非常慢。但是,由于OpenJDK 6的后期更新,这个调用是复杂的,使得空数组版本的性能与预先大小的版本相同,有时甚至更好。同时,传递预先大小的数组对于并发或同步的集合也是危险的,因为在 大小和 toArray调用,如果集合在操作期间同时收缩,则可能会在数组末尾产生额外的空值。
这种检查允许遵循统一的样式:要么使用空数组(这是现代Java中推荐的),要么使用预先大小的数组(在旧的Java版本或基于非热点的JVM中可能更快)。
![?](http://img1.sycdn.imooc.com/533e4ce900010ae802000200-100-100.jpg)
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
String[] strings = list.stream().toArray(String[]::new);
![?](http://img1.sycdn.imooc.com/545847f50001126402200220-100-100.jpg)
www说
TA贡献1775条经验 获得超8个赞
//Creating a sample ArrayList List<Long> list = new ArrayList<Long>(); //Adding some long type valueslist.add(100l);list.add(200l);list.add(300l); //Converting the ArrayList to a LongLong[] array = (Long[]) list.toArray(new Long[list.size()]); //Printing the resultsSystem.out.println(array[0] + " " + array[1] + " " + array[2]);
它创建一个具有原始列表大小的新的长数组。 它使用新创建的数组将原始的ArrayList转换为数组。 它将该数组转换为一个长数组(long[]),我将其恰当地命名为“Array”。
添加回答
举报
0/150
提交
取消