3

Should I use Parcelable? Because that does not seem to accept multi-dimensional arrays like for example int[][]

(I need to send data between activities)

1
  • Could you provide some more context? Otherwise you might get really few (useful) answers.
    – Thomas
    Dec 23, 2011 at 13:49

2 Answers 2

0

Arrays are covariant in Java (and they all extend Object). Therefore, you can assign an array of arrays (like int[][]) to an Object[] array, simply because an array is an object: int[][] means an array of int[] and int[] is an Object.

With generics, if you have a type T and an array T[], defining T as int[] would result in that array being exactly int[][].

Hope this helps.

1
  • 2
    I don't really get it. Could you provide a sample that shows how you transfer a multidimensional array from one activity to another? Could I even use a Bundle?
    – Aron
    Apr 12, 2012 at 17:02
0

I know that this is quite late to the game, but this is the solution I used:

private void write2DimArray(int[][] array, Parcel parcel) {
    parcel.writeInt(array.length);
    for(int[] arrayDim1 : array) {
        parcel.writeInt(arrayDim1.length);
        parcel.writeIntArray(arrayDim1);
    }
}

private int[][] read2DimArray(Parcel parcel) {
    int[][] array = new int[parcel.readInt()][];
    for(int i = 0; i < array.length; i++) {
        int arraySize = parcel.readInt();
        array[i] = new int[arraySize];
        parcel.readIntArray(array[i]);
    }
    return array;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.