1

I have this three-dimensional array named bands. I need to do 4 copies of it, so I can work in parallel with all of them.

int bands[][][] = new int[param][][];

I need the array to keep being a three dimensional array, as it is the input for some methods that needs an int [][][]

How could I do such copies? I was thinking about using an arrayList like this:

List<Integer[][][]> bandsList = new ArrayList<Integer[][][]>();

bandsList.add(bands);

but I get this error on the last line: The method add(Integer[][][]) in the type List<Integer[][][]> is not applicable for the arguments (int[][][])

so what should I do??

6
  • 2
    Use List<int[][][]> instead. But what you are doing is making me crazy. Why you actually want to do something like this? Is there any specific use case which forces you? Commented Feb 17, 2013 at 14:39
  • 2
    Storing an array in a list won't make any copy of the array. You'll just have 4 references to the same array. Commented Feb 17, 2013 at 14:41
  • O.o. Seems like I missed the major issue there. Commented Feb 17, 2013 at 14:42
  • Not the best idea to copy something in order to access it concurrently..its difficult to modify all the copy and merge them. Commented Feb 17, 2013 at 14:42
  • first... I didn't know List<Integer[][][]> was different from List<int[][][]>, so thanks... you can assume I am pretty lost So I had in mind that if I had such list I could then use: for (int i=0; i<4;i++){ method(Listbands.get(i)) } Commented Feb 17, 2013 at 14:52

2 Answers 2

1

The errors is because int[][][] is not the same as Integer[][][].

int[][][] is an 3D array of primitive int.

Integer[][][] is an 3D array of object Integer, which is the wrapper class of int.

Well, technically a 3D array is an array of pointers to a 2D array, which is an array of pointers to a 1D array which is an array of primitives or pointers to objects.

Use List<int[][][]> bandsList = new ArrayList<int[][][]>(); instead.

Also note that

bandsList.add(bands);
bandsList.add(bands);

will simply add 2 pointers to the same array, changing one will also change the other.

You'll need to manually copy them:

int[][][] getCopy(int[][][] bands)
{
  int[][][] newBands = new int[bands.length][][];
  for (int i = 0; i < bands.length; i++)
  {
    newBands[i] = new int[bands[i].length];
    for (int j = 0; j < bands[i].length; j++)
    {
      newBands[i][j] = new int[bands[i][j].length];
      System.arraycopy(bands, 0, newBands, 0, bands[i][j].length))
    }
  }
  return newBands;
}

// to add
bandsList.add(getCopy(bands));
Sign up to request clarification or add additional context in comments.

Comments

0

in this way you aren't doing a copy of the array, you have to do this 4 times:

    int cloneList[][][] = new int[param][..][..];
    for(int i = 0; i<bands.length; i++) {
       int arr1[][] = bands[i];
       for(int j = 0; j<arr1.length; j++) {
           int arr2[] = arr1[j];
           for(int k = 0; k<arr2.length; k++) {
               int x = arr2[k];
               cloneList[i][j][k] = x;
           }
       }
    }

Comments

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.