I can\'t see to get a double boolean array to pass through to the another activity. I use putExtra and when I retrieve it and cast it to boolean[][]
, it states that
If you really require a 2-dimensional array, you can easily convert a 2-dimensional array into a single dimensional array for passing between Activities like so:
public boolean[] toOneDimension(boolean[][] input){
boolean[] output = new boolean[input.length * input[0].length];
for(int i = 0; i < input.length; i++){
for(int j = 0; j < input[i].length; j++){
output[i*j] = input[i][j];
}
}
return output;
}
which you can then build back into a 2-dimensional array like so:
public boolean[][] toTwoDimensions(int dimensions, boolean[] input){
boolean[][] output = new boolean[input.length / dimensions][dimensions];
for(int i = 0; i < input.length; i++){
output[i/dimensions][i % dimensions] = input[i];
}
return output;
}
then use like so:
public static void main(String[] args){
int size = 10;
Random rand = new Random();
Tester tester = new Tester(); //example code holder
boolean[][] value = new boolean[size+1][size];
for(int i = 0; i < size+1; i++){
for(int j = 0; j < size; j++){
value[i][j] = rand.nextBoolean();
}
}
boolean [][] output = tester.toTwoDimensions(size, tester.toOneDimension(value));
for(int i = 0; i < size+1; i++){
for(int j = 0; j < size; j++){
assert value[i][j] == output[i][j];
}
}
}
The only requirement is that you need to know the dimension of your array before you flattened it.
This is old, but helped me a lot, so I wanted to share my findings.
I used Parcelabler to make my Object Class Parcelable(Because everything I read, was written in martian to me), then I used @John Ericksen answer to implement it in my Object Class and some methods to make my life easier flattenMultiDimensionalArray
and restoreMultiDimensionalArray
and the final result.
For a 2 Dimensional Array
MultiDimensionalArray .java
public class MultiDimensionalArray implements Parcelable {
private int[][] multiDimensionalArray;
//Any other Variables, Objects
public MultiDimensionalArray() {
}
public MultiDimensionalArray(int[][] multiDimensionalArray) {
this.multiDimensionalArray = multiDimensionalArray;
//Any other Variables, Objects
}
public int[][] getMultiDimensionalArray() {
return multiDimensionalArray;
}
public void setMultiDimensionalArray(int[][] multiDimensionalArray) {
this.multiDimensionalArray = multiDimensionalArray;
}
protected MultiDimensionalArray(Parcel in) {
int rows = in.readInt();
int columns = in.readInt();
int[] transitionalArray = in.createIntArray();
multiDimensionalArray = restoreMultiDimensionalArray(transitionalArray, rows, columns);
//Any other Variables, Objects
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
int rows = multiDimensionalArray.length;
int columns = multiDimensionalArray[rows - 1].length;
int[] transitionalArray = flattenMultiDimensionalArray(multiDimensionalArray);
dest.writeInt(rows);
dest.writeInt(columns);
dest.writeIntArray(transitionalArray);
//Any other Variables, Objects
}
public static final Creator<MultiDimensionalArray> CREATOR = new Creator<MultiDimensionalArray>() {
@Override
public MultiDimensionalArray createFromParcel(Parcel in) {
return new MultiDimensionalArray(in);
}
@Override
public MultiDimensionalArray[] newArray(int size) {
return new MultiDimensionalArray[size];
}
};
private int[] flattenMultiDimensionalArray(int[][] sourceCard) {
int k = 0;
int[] targetCard = new int[sourceCard.length * sourceCard[sourceCard.length - 1].length];
for (int[] aSourceCard : sourceCard) {
for (int anASourceCard : aSourceCard) {
targetCard[k] = anASourceCard;
k++;
}
}
return targetCard;
}
private int[][] restoreMultiDimensionalArray(int[] sourceCard, int rows, int columns) {
int k = 0;
int[][] multiDimensionalArray = new int[rows][columns];
for (int i = 0; i < multiDimensionalArray.length; i++) {
for (int j = 0; j < multiDimensionalArray[multiDimensionalArray.length - 1].length; j++) {
multiDimensionalArray[i][j] = sourceCard[k];
k++;
}
}
return multiDimensionalArray;
}
}
If you absolutely need a boolean[][] (and can't do this with just a flat boolean[] array passed to Parcel.writeBooleanArray()), then the formal way to do this is to wrap it in a Parcelable class and do the marshalling/unmarshalling there.
I'll sketch out the code, though this is not tested so there are certainly to be some issues.
public class BooleanArrayArray implements Parcelable {
private final boolean[][] mArray;
public BooleanArrayArray(boolean[][] array) {
mArray = array;
}
private BooleanArrayArray(Parcelable in) {
boolean[][] array;
final int N = in.readInt();
array = new boolean[][N];
for (int i=0; i<N; i++) {
array[i] = in.createBooleanArray();
}
mArray = array;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
final int N = mArray.length;
out.writeInt(N);
for (int i=0; i<N; i++) {
out.writeBooleanArray(mArray[i]);
}
}
public static final Parcelable.Creator<BooleanArrayArray> CREATOR
= new Parcelable.Creator<BooleanArrayArray>() {
public BooleanArrayArraycreateFromParcel(Parcel in) {
return new BooleanArrayArray(in);
}
public BooleanArrayArray[] newArray(int size) {
return new BooleanArrayArray[size];
}
};
}