What does a method with (int[] []) mean?
up vote
2
down vote
favorite
We are given some code snippets to look at and figure out what the code does/will do.
I understand methods and methods with arrays but I have never seen methodName(int m)
with two
What does this mean? an array within an array?
java
add a comment |
up vote
2
down vote
favorite
We are given some code snippets to look at and figure out what the code does/will do.
I understand methods and methods with arrays but I have never seen methodName(int m)
with two
What does this mean? an array within an array?
java
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
We are given some code snippets to look at and figure out what the code does/will do.
I understand methods and methods with arrays but I have never seen methodName(int m)
with two
What does this mean? an array within an array?
java
We are given some code snippets to look at and figure out what the code does/will do.
I understand methods and methods with arrays but I have never seen methodName(int m)
with two
What does this mean? an array within an array?
java
java
edited Nov 19 at 17:31
Sotirios Delimanolis
206k39468559
206k39468559
asked Jun 17 '15 at 1:33
James
3516
3516
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
up vote
10
down vote
accepted
int
in the method signature refers to a double array of integers. You can think of a double integer array as being a matrix of int
values.
Taking your example 2D array:
int in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};
This array has the following properties:
System.out.println(in.length); // prints 3 (number of arrays inside 'in')
System.out.println(in[0].length); // prints 3 (number of ints in first array)
System.out.println(in[1].length); // also prints 3 (number of ints in second array)
Here is a visual to show you how accessing this array works:
int a = 1;
int b = 0;
Then in[a][b] == in[1][0] == 3
:
2 0 2
{3 1 2} <-- a = 1 (second subarray)
1 8 4
{3 1 2}
^-- b = 0 (first element in that subarray)
The first index a
chooses the subarray, and the index b
chooses the element inside the subarray.
so if the array that the method is using is 'int in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};' what would say the length be? 3, 3, 3? or 9?
– James
Jun 17 '15 at 1:36
@James "what would say the length be? 3, 3, 3? or 9?" - 3 {{3}, {3}, {3}},in.length
is 3,in[0].length
is 3,in[1].length
is 3,in[2].length
is 3
– MadProgrammer
Jun 17 '15 at 1:38
1
@TimBiegeleisen Nope, my testing showsint in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};
in.length
as3
, as the first level contains 3 elements...
– MadProgrammer
Jun 17 '15 at 1:46
@MadProgrammer I stand corrected. I didn't test every use case.
– Tim Biegeleisen
Jun 17 '15 at 1:47
@TimBiegeleisen Is cool, freaked me out for a minute :P
– MadProgrammer
Jun 17 '15 at 1:47
add a comment |
up vote
2
down vote
It represents multi dimensional arrays (AKA arrays or arrays) of given data type.
Think hierarchical to understand it the best way.
If you have int[3][2], it means,
It holds value for each of the following index.
int[0][0]
int[0][1]
int[1][0]
int[1][1]
int[2][0]
int[2][1]
Hope it will help. I struggled a lot to understand it when i was a beginner.
Possible assign is
int[3][2] iValue = {{00,01}, {10,11}, {20, 21}}
Thanks for the correction.
2
Java indices are 0-indexed, so the possible indices are:{[0][0], [0][1], [1][0], ..., [2][1]}
.
– Obicere
Jun 17 '15 at 1:48
Thanks for the connection Obicere.
– VD007
Jun 17 '15 at 1:52
add a comment |
up vote
1
down vote
methodName(int )
is an array of arrays. In response to all the comments, I tested it in eclipse and the length is 3.
add a comment |
up vote
0
down vote
In many programming languages (including Java), it is possible to create (and use) an array of arrays. In Java (specifically), all arrays are Object
instances. Consider
Object intArray1 = new int[10];
Object intArray2 = new int[10];
Object intArray3 = new int[10];
Then you might have
Object arrs = { intArray1, intArray2, intArray3 };
or even
Object arrs = new Object { intArray1, intArray2, intArray3 };
JLS-15.10.1 Run-Time Evaluation of Array Creation Expressions says (in part)
Otherwise, if
n DimExpr
expressions appear, then array creation effectively executes a set of nested loops of depthn-1
to create the implied arrays of arrays.
A multidimensional array need not have arrays of the same length at each level.
Finally, there is Arrays.deepToString(Object)
the Javadoc says (in part)
Returns a string representation of the "deep contents" of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings.
add a comment |
up vote
0
down vote
In Java, "int [ ][ ]" stands for a 2-dimensional integer array. To make it easy to understand, simply we can compare 2-d integer array with a simple 1-d integer array;
1) Down below, a 1-d int array is initialized;
int arr1d = { 1,2,3 };
2) And on this one, a 2-d int array is initialized;
int arr2d = { {1,2,3}, {4,5,6} };
It is important to understand the structure of 2d arrays. If you print the length of the arr2d , you will get 2 the rows of the array which is 2.
System.out.println(arr2d.length);
You will get the length of the outer array, which is actually row count of the array.
To get the inner array length, which is actually the column count;
System.out.println(arr2d[0].length);
Notice that we take the first row, and get the length of the inner array and print the column number.
To get familiar with the usage of the 2d array in a method, you can check this out;
private static void printIntegerArray(int intArray) {
for(int i = 0; i < intArray.length; i++ ) {
for(int j = 0; j < intArray[i].length; j++ ) {
System.out.printf("%3d ", intArray[i][j]);
}
System.out.println();
}
}
In this static method, int intArray is the only parameter which is obviously a 2 dimensional int array. There are two nested for loops to print the array as a matrix. The outer loop is traversing the rows and the inner loop is traversing on the inner loop.
Here is the complete example for the 2D Method usage;
public class Test2DArray {
public static void main(String args) {
//Init 2d integer list
int simpleArray = { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15} };
//Length of outer array which is actually Row Count;
System.out.println("Rows : " + simpleArray.length);
//Length of inner array which is actually Column Count;
//Notice that we take the first Row to get the Column length
System.out.println("Columns: " + simpleArray[0].length);
//Call the printIntegerList method with int parameter
printIntegerArray(simpleArray);
}
private static void printIntegerArray(int intArray) {
for(int i = 0; i < intArray.length; i++ ) {
for(int j = 0; j < intArray[i].length; j++ ) {
System.out.printf("%3d ", intArray[i][j]);
}
System.out.println();
}
}
}
And the output to the console is as below;
Rows : 3
Columns: 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
accepted
int
in the method signature refers to a double array of integers. You can think of a double integer array as being a matrix of int
values.
Taking your example 2D array:
int in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};
This array has the following properties:
System.out.println(in.length); // prints 3 (number of arrays inside 'in')
System.out.println(in[0].length); // prints 3 (number of ints in first array)
System.out.println(in[1].length); // also prints 3 (number of ints in second array)
Here is a visual to show you how accessing this array works:
int a = 1;
int b = 0;
Then in[a][b] == in[1][0] == 3
:
2 0 2
{3 1 2} <-- a = 1 (second subarray)
1 8 4
{3 1 2}
^-- b = 0 (first element in that subarray)
The first index a
chooses the subarray, and the index b
chooses the element inside the subarray.
so if the array that the method is using is 'int in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};' what would say the length be? 3, 3, 3? or 9?
– James
Jun 17 '15 at 1:36
@James "what would say the length be? 3, 3, 3? or 9?" - 3 {{3}, {3}, {3}},in.length
is 3,in[0].length
is 3,in[1].length
is 3,in[2].length
is 3
– MadProgrammer
Jun 17 '15 at 1:38
1
@TimBiegeleisen Nope, my testing showsint in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};
in.length
as3
, as the first level contains 3 elements...
– MadProgrammer
Jun 17 '15 at 1:46
@MadProgrammer I stand corrected. I didn't test every use case.
– Tim Biegeleisen
Jun 17 '15 at 1:47
@TimBiegeleisen Is cool, freaked me out for a minute :P
– MadProgrammer
Jun 17 '15 at 1:47
add a comment |
up vote
10
down vote
accepted
int
in the method signature refers to a double array of integers. You can think of a double integer array as being a matrix of int
values.
Taking your example 2D array:
int in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};
This array has the following properties:
System.out.println(in.length); // prints 3 (number of arrays inside 'in')
System.out.println(in[0].length); // prints 3 (number of ints in first array)
System.out.println(in[1].length); // also prints 3 (number of ints in second array)
Here is a visual to show you how accessing this array works:
int a = 1;
int b = 0;
Then in[a][b] == in[1][0] == 3
:
2 0 2
{3 1 2} <-- a = 1 (second subarray)
1 8 4
{3 1 2}
^-- b = 0 (first element in that subarray)
The first index a
chooses the subarray, and the index b
chooses the element inside the subarray.
so if the array that the method is using is 'int in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};' what would say the length be? 3, 3, 3? or 9?
– James
Jun 17 '15 at 1:36
@James "what would say the length be? 3, 3, 3? or 9?" - 3 {{3}, {3}, {3}},in.length
is 3,in[0].length
is 3,in[1].length
is 3,in[2].length
is 3
– MadProgrammer
Jun 17 '15 at 1:38
1
@TimBiegeleisen Nope, my testing showsint in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};
in.length
as3
, as the first level contains 3 elements...
– MadProgrammer
Jun 17 '15 at 1:46
@MadProgrammer I stand corrected. I didn't test every use case.
– Tim Biegeleisen
Jun 17 '15 at 1:47
@TimBiegeleisen Is cool, freaked me out for a minute :P
– MadProgrammer
Jun 17 '15 at 1:47
add a comment |
up vote
10
down vote
accepted
up vote
10
down vote
accepted
int
in the method signature refers to a double array of integers. You can think of a double integer array as being a matrix of int
values.
Taking your example 2D array:
int in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};
This array has the following properties:
System.out.println(in.length); // prints 3 (number of arrays inside 'in')
System.out.println(in[0].length); // prints 3 (number of ints in first array)
System.out.println(in[1].length); // also prints 3 (number of ints in second array)
Here is a visual to show you how accessing this array works:
int a = 1;
int b = 0;
Then in[a][b] == in[1][0] == 3
:
2 0 2
{3 1 2} <-- a = 1 (second subarray)
1 8 4
{3 1 2}
^-- b = 0 (first element in that subarray)
The first index a
chooses the subarray, and the index b
chooses the element inside the subarray.
int
in the method signature refers to a double array of integers. You can think of a double integer array as being a matrix of int
values.
Taking your example 2D array:
int in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};
This array has the following properties:
System.out.println(in.length); // prints 3 (number of arrays inside 'in')
System.out.println(in[0].length); // prints 3 (number of ints in first array)
System.out.println(in[1].length); // also prints 3 (number of ints in second array)
Here is a visual to show you how accessing this array works:
int a = 1;
int b = 0;
Then in[a][b] == in[1][0] == 3
:
2 0 2
{3 1 2} <-- a = 1 (second subarray)
1 8 4
{3 1 2}
^-- b = 0 (first element in that subarray)
The first index a
chooses the subarray, and the index b
chooses the element inside the subarray.
edited Jun 17 '15 at 2:10
answered Jun 17 '15 at 1:35
Tim Biegeleisen
212k1384132
212k1384132
so if the array that the method is using is 'int in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};' what would say the length be? 3, 3, 3? or 9?
– James
Jun 17 '15 at 1:36
@James "what would say the length be? 3, 3, 3? or 9?" - 3 {{3}, {3}, {3}},in.length
is 3,in[0].length
is 3,in[1].length
is 3,in[2].length
is 3
– MadProgrammer
Jun 17 '15 at 1:38
1
@TimBiegeleisen Nope, my testing showsint in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};
in.length
as3
, as the first level contains 3 elements...
– MadProgrammer
Jun 17 '15 at 1:46
@MadProgrammer I stand corrected. I didn't test every use case.
– Tim Biegeleisen
Jun 17 '15 at 1:47
@TimBiegeleisen Is cool, freaked me out for a minute :P
– MadProgrammer
Jun 17 '15 at 1:47
add a comment |
so if the array that the method is using is 'int in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};' what would say the length be? 3, 3, 3? or 9?
– James
Jun 17 '15 at 1:36
@James "what would say the length be? 3, 3, 3? or 9?" - 3 {{3}, {3}, {3}},in.length
is 3,in[0].length
is 3,in[1].length
is 3,in[2].length
is 3
– MadProgrammer
Jun 17 '15 at 1:38
1
@TimBiegeleisen Nope, my testing showsint in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};
in.length
as3
, as the first level contains 3 elements...
– MadProgrammer
Jun 17 '15 at 1:46
@MadProgrammer I stand corrected. I didn't test every use case.
– Tim Biegeleisen
Jun 17 '15 at 1:47
@TimBiegeleisen Is cool, freaked me out for a minute :P
– MadProgrammer
Jun 17 '15 at 1:47
so if the array that the method is using is 'int in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};' what would say the length be? 3, 3, 3? or 9?
– James
Jun 17 '15 at 1:36
so if the array that the method is using is 'int in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};' what would say the length be? 3, 3, 3? or 9?
– James
Jun 17 '15 at 1:36
@James "what would say the length be? 3, 3, 3? or 9?" - 3 {{3}, {3}, {3}},
in.length
is 3, in[0].length
is 3, in[1].length
is 3, in[2].length
is 3– MadProgrammer
Jun 17 '15 at 1:38
@James "what would say the length be? 3, 3, 3? or 9?" - 3 {{3}, {3}, {3}},
in.length
is 3, in[0].length
is 3, in[1].length
is 3, in[2].length
is 3– MadProgrammer
Jun 17 '15 at 1:38
1
1
@TimBiegeleisen Nope, my testing shows
int in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};
in.length
as 3
, as the first level contains 3 elements...– MadProgrammer
Jun 17 '15 at 1:46
@TimBiegeleisen Nope, my testing shows
int in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};
in.length
as 3
, as the first level contains 3 elements...– MadProgrammer
Jun 17 '15 at 1:46
@MadProgrammer I stand corrected. I didn't test every use case.
– Tim Biegeleisen
Jun 17 '15 at 1:47
@MadProgrammer I stand corrected. I didn't test every use case.
– Tim Biegeleisen
Jun 17 '15 at 1:47
@TimBiegeleisen Is cool, freaked me out for a minute :P
– MadProgrammer
Jun 17 '15 at 1:47
@TimBiegeleisen Is cool, freaked me out for a minute :P
– MadProgrammer
Jun 17 '15 at 1:47
add a comment |
up vote
2
down vote
It represents multi dimensional arrays (AKA arrays or arrays) of given data type.
Think hierarchical to understand it the best way.
If you have int[3][2], it means,
It holds value for each of the following index.
int[0][0]
int[0][1]
int[1][0]
int[1][1]
int[2][0]
int[2][1]
Hope it will help. I struggled a lot to understand it when i was a beginner.
Possible assign is
int[3][2] iValue = {{00,01}, {10,11}, {20, 21}}
Thanks for the correction.
2
Java indices are 0-indexed, so the possible indices are:{[0][0], [0][1], [1][0], ..., [2][1]}
.
– Obicere
Jun 17 '15 at 1:48
Thanks for the connection Obicere.
– VD007
Jun 17 '15 at 1:52
add a comment |
up vote
2
down vote
It represents multi dimensional arrays (AKA arrays or arrays) of given data type.
Think hierarchical to understand it the best way.
If you have int[3][2], it means,
It holds value for each of the following index.
int[0][0]
int[0][1]
int[1][0]
int[1][1]
int[2][0]
int[2][1]
Hope it will help. I struggled a lot to understand it when i was a beginner.
Possible assign is
int[3][2] iValue = {{00,01}, {10,11}, {20, 21}}
Thanks for the correction.
2
Java indices are 0-indexed, so the possible indices are:{[0][0], [0][1], [1][0], ..., [2][1]}
.
– Obicere
Jun 17 '15 at 1:48
Thanks for the connection Obicere.
– VD007
Jun 17 '15 at 1:52
add a comment |
up vote
2
down vote
up vote
2
down vote
It represents multi dimensional arrays (AKA arrays or arrays) of given data type.
Think hierarchical to understand it the best way.
If you have int[3][2], it means,
It holds value for each of the following index.
int[0][0]
int[0][1]
int[1][0]
int[1][1]
int[2][0]
int[2][1]
Hope it will help. I struggled a lot to understand it when i was a beginner.
Possible assign is
int[3][2] iValue = {{00,01}, {10,11}, {20, 21}}
Thanks for the correction.
It represents multi dimensional arrays (AKA arrays or arrays) of given data type.
Think hierarchical to understand it the best way.
If you have int[3][2], it means,
It holds value for each of the following index.
int[0][0]
int[0][1]
int[1][0]
int[1][1]
int[2][0]
int[2][1]
Hope it will help. I struggled a lot to understand it when i was a beginner.
Possible assign is
int[3][2] iValue = {{00,01}, {10,11}, {20, 21}}
Thanks for the correction.
edited Jun 24 '15 at 18:06
Brian Tompsett - 汤莱恩
4,153133699
4,153133699
answered Jun 17 '15 at 1:39
VD007
199113
199113
2
Java indices are 0-indexed, so the possible indices are:{[0][0], [0][1], [1][0], ..., [2][1]}
.
– Obicere
Jun 17 '15 at 1:48
Thanks for the connection Obicere.
– VD007
Jun 17 '15 at 1:52
add a comment |
2
Java indices are 0-indexed, so the possible indices are:{[0][0], [0][1], [1][0], ..., [2][1]}
.
– Obicere
Jun 17 '15 at 1:48
Thanks for the connection Obicere.
– VD007
Jun 17 '15 at 1:52
2
2
Java indices are 0-indexed, so the possible indices are:
{[0][0], [0][1], [1][0], ..., [2][1]}
.– Obicere
Jun 17 '15 at 1:48
Java indices are 0-indexed, so the possible indices are:
{[0][0], [0][1], [1][0], ..., [2][1]}
.– Obicere
Jun 17 '15 at 1:48
Thanks for the connection Obicere.
– VD007
Jun 17 '15 at 1:52
Thanks for the connection Obicere.
– VD007
Jun 17 '15 at 1:52
add a comment |
up vote
1
down vote
methodName(int )
is an array of arrays. In response to all the comments, I tested it in eclipse and the length is 3.
add a comment |
up vote
1
down vote
methodName(int )
is an array of arrays. In response to all the comments, I tested it in eclipse and the length is 3.
add a comment |
up vote
1
down vote
up vote
1
down vote
methodName(int )
is an array of arrays. In response to all the comments, I tested it in eclipse and the length is 3.
methodName(int )
is an array of arrays. In response to all the comments, I tested it in eclipse and the length is 3.
answered Jun 17 '15 at 1:42
user4920910
add a comment |
add a comment |
up vote
0
down vote
In many programming languages (including Java), it is possible to create (and use) an array of arrays. In Java (specifically), all arrays are Object
instances. Consider
Object intArray1 = new int[10];
Object intArray2 = new int[10];
Object intArray3 = new int[10];
Then you might have
Object arrs = { intArray1, intArray2, intArray3 };
or even
Object arrs = new Object { intArray1, intArray2, intArray3 };
JLS-15.10.1 Run-Time Evaluation of Array Creation Expressions says (in part)
Otherwise, if
n DimExpr
expressions appear, then array creation effectively executes a set of nested loops of depthn-1
to create the implied arrays of arrays.
A multidimensional array need not have arrays of the same length at each level.
Finally, there is Arrays.deepToString(Object)
the Javadoc says (in part)
Returns a string representation of the "deep contents" of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings.
add a comment |
up vote
0
down vote
In many programming languages (including Java), it is possible to create (and use) an array of arrays. In Java (specifically), all arrays are Object
instances. Consider
Object intArray1 = new int[10];
Object intArray2 = new int[10];
Object intArray3 = new int[10];
Then you might have
Object arrs = { intArray1, intArray2, intArray3 };
or even
Object arrs = new Object { intArray1, intArray2, intArray3 };
JLS-15.10.1 Run-Time Evaluation of Array Creation Expressions says (in part)
Otherwise, if
n DimExpr
expressions appear, then array creation effectively executes a set of nested loops of depthn-1
to create the implied arrays of arrays.
A multidimensional array need not have arrays of the same length at each level.
Finally, there is Arrays.deepToString(Object)
the Javadoc says (in part)
Returns a string representation of the "deep contents" of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings.
add a comment |
up vote
0
down vote
up vote
0
down vote
In many programming languages (including Java), it is possible to create (and use) an array of arrays. In Java (specifically), all arrays are Object
instances. Consider
Object intArray1 = new int[10];
Object intArray2 = new int[10];
Object intArray3 = new int[10];
Then you might have
Object arrs = { intArray1, intArray2, intArray3 };
or even
Object arrs = new Object { intArray1, intArray2, intArray3 };
JLS-15.10.1 Run-Time Evaluation of Array Creation Expressions says (in part)
Otherwise, if
n DimExpr
expressions appear, then array creation effectively executes a set of nested loops of depthn-1
to create the implied arrays of arrays.
A multidimensional array need not have arrays of the same length at each level.
Finally, there is Arrays.deepToString(Object)
the Javadoc says (in part)
Returns a string representation of the "deep contents" of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings.
In many programming languages (including Java), it is possible to create (and use) an array of arrays. In Java (specifically), all arrays are Object
instances. Consider
Object intArray1 = new int[10];
Object intArray2 = new int[10];
Object intArray3 = new int[10];
Then you might have
Object arrs = { intArray1, intArray2, intArray3 };
or even
Object arrs = new Object { intArray1, intArray2, intArray3 };
JLS-15.10.1 Run-Time Evaluation of Array Creation Expressions says (in part)
Otherwise, if
n DimExpr
expressions appear, then array creation effectively executes a set of nested loops of depthn-1
to create the implied arrays of arrays.
A multidimensional array need not have arrays of the same length at each level.
Finally, there is Arrays.deepToString(Object)
the Javadoc says (in part)
Returns a string representation of the "deep contents" of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings.
answered Jun 17 '15 at 1:49
Elliott Frisch
151k1389175
151k1389175
add a comment |
add a comment |
up vote
0
down vote
In Java, "int [ ][ ]" stands for a 2-dimensional integer array. To make it easy to understand, simply we can compare 2-d integer array with a simple 1-d integer array;
1) Down below, a 1-d int array is initialized;
int arr1d = { 1,2,3 };
2) And on this one, a 2-d int array is initialized;
int arr2d = { {1,2,3}, {4,5,6} };
It is important to understand the structure of 2d arrays. If you print the length of the arr2d , you will get 2 the rows of the array which is 2.
System.out.println(arr2d.length);
You will get the length of the outer array, which is actually row count of the array.
To get the inner array length, which is actually the column count;
System.out.println(arr2d[0].length);
Notice that we take the first row, and get the length of the inner array and print the column number.
To get familiar with the usage of the 2d array in a method, you can check this out;
private static void printIntegerArray(int intArray) {
for(int i = 0; i < intArray.length; i++ ) {
for(int j = 0; j < intArray[i].length; j++ ) {
System.out.printf("%3d ", intArray[i][j]);
}
System.out.println();
}
}
In this static method, int intArray is the only parameter which is obviously a 2 dimensional int array. There are two nested for loops to print the array as a matrix. The outer loop is traversing the rows and the inner loop is traversing on the inner loop.
Here is the complete example for the 2D Method usage;
public class Test2DArray {
public static void main(String args) {
//Init 2d integer list
int simpleArray = { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15} };
//Length of outer array which is actually Row Count;
System.out.println("Rows : " + simpleArray.length);
//Length of inner array which is actually Column Count;
//Notice that we take the first Row to get the Column length
System.out.println("Columns: " + simpleArray[0].length);
//Call the printIntegerList method with int parameter
printIntegerArray(simpleArray);
}
private static void printIntegerArray(int intArray) {
for(int i = 0; i < intArray.length; i++ ) {
for(int j = 0; j < intArray[i].length; j++ ) {
System.out.printf("%3d ", intArray[i][j]);
}
System.out.println();
}
}
}
And the output to the console is as below;
Rows : 3
Columns: 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
add a comment |
up vote
0
down vote
In Java, "int [ ][ ]" stands for a 2-dimensional integer array. To make it easy to understand, simply we can compare 2-d integer array with a simple 1-d integer array;
1) Down below, a 1-d int array is initialized;
int arr1d = { 1,2,3 };
2) And on this one, a 2-d int array is initialized;
int arr2d = { {1,2,3}, {4,5,6} };
It is important to understand the structure of 2d arrays. If you print the length of the arr2d , you will get 2 the rows of the array which is 2.
System.out.println(arr2d.length);
You will get the length of the outer array, which is actually row count of the array.
To get the inner array length, which is actually the column count;
System.out.println(arr2d[0].length);
Notice that we take the first row, and get the length of the inner array and print the column number.
To get familiar with the usage of the 2d array in a method, you can check this out;
private static void printIntegerArray(int intArray) {
for(int i = 0; i < intArray.length; i++ ) {
for(int j = 0; j < intArray[i].length; j++ ) {
System.out.printf("%3d ", intArray[i][j]);
}
System.out.println();
}
}
In this static method, int intArray is the only parameter which is obviously a 2 dimensional int array. There are two nested for loops to print the array as a matrix. The outer loop is traversing the rows and the inner loop is traversing on the inner loop.
Here is the complete example for the 2D Method usage;
public class Test2DArray {
public static void main(String args) {
//Init 2d integer list
int simpleArray = { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15} };
//Length of outer array which is actually Row Count;
System.out.println("Rows : " + simpleArray.length);
//Length of inner array which is actually Column Count;
//Notice that we take the first Row to get the Column length
System.out.println("Columns: " + simpleArray[0].length);
//Call the printIntegerList method with int parameter
printIntegerArray(simpleArray);
}
private static void printIntegerArray(int intArray) {
for(int i = 0; i < intArray.length; i++ ) {
for(int j = 0; j < intArray[i].length; j++ ) {
System.out.printf("%3d ", intArray[i][j]);
}
System.out.println();
}
}
}
And the output to the console is as below;
Rows : 3
Columns: 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
add a comment |
up vote
0
down vote
up vote
0
down vote
In Java, "int [ ][ ]" stands for a 2-dimensional integer array. To make it easy to understand, simply we can compare 2-d integer array with a simple 1-d integer array;
1) Down below, a 1-d int array is initialized;
int arr1d = { 1,2,3 };
2) And on this one, a 2-d int array is initialized;
int arr2d = { {1,2,3}, {4,5,6} };
It is important to understand the structure of 2d arrays. If you print the length of the arr2d , you will get 2 the rows of the array which is 2.
System.out.println(arr2d.length);
You will get the length of the outer array, which is actually row count of the array.
To get the inner array length, which is actually the column count;
System.out.println(arr2d[0].length);
Notice that we take the first row, and get the length of the inner array and print the column number.
To get familiar with the usage of the 2d array in a method, you can check this out;
private static void printIntegerArray(int intArray) {
for(int i = 0; i < intArray.length; i++ ) {
for(int j = 0; j < intArray[i].length; j++ ) {
System.out.printf("%3d ", intArray[i][j]);
}
System.out.println();
}
}
In this static method, int intArray is the only parameter which is obviously a 2 dimensional int array. There are two nested for loops to print the array as a matrix. The outer loop is traversing the rows and the inner loop is traversing on the inner loop.
Here is the complete example for the 2D Method usage;
public class Test2DArray {
public static void main(String args) {
//Init 2d integer list
int simpleArray = { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15} };
//Length of outer array which is actually Row Count;
System.out.println("Rows : " + simpleArray.length);
//Length of inner array which is actually Column Count;
//Notice that we take the first Row to get the Column length
System.out.println("Columns: " + simpleArray[0].length);
//Call the printIntegerList method with int parameter
printIntegerArray(simpleArray);
}
private static void printIntegerArray(int intArray) {
for(int i = 0; i < intArray.length; i++ ) {
for(int j = 0; j < intArray[i].length; j++ ) {
System.out.printf("%3d ", intArray[i][j]);
}
System.out.println();
}
}
}
And the output to the console is as below;
Rows : 3
Columns: 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
In Java, "int [ ][ ]" stands for a 2-dimensional integer array. To make it easy to understand, simply we can compare 2-d integer array with a simple 1-d integer array;
1) Down below, a 1-d int array is initialized;
int arr1d = { 1,2,3 };
2) And on this one, a 2-d int array is initialized;
int arr2d = { {1,2,3}, {4,5,6} };
It is important to understand the structure of 2d arrays. If you print the length of the arr2d , you will get 2 the rows of the array which is 2.
System.out.println(arr2d.length);
You will get the length of the outer array, which is actually row count of the array.
To get the inner array length, which is actually the column count;
System.out.println(arr2d[0].length);
Notice that we take the first row, and get the length of the inner array and print the column number.
To get familiar with the usage of the 2d array in a method, you can check this out;
private static void printIntegerArray(int intArray) {
for(int i = 0; i < intArray.length; i++ ) {
for(int j = 0; j < intArray[i].length; j++ ) {
System.out.printf("%3d ", intArray[i][j]);
}
System.out.println();
}
}
In this static method, int intArray is the only parameter which is obviously a 2 dimensional int array. There are two nested for loops to print the array as a matrix. The outer loop is traversing the rows and the inner loop is traversing on the inner loop.
Here is the complete example for the 2D Method usage;
public class Test2DArray {
public static void main(String args) {
//Init 2d integer list
int simpleArray = { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15} };
//Length of outer array which is actually Row Count;
System.out.println("Rows : " + simpleArray.length);
//Length of inner array which is actually Column Count;
//Notice that we take the first Row to get the Column length
System.out.println("Columns: " + simpleArray[0].length);
//Call the printIntegerList method with int parameter
printIntegerArray(simpleArray);
}
private static void printIntegerArray(int intArray) {
for(int i = 0; i < intArray.length; i++ ) {
for(int j = 0; j < intArray[i].length; j++ ) {
System.out.printf("%3d ", intArray[i][j]);
}
System.out.println();
}
}
}
And the output to the console is as below;
Rows : 3
Columns: 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
answered Jun 17 '15 at 8:45
Levent Divilioglu
6,13533282
6,13533282
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30880910%2fwhat-does-a-method-with-int-mean%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown