java - Create ArrayList of arrays using Arrays.asList
问题描述
I am trying to create ArrayList of arrays with method Arrays.asList but I am struggling to achieve this goal when I have only one array to pass into the list.
List<String[]> notWithArrays = Arrays.asList(new String[] {"Slot"}); // compiler does not allow this
List<String[]> withArrays = Arrays.asList(new String[] {"Slot"},new String[] {"ts"}); // this is ok
The problem is that sometimes I have only one array to pass as argument and since it is only one iterable method asList creates List of strings out of it instead of required List<String[]>. Is there a way or method to make the list of arrays notWithArrays without having to create it manually?
Example of creating it manually:
List<String[]> withArraysManual = new ArrayList<>();
withArraysManual.add(new String[] {"Slot"});
解决方案
I think you want to create a List<String[]>
using Arrays.asList
, containing the string array {"Slot"}
.
You can do that like this:
List<String[]> notWithArrays = Arrays.asList(new String[][] {{"Slot"}});
or you can explicitly specify the generic type parameter to asList
, like this:
List<String[]> notWithArrays = Arrays.<String[]>asList(new String[] {"Slot"});
推荐阅读
- python - 使用python从多个文件导入数据
- php - 我如何在yii2应用程序中提取json数据
- postgresql - 城市的 Postgis POLYGON
- mfc - (MFC) 始终显示与 Per Monitor HiDPI 相同大小的 UI
- java - 是否可以在 heroku 非 web java 应用程序上运行?
- r - R-比较组中跨多列的行
- c++ - Libcurl 未解析的外部符号 VS2019 (cpp)
- prolog - Prolog中的堆排序
- c++ - 如何监控网络流量和浏览器活动
- mongodb - 读取 X.509 证书时出现“asn1:结构错误:整数未进行最低编码”