0

I am trying to solve Pascal's triangle. I have two snippets of code written in Java, first one creates inner ArrayList few times and works fine for me.

But in second version of the code, if I modify inner ArrayList, it also modifies the outer ArrayList and doesn't give me the expected result. Can anyone please explain why does it happen?

Also, using inner = new ArrayList<Integer>(); statement is not a good approach as I am using that few times in a short function. How can I get rid of that? Any explanation and suggestion would be really appreciated!

Way 1: works but not efficient!

  public static ArrayList<ArrayList<Integer>> generate(int a) {
        ArrayList<ArrayList<Integer>> outer = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> inner = new ArrayList<Integer>();

        if (a==0) return outer;

        inner.add(1);
        outer.add(inner);

        if(a==1) return outer;

        inner = new ArrayList<Integer>();
        inner.add(1);
        inner.add(1);
        outer.add(inner);

        inner = new ArrayList<Integer>();    
        if (a==2) return outer;


        for (int i=2; i<a; i++){
            inner = new ArrayList<Integer>();
            inner.add(1);
            for(int j=1; j<i; j++){
                inner.add(outer.get(i-1).get(j-1)+outer.get(i-1).get(j));
            }
            inner.add(1);
            outer.add(inner);          
        }

        return outer;          
  }

Way 2: does not work!

  public static ArrayList<ArrayList<Integer>> generate(int a) {
        ArrayList<ArrayList<Integer>> outer = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> inner = new ArrayList<Integer>();

        if (a==0) return outer;

        inner.add(1);
        outer.add(inner);
        inner.clear();

        if(a==1) return outer;

        inner.add(1);
        inner.add(1);
        outer.add(inner);
        inner.clear();
        if (a==2) return outer;

        for (int i=2; i<a; i++){
            inner.add(1);
            for(int j=1; j<i; j++){
                inner.add(outer.get(i-1).get(j-1)+outer.get(i-1).get(j));
            }
            inner.add(1);
            outer.add(inner);
            inner.clear();
        }

        return outer;    
  }
6
  • where you want to Modifying elements? Commented Nov 2, 2015 at 4:42
  • I am reusing inner ArrayList, did you get that? Commented Nov 2, 2015 at 4:43
  • @SMAlMamun Are you sure that you are reusing it? Look at your code more closely. Commented Nov 2, 2015 at 4:46
  • @NathanielJones when I am clearing it, I am not creating anything new in second version of code! Commented Nov 2, 2015 at 4:52
  • @SMAlMamun First, you are creating new ones in your for loop, and second, it is the clearing that causes the problem. Commented Nov 2, 2015 at 4:55

1 Answer 1

1

If you want add multiple ArrayList<Integer> objects to your outer ArrayList, you must call new ArrayList<Integer>() for each one. Your second version is not more efficient. In your second version, you clear every ArrayList<Integer> that you make, which means that all of your lists are empty.

Remember that this:

outer.add(inner);
inner.clear();

and this:

inner.clear();
outer.add(inner);

are equivalent to each other. Both sets of instructions will empty the inner ArrayList and add it to the outer ArrayList.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.