Java 添加到arraylist的列表中
我正在尝试添加存储在TempEdge中的以下2个:Java 添加到arraylist的列表中,java,list,arraylist,nested,add,Java,List,Arraylist,Nested,Add,我正在尝试添加存储在TempEdge中的以下2个: [皇家大象,是一头大象] [皇家大象,不是,灰色] 尽管我只希望将每个arraylist的最后2个元素添加到CopiedPath中的2个arraylist中 ArrayList是: public static List<ArrayList<String>> copiedPaths = new ArrayList<>(); public static List&l
public static List<ArrayList<String>> copiedPaths = new ArrayList<>();
public static List<ArrayList<String>> tempEdges = new ArrayList<>();
这不符合预期,因为两个阵列都添加了is-not-A,灰色,而不是一个具有is-A,大象和另一个具有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);
输出
[[RoyalElephant,IS-A,Elephant],[RoyalElephant,IS-NOT-A,Gray]]
[IS-A,大象][IS-NOT-A,灰色]]
请注意,子列表
创建基础列表的视图。如果原始的tempEdges
列表可以更改,则需要创建副本,即更改
list.subList(1, list.size())
到
newarraylist(list.subList(1,list.size()))
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);
输出
[[RoyalElephant,IS-A,Elephant],[RoyalElephant,IS-NOT-A,Gray]]
[IS-A,大象][IS-NOT-A,灰色]]
请注意,子列表
创建基础列表的视图。如果原始的tempEdges
列表可以更改,则需要创建副本,即更改
list.subList(1, list.size())
到
newarraylist(list.subList(1,list.size()))
ArrayList CopiedPath=new ArrayList();
ArrayList tempEdge=新的ArrayList();
add(新的ArrayList(Arrays.asList(“RoyalElephant”、“IS-A”、“Elephant”));
add(新的ArrayList(Arrays.asList(“RoyalElephant”、“IS-NOT-A”、“Gray”));
添加(新的ArrayList(Arrays.asList(tempredges.get(0).get(1),tempredges.get(0).get(2)));
添加(新的ArrayList(Arrays.asList(tempredges.get(1).get(1),tempredges.get(1).get(2)));
System.out.println(Arrays.toString(copiedPaths.get(0.toArray());
System.out.println(Arrays.toString(copiedPaths.get(1.toArray());
输出:- [是一头大象] [IS-NOT-A,灰色]
ArrayList copiedPath=new ArrayList();
ArrayList tempEdge=新的ArrayList();
add(新的ArrayList(Arrays.asList(“RoyalElephant”、“IS-A”、“Elephant”));
add(新的ArrayList(Arrays.asList(“RoyalElephant”、“IS-NOT-A”、“Gray”));
添加(新的ArrayList(Arrays.asList(tempredges.get(0).get(1),tempredges.get(0).get(2)));
添加(新的ArrayList(Arrays.asList(tempredges.get(1).get(1),tempredges.get(1).get(2)));
System.out.println(Arrays.toString(copiedPaths.get(0.toArray());
System.out.println(Arrays.toString(copiedPaths.get(1.toArray());
输出:- [是一头大象]
[IS-NOT-A,Gray]我知道,那不是问题我知道,那不是问题答案,为什么不使用.get()和.add()对我不起作用?@MrMosby,因为你从未在
复制路径中创建过内部列表。
。谢谢,为什么不使用.get()和.add()但对我不起作用?@MrMosby,因为你从未在copiedpath
中创建过内部列表。
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()));