2d Array length rows and column length (Java) [duplicate] - java

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.

Related

How are 2d arrays structured?

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

Java 2D Array rows vs cols syntax question

I am having a hard time understanding why putting brackets versus leaving them out either calls for the rows or cols in a 2D array. I get the syntax I just don't understand why it works that way? Should I just not worry about it and move on with my life?
double[][] values = {
{1.2, 9.0, 3.2},
{8.2, 8.6, -1.2},
{-7.3, 2.5, 9.7},
{4.1, 7.0, 5.1},
};
System.out.println("Number of Rows: " + values.length);
System.out.println("Number of Cols: " + values[0].length);
a 2D array is an array of arrays. The variable length returns the number of elements in the array.
so values.length gives the number of elements in the outer array which are the inner arrays (rows)
and values[0].length gives the number of elements in the first inner array (columns)
values.length gives you the length of the outer array. That is the array containing 4 rows. The outer array happens to contain objects that are themselves arrays.
values[0].length gives you the length of the 1st (0th) object in the outer array. This 1st object is itself an array. Since your 2D array is rectangular, every row has the same number of elements, or columns. So getting the length of any one of them will give you the number of columns
Hope that's clearer!

Why can I not get a one dimensional array of a column as easy as a row?

Background
This is a 2d array.-
String[][] names = { {"Sam", "Smith"}, {"Robert", "Delgro"}, {"James", "Gosling"}, };
I can access the rows by calling this:
names[n]
Where n is any integer between the bounds and in this case 0<=n<=names.length-1.
Likewise
I can call this
String [] rowOne = names[0];
Inquiry
The question now remains,instead of rows, is there a way to access a specific column in the same way a specific row is accessed.
Feedback
I would like feedback on this suggestion.
In the particular case of accessing a column in a 2d array,names[][x]would be the declaration to call a specific column,where 0<=x<=names[0].length-1.
String[] columnOne = names[][0]
This would mean that names.length would be equivalent to calling names[][x].length
This could also work for arrays greater than a 2d array.
String[][][]names = { {{"Sam", "Smith"}, {"Robert", "Delgro"}}, {{"James", "Gosling"}}};
Just add extra square brackets.
For example In a 3d array, to access an aisle, which is what I will refer to the 3rd dimension.
names[][][z], where 0<=z<= names[0][1].length-1
One dimensional arrays from multi-dimensional arrays.
...is there a way to access a specific column in the same way a specific row is accessed.
No; Java's multidimensional arrays are essentially arrays that contain references to other arrays are not contiguous in memory. Java also allows arrays to be jagged. Let's say you have the following multidimensional array:
int[][] array = {
{1, 2, 3},
{4, 5},
{6}
};
By calling array[][2], what would you expect to receive? What about array[][3]?
To be able to get an array of columns, you would need to first transpose the multidimensional array.

How to grab only a certain row of a two-dimensional matrix in java?

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]

Creating a new 2D array that = size of 2D array 1 + size of 2d array 2 (rows and columns)

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.

Categories