There are a couple of ways to make 3-D arrays.
I would avoid Object[][], I never like the handling or performance.
Since a 3-D array is just an array of arrays, an easy approach would to use the List data structure.
List[] or List> should do the trick. This way you have all the built-ins of a Collection object plus you can also Apache commons, lambdaJ or Guava on top of it.
If you are dead set on using primitive arrays then you could also make a regular 2-D array, [], that can act like a 3-D array, [][].
Here is simple wrapper method I made around a basic array that will function the same as 3-D array.
public class MyThreeDArray{
int MAX_ROW;
int MAX_COL;
int[] arr;
public MyThreeDArray(final int MAX_ROW, final int MAX_COL){
this.MAX_ROW = MAX_ROW;
this.MAX_COL = MAX_COL;
arr = new int[MAX_ROW * MAX_COL];
}
private int findIndex(int row, int col) throws IllegalArgumentException{
if(row < 0 && row >= MAX_ROW ){
throw new IllegalArgumentException("Invaild row value");
}
if(col < 0 && col >= MAX_COL ){
throw new IllegalArgumentException("Invaild col value");
}
return ( row * MAX_COL + col );
}
public int get(int row, int col){
return arr[findIndex(row, col)];
}
public void set(int row, int col, int value){
arr[findIndex(row, col)] = value;
}
}
Also keep in mind I never tested any of this.
So if you did this with Strings then row 0 might contain the first name values, ... and row 4 could have the unit values.
To retrieve person A's data with this wrapper could make a call similar to this:
String firstName = myWrapper.get(0,0);
String lastName = myWrapper.get(0,1);
String phone = myWrapper.get(0,2);
String unit = myWrapper.get(0,3);
And person B's data would be stored in the second row.
But why try to combine arrays together? You could easy make a POJO called person
public class Person{
String firstName;
String lastName;
String phone;
String unit;
public Person(){}
//ToDo: Getters and Setters
}
This way could just easily add validation and clearly call a specific person without any trouble.
Person[] customers = new Person[5];
or better yet
List<Person> customers = new ArrayList<Person>();
List
– Bromism