user1646637
user1646637

Reputation: 33

ArrayList of ArrayLists - clear function confusion

Here is a particular method I have written:

class A {

    private static ArrayList<ArrayList<Integer>> inputTerms = new ArrayList<ArrayList<Integer>();

    public static void method1(ArrayList<Integer> terms) {
        ArrayList<Integer> clauses = new ArrayList<Integer>();
        int N = terms.size();
        for (int i = 0; i < N - 1; i++) {
            for (int j = i + 1; j < N; j++) {
                clauses.add(-terms.get(i));
                clauses.add(-terms.get(j));
                inputTerms.add(clauses);
                clauses.clear();
            }
        }
    }
}

This method is called multiple times from the main function. In the end, i try to write the contents of the class variable into a file. However, when I do this, i get 0 as the contents of inputTerms. However, if i remove the clauses.clear() line, i am able to get approppriate values.

My program is such that it is vital for me to clear the clauses after adding to inputTerms. Is there any alternative to this?

**Hmmm.. I have done what you've suggested. However, I haven't quite overcome the problem. To give more background, in my main function, I have the following code:

for (int i=0; i<N-1; i++){ 
ArrayList<Integer> firstdiagonalTerms = new ArrayList<Integer>(); 
for (int j=0; j<N-i; j++){ 
firstdiagonalTerms.add(variable[j][i+j]); 
} 
method1(firstdiagonalTerms);
} 

I have to call the method1 function 4 times for different combinations of 'i' and 'j'. However, I still get 0 when I use the above mentioned suggestions**

Upvotes: 1

Views: 146

Answers (4)

kosa
kosa

Reputation: 66677

When you call clear() on list, you are updating/removing same objects (because list contains reference to objects, not copy of object). That is what causing the issue.

I think you need to do something like below. Instead of using clear(), create a new list everytime.

public static void method1 (ArrayList<Integer> terms)
{

int N = terms.size();       
        for (int i = 0; i<N-1; i++) {
            for (int j=i+1; j<N; j++) {
              ArrayList<Integer> clauses = new ArrayList<Integer>();
              clauses.add(-terms.get(i));
              clauses.add(-terms.get(j));
              inputTerms.add(clauses);

}
}

Upvotes: 0

Domenic D.
Domenic D.

Reputation: 5366

When you are adding "clauses" you are adding the actual object to the arrayList, not a copy. So when you clear them all the values in the list will be removed. To get arround this, add a clone of the list:

inputTerms.add((ArrayList<Integer>) clauses.clone());

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533880

You are adding the same list and clearing it repeatedly. When you add an object to a list it copies a reference to it, not a copy of the object.

int N = terms.size();
for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
        List<Integer> clauses = new ArrayList<Integer>();
        clauses.add(-terms.get(i));
        clauses.add(-terms.get(j));
        inputTerms.add(clauses);
    }
}

or

for (int i = 0, N = terms.size(); i < N - 1; i++) 
    for (int j = i + 1; j < N; j++) 
        inputTerms.add(Arrays.asList(-terms.get(i), -terms.get(j)));

Upvotes: 2

assylias
assylias

Reputation: 328923

Not sure i understand what you are trying to achieve, but you keep reusing the same list, which is probably not what you meant to do.

You should probably move the ArrayList<Integer> clauses = new ArrayList<Integer>(); inside the inner loop, and not call clauses.clear() at all.

Upvotes: 0

Related Questions