When I was taught 2d arrays, I was taught that they are arrays of arrays, similar to an array of ArrayLists. But I was thinking about it and it is actually really confusing:
ArrayList[] arrOfList = new ArrayList[5];
arrOfList[0] = new ArrayList<Integer>();
arrOfList[0].add(1);
arrOfList[0].add(2);
arrOfList[0].add(3);
//arrOfList[0] is the first list, with 3 elements
//arrOfList is an array of 5 lists; arrOfList.length = 5
int[][] arrOfArr = new int[3][5];
//arrOfArr[0] is NOT an array with 3 elements
//arrOfArr is NOT an array of 5 arrays; arrOfArr.length = 3
I think what I'm getting at is more clear if I bold the parts that I think would correlate (but don't):
ArrayList[] arrOfList = new ArrayList[5];
int[][] arrOfArr = new int[3][5];
What is a more intuitive way of thinking about declaring 2d arrays, since relating it to an arbitrary type is not right? (as illustrated above)
This may help you visualize it more.
When using square brackets [], first pair is always row, second pair is columns.
In both scenarios, the first dimension (row) saves the memory address of the corresponding Array / ArrayLists.
The second dimension (column) saves the corresponding value stored in that particular row of Array/ArrayList.
What you are taught is correct. An array of arrays is similar to an array of arraylists, with the exception that arraylists have variable length. The implication is that with an array of arraylists, the width (no. of columns) isn't static.
ArrayList[] arrOfList = new ArrayList[5]
This is saying, create an Array of length 5 (5 rows), with the data type being ArrayList (of variable length, thus can be any number of columns).
int[][] arrOfArr = new int[3][5]
This is saying, create an Array of length 3 (3 rows), with the data type being Array with length 5 (5 columns).
The fact that an ArrayList has variable length is likely causing the confusion, but I still stand by the same answer: first pair of [] = row; second pair of [] = column.
int[][] arrOfArr = new int[3][5]
That creates an empty grid of array cells with 3 rows and 5 columns. i.e.
[][][][][]
[][][][][]
[][][][][]
Then to fill those columns you just do arrOfArr[0][2] = 4 which goes down to row 0 and across to column 2. i.e.
[][][4][][]
[][][][][]
[][][][][]
Alternatively, see it as a set of 3 lots of 5 cell arrays
[][][4][][], [][][][][], [][][][][]
int[][] arrOfArr = new int[3][5];
//arrOfArr[3] is the number of rows
//arrOfArr[5] is the number of colums
Related
This question already has an answer here:
How to get rows and columns count of a 2D array in Java? [duplicate]
(1 answer)
Closed 4 years ago.
int[][] example = {{1,2,3},
{2,3,4},
{6,7,8}};
We can get the length of the column in a 2d array using example[0].length,
but how the length of the row is determine using example.length?
NOTE : I am asking for an explanation for Why example.length is used to get the length of row? not how to get the length of columns and row in 2d array? .
Think of it like this: for a 2-D array such as int[][] example, the number of rows is example.length, while the number of columns in a row, eg. example[0], is the length of that particular row, which can be expressed as example[0].length.
Note: the number of columns in each row can be different. For example, you could define example as:
int[][] example = { {1, 2}, // row of length 2
{2, 3, 4}, // row of length 3
{6} }; // row of length 1
Java technically doesn't have 2-dimensional arrays, it has arrays of arrays.
System.out.println(example.length); // number of rows
System.out.println(example[0].length); // number of columns in first row
But This could fail if:
array size is 0, in which you will get an exception.
You are explicitly assuming that size of first element of array as size of rows, this could be ambiguous if you are taking a Jagged array.
Jagged Array: Jagged array is array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D arrays but with variable number of columns in each row.
When I have a two-dimensional matrix in java, but I only want to work with a certain row of that matrix respectively creating a new array that contains the contents of the matrix in that certain row, how would I accomplish that (with primitives)?
So, for example, we would write:
int[][] matrix = new int[10][10];
Now we have a two-dimensional matrix with 10 rows and 10 columns. Assume that we fill the whole matrix with certain elements, and now, I wish to work only with the first row, meaning to define a new array that contains exactly the elements of the first row of the matrix.
Assuming that row means the horizontal segments of the matrix (as it almost always is):
In a 2 dimensional array in java, the rows are the first index and the columns are the second index.
Basically a two dimensional array is an array of arrays. So
int[][] intArray = new int[10][3];
is actually an array of size 10. Each element in the array is an array in itself of size 3.
Say you have an array of integers
int[][] integerArray; //we have to initialize the array.
then we want to work with the 1st row. We would use:
int[] arr = integerArray[0];
LIMITATIONS
The matrix must be initialized (must have values in cells)
The matrix must have a 1st row
Note: we use integerArray[0] because arrays start at index 0, so the third row would be integerArray[2]
I am preparing for OCAJP exam, I got a problem with the multi-dimensional arrays in java. After go through a video tutorial on YouTube, I think I got an idea about how it works. It says the following statement creates two double dimensional arrays and one array to hold both arrays. Hence it is a three dimensional array.
int arr[][][] = new int[2][4][3];
So I want to get confirmed, that if I want a five dimensional array, this statement would do it.
int arr[][][] = new int[4][4][3];
Try to visualise it geometrically.
A 1-dimensional array is just a list: new int[2]
A 2-dimensional array is a rectangular grid (or a list of lists): new int[2][3]
A 3-dimensional array is a cuboid (or a list of rectangles, or a list
of lists of lists): new int[2][3][4]
After this it gets harder, but :
a 4D array is a list of cuboids (a list
of lists of lists of lists) new int[2][3][4][5]
a 5D array is a grid of cuboids (a list
of lists of lists of lists of lists): new int[2][3][4][5][6]
int arr[][][] = new int[4][4][3];
Is still a 3 dimensional array.
A 5 dimensional array looks like
int arr[][][][][] = new int[4][4][3][4][3];
int arr[][][][][] = new int[4][4][3][X][X];
x can be any number. this is a 5 dimentional array.
Imagine a cube.
int arr[][][] = new int[2][4][3];
Here you have 2 slices of an array of 4x3.
int arr[][][] = new int[4][4][3];
With this, you have 4 slices of an array of 4x3.
So, it stills a three dimensional array.
However, you can save 4 different two dimensional arrays there.
every time you add a new dimension the number of elements grow exponentially. int[4][4][3] means a 3-dimensions array with 4*4*3=48 elements. to create a 5-dimension array add 2 more square-brackets int[2][2][2][2][2] which is an array with 2^5 elements(2*2*2*2*2)
public static int[][] add2DArrays(int[][]A, int[][]B) {
return int[][] C;
}
I understand how to populate this new array by adding the sums of the two arrays A & B, the road block I am facing is creating this initial array C. We cannot assume the size of the arrays given as parameters, so that is the part I am having a problem with.
I know by doing:
int[][] C = new int[A.length+B.length][]; //this will only give me the # of rows;
I am stuck on how to get the proper length of columns for the rows in the new array. I am thinking it may be possible to some how record the length of the longest row in A, and then record the longest row in B, and choose the bigger of the two, but that seems messy and inefficient. Thank you all for your support.
If both are really 2D arrays -- that is, the length of all the rows is the same -- and row 0 was actually allocated, you can get the summed length of a row from A[0].length+B[0].length
If the length of the rows might vary -- which is perfectly legal in Java -- then you might need to iterate through A and B to find the longest row and use that length.
I am trying to have a 2D Array that is 16 rows by 11 columns where row 0, column 3 is a reference to another array that is a single dimension array. How do i go about doing this? I already have both arrays where the single dimension array is a char array(although i could make it a string array if i wanted) and the 2D array is a string array. The rest of the 2D Array is filled with plain strings for each elements with the exception of row 0, column 3 which i want it to be the single dimension array.
Object[] arrayToReference = ...;
Object[][] arrayWithReference = new Object[] { ..., arrayToReference, ...};
This should work; just reference the array and it should change as the original changes
Here's an example code snippet:
Object[] array = new Object[] { "Test!" };
Object[][] arrayArray = new Object[][] { array };
System.out.println("Before: " + arrayArray[0][0]);
array[0] = "Test2!";
System.out.println("After: " + arrayArray[0][0]);
which has the following output:
Before: Test!
After: Test2!
That just sounds like your approach to the problem is wrong. Maybe you should rethink your data structure. Normally, an array is typed. You can't really decide to insert a different type in one cell.
If you really can't change the data structure, try inserting a single dimension array of strings in each cell with only 1 entry in them, which is the string that goes there normally, except for row 0, column 3, which already is an array.
I'm thinking a bit of an overkill solution, but should work for this with some refining. If it is possible for you.
Can you make two classes that implement the same interface? First one will be the type of the 2D array, and the other one would be class with the 1D array.
Since they implement a common interface, you can make a 2D array with that interface. That kind of 2D array can accept any kind of the two objects on any place in it.