添加到 arraylist 列表
Adding to list of arraylist
我正在尝试添加存储在 tempEdges 中的以下 2 个:
- [RoyalElephant, IS-A, Elephant]
- [RoyalElephant, IS-NOT-A, Gray]
尽管我只想将每个数组列表的最后 2 个元素添加到 copiedPaths 中的 2 个数组列表。
数组列表是:
public static List<ArrayList<String>> copiedPaths = new ArrayList<>();
public static List<ArrayList<String>> tempEdges = new ArrayList<>();
无效代码是:
copiedPaths.get(0).add(tempEdges.get(0).get(1));
copiedPaths.get(0).add(tempEdges.get(0).get(2));
copiedPaths.get(1).add(tempEdges.get(1).get(1));
copiedPaths.get(1).add(tempEdges.get(1).get(2));
这没有按预期工作,因为两个数组都添加了 IS-NOT-A,灰色而不是具有 IS-A[=26 的数组=]、大象和另一个 IS-NOT-A、灰色
Java 9+解法:
List<List<String>> tempEdges = List.of(List.of("RoyalElephant", "IS-A", "Elephant"),
List.of("RoyalElephant", "IS-NOT-A", "Gray"));
List<List<String>> copiedPaths = tempEdges.stream()
.map(list -> list.subList(1, list.size()))
.collect(Collectors.toList());
System.out.println(tempEdges);
System.out.println(copiedPaths);
对于 Java 8+,使用 Arrays.asList
而不是 List.of
。
对于 Java 7+,使用:
List<List<String>> tempEdges = Arrays.asList(Arrays.asList("RoyalElephant", "IS-A", "Elephant"),
Arrays.asList("RoyalElephant", "IS-NOT-A", "Gray"));
List<List<String>> copiedPaths = new ArrayList<>();
for (List<String> list : tempEdges)
copiedPaths.add(list.subList(1, list.size()));
System.out.println(tempEdges);
System.out.println(copiedPaths);
输出
[[RoyalElephant, IS-A, Elephant], [RoyalElephant, IS-NOT-A, Gray]]
[[IS-A, Elephant], [IS-NOT-A, Gray]]
请注意,subList
创建了基础列表的视图。如果原始 tempEdges
列表可以更改,则需要创建一个副本,即 change
list.subList(1, list.size())
到
new ArrayList<>(list.subList(1, list.size()))
ArrayList<ArrayList<String>> copiedPaths = new ArrayList<>();
ArrayList<ArrayList<String>> tempEdges = new ArrayList<>();
tempEdges.add(new ArrayList<>(Arrays.asList("RoyalElephant", "IS-A", "Elephant")));
tempEdges.add(new ArrayList<>(Arrays.asList("RoyalElephant", "IS-NOT-A", "Gray")));
copiedPaths.add(new ArrayList<>(Arrays.asList(tempEdges.get(0).get(1),tempEdges.get(0).get(2))));
copiedPaths.add(new ArrayList<>(Arrays.asList(tempEdges.get(1).get(1),tempEdges.get(1).get(2))));
System.out.println(Arrays.toString(copiedPaths.get(0).toArray()));
System.out.println(Arrays.toString(copiedPaths.get(1).toArray()));
输出:-
[IS-A,大象]
[IS-NOT-A,灰色]
我正在尝试添加存储在 tempEdges 中的以下 2 个:
- [RoyalElephant, IS-A, Elephant]
- [RoyalElephant, IS-NOT-A, Gray]
尽管我只想将每个数组列表的最后 2 个元素添加到 copiedPaths 中的 2 个数组列表。
数组列表是:
public static List<ArrayList<String>> copiedPaths = new ArrayList<>();
public static List<ArrayList<String>> tempEdges = new ArrayList<>();
无效代码是:
copiedPaths.get(0).add(tempEdges.get(0).get(1));
copiedPaths.get(0).add(tempEdges.get(0).get(2));
copiedPaths.get(1).add(tempEdges.get(1).get(1));
copiedPaths.get(1).add(tempEdges.get(1).get(2));
这没有按预期工作,因为两个数组都添加了 IS-NOT-A,灰色而不是具有 IS-A[=26 的数组=]、大象和另一个 IS-NOT-A、灰色
Java 9+解法:
List<List<String>> tempEdges = List.of(List.of("RoyalElephant", "IS-A", "Elephant"),
List.of("RoyalElephant", "IS-NOT-A", "Gray"));
List<List<String>> copiedPaths = tempEdges.stream()
.map(list -> list.subList(1, list.size()))
.collect(Collectors.toList());
System.out.println(tempEdges);
System.out.println(copiedPaths);
对于 Java 8+,使用 Arrays.asList
而不是 List.of
。
对于 Java 7+,使用:
List<List<String>> tempEdges = Arrays.asList(Arrays.asList("RoyalElephant", "IS-A", "Elephant"),
Arrays.asList("RoyalElephant", "IS-NOT-A", "Gray"));
List<List<String>> copiedPaths = new ArrayList<>();
for (List<String> list : tempEdges)
copiedPaths.add(list.subList(1, list.size()));
System.out.println(tempEdges);
System.out.println(copiedPaths);
输出
[[RoyalElephant, IS-A, Elephant], [RoyalElephant, IS-NOT-A, Gray]]
[[IS-A, Elephant], [IS-NOT-A, Gray]]
请注意,subList
创建了基础列表的视图。如果原始 tempEdges
列表可以更改,则需要创建一个副本,即 change
list.subList(1, list.size())
到
new ArrayList<>(list.subList(1, list.size()))
ArrayList<ArrayList<String>> copiedPaths = new ArrayList<>();
ArrayList<ArrayList<String>> tempEdges = new ArrayList<>();
tempEdges.add(new ArrayList<>(Arrays.asList("RoyalElephant", "IS-A", "Elephant")));
tempEdges.add(new ArrayList<>(Arrays.asList("RoyalElephant", "IS-NOT-A", "Gray")));
copiedPaths.add(new ArrayList<>(Arrays.asList(tempEdges.get(0).get(1),tempEdges.get(0).get(2))));
copiedPaths.add(new ArrayList<>(Arrays.asList(tempEdges.get(1).get(1),tempEdges.get(1).get(2))));
System.out.println(Arrays.toString(copiedPaths.get(0).toArray()));
System.out.println(Arrays.toString(copiedPaths.get(1).toArray()));
输出:-
[IS-A,大象]
[IS-NOT-A,灰色]