-1

Currently newResult is getting values: [[0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1]]

I am trying to get [[0, 0], [1, 0], [0, 1], [1, 1]] Basically, as a base case, in result, I am putting [[0], [1]] and then I am trying to add elements to it, thru copying result in newResult twice and then appending 0 in first half and appending 1 in second half.

I am using clone() also, but not able to resolve issue

import java.util.ArrayList;

public class ArrayListNestedTest {
    public static void main(String[] args) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        ArrayList<ArrayList<Integer>> newResult = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> zero = new ArrayList<Integer>();
        zero.add(0);
        ArrayList<Integer> one = new ArrayList<Integer>();
        one.add(0);
        result.add(zero);
        result.add(one);
        ArrayList<ArrayList<Integer>> tmp1 = new ArrayList<ArrayList<Integer>>();
        ArrayList<ArrayList<Integer>> tmp2 = new ArrayList<ArrayList<Integer>>();
        tmp1 = (ArrayList<ArrayList<Integer>>) result.clone();
        tmp2 = (ArrayList<ArrayList<Integer>>) result.clone();
        newResult.addAll(tmp1);
        newResult.addAll(tmp2);
        newResult.get(0).add(0);
        newResult.get(1).add(0);
        newResult.get(2).add(1);
        newResult.get(3).add(1);
        System.out.println("Hi");
    }
}

Thanks

Neuron
  • 5,141
  • 5
  • 38
  • 59
BioLogic
  • 49
  • 7
  • 2
    Use the abstract type, and inferred type, when declaring a list, ie `List> result = new ArrayList<>();`. Never use `clone()`; use the copy constructor `List> tmp1 = new ArrayList<>(result);` to copy a list (note that this is a shallow copy). You really need to debug your code; see https://stackoverflow.com/questions/25385173 – Bohemian Sep 02 '21 at 03:35
  • I have not fully analyzed your code but do you realize the clone does not do a deep clone. So for the nested lists, the contained ones will be the same objects. You should just copy them. – WJS Sep 02 '21 at 03:36
  • 3
    I think you're going about this the hard way. Consider iterating from 0 to 3, converting that number to binary, then converting the binary digits to elements in a list, then collection those lists (you can do the entire task in 1 line). – Bohemian Sep 02 '21 at 03:39
  • @Bohemian : Yes, the alternative suggested seems interesting and more straightward. Will try it. – BioLogic Sep 02 '21 at 05:12
  • I believe that if you indent your code properly, more people will be interested in analysing it. Your IDE can do it for you. – Ole V.V. Sep 02 '21 at 05:32

1 Answers1

2

Please, describe more briefly your task. It looks like you make too many actions. For example: is it absolutly necessary to make a clone?

Thanks to @Holger comment. I have changed double curly braces for initializer new ArrayList<Integer>(){{add(0);}} to new ArrayList<>(Arrays.asList(0)) because it is strongly discouraged.

Simple solusion could be this one:

    List<List<Integer>> newResult = new ArrayList<>();
    newResult.add(new ArrayList<>(Arrays.asList(0)));
    newResult.add(new ArrayList<>(Arrays.asList(1)));
    newResult.add(new ArrayList<>(Arrays.asList(0)));
    newResult.add(new ArrayList<>(Arrays.asList(1)));

    newResult.get(0).add(0);
    newResult.get(1).add(0);
    newResult.get(2).add(1);
    newResult.get(3).add(1);

    System.out.println(newResult);
Denis Rodin
  • 319
  • 1
  • 7