作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 ArrayList 列表,我想使用 addall
向它添加预定义的值
List<ArrayList<String>> places;
但我不确定该怎么做。它看起来像下面这样吗:
places.addall(["a","b","c"],["aa","bb","cc"]....);
我试过了,还是不行。
最佳答案
我认为您不能以这种方式使用 addAll,因为 addAll 需要 Collection
作为参数;在你的情况下它应该是 Collection<? extends List<String>>
因此,您需要使用您拥有的查找数据的数组创建一个集合,然后将其添加到您的 places
中。 Collection 。
我能想到的最接近的是做如下的事情,
List<List<String>> places = new ArrayList<List<String>>();
String[] string1 = new String[]{"a", "b", "c"};
String[] string2 = new String[]{"aa", "bb", "cc"};
places.add(Arrays.asList(string1));
places.add(Arrays.asList(string2));
如果你真的想用addAll
那么你将不得不做这样的事情,
List<List<String>> tempPlaces = new ArrayList<List<String>>();
String[] string1 = new String[]{"a", "b", "c"};
String[] string2 = new String[]{"aa", "bb", "cc"};
tempPlaces.add(Arrays.asList(string1));
tempPlaces.add(Arrays.asList(string2));
List<List<String>> places = new ArrayList<List<String>>();
places.addAll(tempPlaces);
关于java - 将预定义值添加到 arraylist 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35460447/
我是一名优秀的程序员,十分优秀!