Sharif Mamun
Sharif Mamun

Reputation: 3554

Modifying elements of an Arraylist and adding that to another ArrayList of ArrayList

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

Upvotes: 0

Views: 62

Answers (1)

Nathaniel Jones
Nathaniel Jones

Reputation: 1849

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.

Upvotes: 1

Related Questions