简体   繁体   中英

Adding to list of arraylist

I am trying to add the following 2 which are stored in tempEdges:

  1. [RoyalElephant, IS-A, Elephant]
  2. [RoyalElephant, IS-NOT-A, Gray]

although i only want the last 2 elements of each arraylist to be added to the 2 arraylists in copiedPaths.

The arraylists are:

            public static List<ArrayList<String>> copiedPaths = new ArrayList<>();
            public static List<ArrayList<String>> tempEdges = new ArrayList<>();

And the dis-functional code is:

            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));

This is not working as intended as both arrays are being added with IS-NOT-A , Gray instead of one having IS-A , Elephant and the other having IS-NOT-A , Gray

Java 9+ solution:

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);

For Java 8+, use Arrays.asList instead of List.of .

For Java 7+, use:

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);

Output

[[RoyalElephant, IS-A, Elephant], [RoyalElephant, IS-NOT-A, Gray]]
[[IS-A, Elephant], [IS-NOT-A, Gray]]

Note that subList creates a view of the underlying list. If the original tempEdges lists can change, you need to create a copy, ie change

list.subList(1, list.size())

to

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()));

Output:-

[IS-A, Elephant]

[IS-NOT-A, Gray]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM