0

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

1
  • I know, that is not the issue Commented Apr 15, 2020 at 15:57

2 Answers 2

1
    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]

Sign up to request clarification or add additional context in comments.

Comments

1

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, i.e. change

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

to

new ArrayList<>(list.subList(1, list.size()))

2 Comments

Thanks, why didn't using the .get() and .add() not work for me though?
@MrMosby Because you never created the inner lists in copiedPaths.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.