0

I am trying to create a 2d ArrayList from a 2d array. Actually I ve got a temp ArrayListand I parse it to my 2d ArrayList in for loop. I am trying to find the correct way to clear this temp ArrayList in order to pass every time, the correct values to mags ArrayList.

   for (int m = 0; m < M; m++) {
        for (int n = 0; n < N; n++) {
            for (int i = 0; i < gaborWavelet.length; i++) {
                temp.clear();   
                for (int j = 0; j < gaborWavelet[0].length; j++) {
                    sum = gaborWavelet[i][j][m][n][0] / (M * N);
                    temp.add((int) (sum * 170));                
                }
                mags.add(temp);             
            }
        }
    }

In the above code only the final indexed-i it is passed in the temp m*n*i times.

EDIT:

        for (int m = 0; m < M; m++) {
        for (int n = 0; n < N; n++) {
            for (int i = 0; i < gaborWavelet.length; i++) {
                ArrayList<Integer> temp = new ArrayList<Integer>(); 
                for (int j = 0; j < gaborWavelet[0].length; j++) {
                    sum = gaborWavelet[i][j][m][n][0] / (M * N);
                    temp.add((int) (sum * 170));                
                }
                mags.add(temp);             
            }
        }
    }

    System.out.println(mags.get(200));
    System.out.println(mags.get(0));

If the above is what you mean, I am getting the same issue here.

snake plissken
  • 2,649
  • 10
  • 43
  • 64

1 Answers1

2

Instead of temp.clear() do temp = new ArrayList(). With your code in mags will be the same ArrayList added multiple times.

And read this post on how to create ArrayList from array

Community
  • 1
  • 1
1ac0
  • 2,875
  • 3
  • 33
  • 47