2

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) {

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

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 4 such double for-loops in my main function where i call method1 for different iterations. However, while the 1st for-loop gives the approppriate answer, the minute i declare an ArrayList inside my 2nd for loop, my inputTerns variable gets set to null. How do i overcome this?

2 Answers 2

1

I don't understand what you are trying to achieve here. But you can definitely create ArrayList of ArrayLists.

public class Testapp {

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

public static void main(String[] args) {
    int outerlist = 3;
    int innerlist = 5;
    for(int i=0; i<outerlist; i++)
    {
        ArrayList<Integer> list = new ArrayList<Integer>(); 
        for(int j=0; j<innerlist; j++)
        {
            list.add(j);
        }
        inputTerms.add(list);
    }

    Iterator outi = inputTerms.iterator();
    while(outi.hasNext()){
        ArrayList<Integer> temp = (ArrayList<Integer>)outi.next();
        Iterator ini = temp.iterator();
        while(ini.hasNext()){
            System.out.print(ini.next());
        }
        System.out.println();
    }
}
}

Output:

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

Comments

1

I recommend you use ArrayList<Integer[]> instead of another ArrayList. You might lose some stuff without ArrayList, but it's the only way.

2 Comments

sorry, could you elaborate a bit?
Sorry that was my bad i meant arrarylist<integer[]>

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.