Getting the array length of a 2D array in Java
up vote
92
down vote
favorite
I need to get the length of a 2D array for both the row and column. I’ve successfully done this, using the following code:
public class MyClass
public static void main(String args)
int test;
test = new int[5][10];
int row = test.length;
int col = test[0].length;
System.out.println(row);
System.out.println(col);
This prints out 5, 10 as expected.
Now take a look at this line:
int col = test[0].length;
Notice that I actually have to reference a particular row, in order to get the column length. To me, this seems incredibly ugly. Additionally, if the array was defined as:
test = new int[0][10];
Then the code would fail when trying to get the length. Is there a different (more intelligent) way to do this?
java arrays multidimensional-array
add a comment |
up vote
92
down vote
favorite
I need to get the length of a 2D array for both the row and column. I’ve successfully done this, using the following code:
public class MyClass
public static void main(String args)
int test;
test = new int[5][10];
int row = test.length;
int col = test[0].length;
System.out.println(row);
System.out.println(col);
This prints out 5, 10 as expected.
Now take a look at this line:
int col = test[0].length;
Notice that I actually have to reference a particular row, in order to get the column length. To me, this seems incredibly ugly. Additionally, if the array was defined as:
test = new int[0][10];
Then the code would fail when trying to get the length. Is there a different (more intelligent) way to do this?
java arrays multidimensional-array
add a comment |
up vote
92
down vote
favorite
up vote
92
down vote
favorite
I need to get the length of a 2D array for both the row and column. I’ve successfully done this, using the following code:
public class MyClass
public static void main(String args)
int test;
test = new int[5][10];
int row = test.length;
int col = test[0].length;
System.out.println(row);
System.out.println(col);
This prints out 5, 10 as expected.
Now take a look at this line:
int col = test[0].length;
Notice that I actually have to reference a particular row, in order to get the column length. To me, this seems incredibly ugly. Additionally, if the array was defined as:
test = new int[0][10];
Then the code would fail when trying to get the length. Is there a different (more intelligent) way to do this?
java arrays multidimensional-array
I need to get the length of a 2D array for both the row and column. I’ve successfully done this, using the following code:
public class MyClass
public static void main(String args)
int test;
test = new int[5][10];
int row = test.length;
int col = test[0].length;
System.out.println(row);
System.out.println(col);
This prints out 5, 10 as expected.
Now take a look at this line:
int col = test[0].length;
Notice that I actually have to reference a particular row, in order to get the column length. To me, this seems incredibly ugly. Additionally, if the array was defined as:
test = new int[0][10];
Then the code would fail when trying to get the length. Is there a different (more intelligent) way to do this?
java arrays multidimensional-array
java arrays multidimensional-array
edited Oct 22 '10 at 19:31
robert_x44
8,03312235
8,03312235
asked Oct 22 '10 at 19:15
user432209
14.3k95071
14.3k95071
add a comment |
add a comment |
8 Answers
8
active
oldest
votes
up vote
126
down vote
accepted
Consider
public static void main(String args)
int foo = new int
new int 1, 2, 3 ,
new int 1, 2, 3, 4,
;
System.out.println(foo.length); //2
System.out.println(foo[0].length); //3
System.out.println(foo[1].length); //4
Column lengths differ per row. If you're backing some data by a fixed size 2D array, then provide getters to the fixed values in a wrapper class.
Unrelated question about your answer. What is the technique/method called where you put "...;" after the object definition. As a new developer I keep seeing this more and more.
– user432209
Oct 22 '10 at 19:27
Well, I understand that much :). I just thought there might be a specific name for the technique.
– user432209
Oct 22 '10 at 19:53
Not sure what the name of it is - object initialization? inline initialization? one of our Java Gurus will know
– NG.
Oct 22 '10 at 19:59
5
Per JLS 10.6 and 15.10, the curly braces part is simply an Array Initializer, while the whole thing starting with new is an Array Creation Expression.
– ILMTitan
Oct 22 '10 at 20:30
add a comment |
up vote
11
down vote
A 2D array is not a rectangular grid. Or maybe better, there is no such thing as a 2D array in Java.
import java.util.Arrays;
public class Main
public static void main(String args)
int test;
test = new int[5];//'2D array'
for (int i=0;i<test.length;i++)
test[i] = new int[i];
System.out.println(Arrays.deepToString(test));
Object test2;
test2 = new Object[5];//array of objects
for (int i=0;i<test2.length;i++)
test2[i] = new int[i];//array is a object too
System.out.println(Arrays.deepToString(test2));
Outputs
[, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
[, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
The arrays test
and test2
are (more or less) the same.
add a comment |
up vote
4
down vote
Java allows you to create "ragged arrays" where each "row" has different lengths. If you know you have a square array, you can use your code modified to protect against an empty array like this:
if (row > 0) col = test[0].length;
add a comment |
up vote
3
down vote
There's not a cleaner way at the language level because not all multidimensional arrays are rectangular. Sometimes jagged (differing column lengths) arrays are necessary.
You could easy create your own class to abstract the functionality you need.
If you aren't limited to arrays, then perhaps some of the various collection classes would work as well, like a Multimap.
add a comment |
up vote
2
down vote
If you have this array:
String example = "Please!", "Thanks", "Hello!", "Hey", "Hi!",
"Why?", "Where?", "When?", "Who?", "Yes!";
You can do this:
example.length;
= 2
example[0].length;
= 2
example[1].length;
= 2
example[0][1].length;
= 3
example[1][0].length;
= 4
I saw a similar answer before but I think it's easier to understand with examples!
– Andy
Oct 4 '17 at 15:41
add a comment |
up vote
1
down vote
Try this following program for 2d array in java:
public class ArrayTwo2
public static void main(String args) throws IOException,NumberFormatException
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a;
int sum=0;
a=new int[3][2];
System.out.println("Enter array with 5 elements");
for(int i=0;i<a.length;i++)
for(int j=0;j<a[0].length;j++)
a[i][j]=Integer.parseInt(br.readLine());
for(int i=0;i<a.length;i++)
for(int j=0;j<a[0].length;j++)
System.out.print(a[i][j]+" ");
sum=sum+a[i][j];
System.out.println();
//System.out.println("Array Sum: "+sum);
sum=0;
add a comment |
up vote
0
down vote
import java.util.Arrays;
public class Main
public static void main(String args)
double test = 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000;
int removeRow = 0, 1, 3, 4, ;
double newTest = new double[test.length - removeRow.length][test[0].length];
for (int i = 0, j = 0, k = 0; i < test.length; i++)
if (j < removeRow.length)
if (i == removeRow[j][0])
j++;
continue;
newTest[k][0] = test[i][0];
k++;
System.out.println(Arrays.deepToString(newTest));
Will print [[300.0], [600.0], [700.0], [800.0], [900.0], [1000.0]] convert to ints to lose decimal point. This solution assumes your input array will always be of size ([x][1])
– RobynVG
Nov 11 at 16:57
add a comment |
up vote
-1
down vote
public class Array_2D
int arr;
public Array_2D()
Random r=new Random(10);
arr = new int[5][10];
for(int i=0;i<5;i++)
for(int j=0;j<10;j++)
arr[i][j]=(int)r.nextInt(10);
public void display()
for(int i=0;i<5;i++)
for(int j=0;j<10;j++)
System.out.print(arr[i][j]+" ");
System.out.println("");
public static void main(String args)
Array_2D s=new Array_2D();
s.display();
1
this is not related to the question
– alfonso.kim
Apr 20 '17 at 13:12
add a comment |
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
126
down vote
accepted
Consider
public static void main(String args)
int foo = new int
new int 1, 2, 3 ,
new int 1, 2, 3, 4,
;
System.out.println(foo.length); //2
System.out.println(foo[0].length); //3
System.out.println(foo[1].length); //4
Column lengths differ per row. If you're backing some data by a fixed size 2D array, then provide getters to the fixed values in a wrapper class.
Unrelated question about your answer. What is the technique/method called where you put "...;" after the object definition. As a new developer I keep seeing this more and more.
– user432209
Oct 22 '10 at 19:27
Well, I understand that much :). I just thought there might be a specific name for the technique.
– user432209
Oct 22 '10 at 19:53
Not sure what the name of it is - object initialization? inline initialization? one of our Java Gurus will know
– NG.
Oct 22 '10 at 19:59
5
Per JLS 10.6 and 15.10, the curly braces part is simply an Array Initializer, while the whole thing starting with new is an Array Creation Expression.
– ILMTitan
Oct 22 '10 at 20:30
add a comment |
up vote
126
down vote
accepted
Consider
public static void main(String args)
int foo = new int
new int 1, 2, 3 ,
new int 1, 2, 3, 4,
;
System.out.println(foo.length); //2
System.out.println(foo[0].length); //3
System.out.println(foo[1].length); //4
Column lengths differ per row. If you're backing some data by a fixed size 2D array, then provide getters to the fixed values in a wrapper class.
Unrelated question about your answer. What is the technique/method called where you put "...;" after the object definition. As a new developer I keep seeing this more and more.
– user432209
Oct 22 '10 at 19:27
Well, I understand that much :). I just thought there might be a specific name for the technique.
– user432209
Oct 22 '10 at 19:53
Not sure what the name of it is - object initialization? inline initialization? one of our Java Gurus will know
– NG.
Oct 22 '10 at 19:59
5
Per JLS 10.6 and 15.10, the curly braces part is simply an Array Initializer, while the whole thing starting with new is an Array Creation Expression.
– ILMTitan
Oct 22 '10 at 20:30
add a comment |
up vote
126
down vote
accepted
up vote
126
down vote
accepted
Consider
public static void main(String args)
int foo = new int
new int 1, 2, 3 ,
new int 1, 2, 3, 4,
;
System.out.println(foo.length); //2
System.out.println(foo[0].length); //3
System.out.println(foo[1].length); //4
Column lengths differ per row. If you're backing some data by a fixed size 2D array, then provide getters to the fixed values in a wrapper class.
Consider
public static void main(String args)
int foo = new int
new int 1, 2, 3 ,
new int 1, 2, 3, 4,
;
System.out.println(foo.length); //2
System.out.println(foo[0].length); //3
System.out.println(foo[1].length); //4
Column lengths differ per row. If you're backing some data by a fixed size 2D array, then provide getters to the fixed values in a wrapper class.
answered Oct 22 '10 at 19:21
NG.
19.2k44559
19.2k44559
Unrelated question about your answer. What is the technique/method called where you put "...;" after the object definition. As a new developer I keep seeing this more and more.
– user432209
Oct 22 '10 at 19:27
Well, I understand that much :). I just thought there might be a specific name for the technique.
– user432209
Oct 22 '10 at 19:53
Not sure what the name of it is - object initialization? inline initialization? one of our Java Gurus will know
– NG.
Oct 22 '10 at 19:59
5
Per JLS 10.6 and 15.10, the curly braces part is simply an Array Initializer, while the whole thing starting with new is an Array Creation Expression.
– ILMTitan
Oct 22 '10 at 20:30
add a comment |
Unrelated question about your answer. What is the technique/method called where you put "...;" after the object definition. As a new developer I keep seeing this more and more.
– user432209
Oct 22 '10 at 19:27
Well, I understand that much :). I just thought there might be a specific name for the technique.
– user432209
Oct 22 '10 at 19:53
Not sure what the name of it is - object initialization? inline initialization? one of our Java Gurus will know
– NG.
Oct 22 '10 at 19:59
5
Per JLS 10.6 and 15.10, the curly braces part is simply an Array Initializer, while the whole thing starting with new is an Array Creation Expression.
– ILMTitan
Oct 22 '10 at 20:30
Unrelated question about your answer. What is the technique/method called where you put "...;" after the object definition. As a new developer I keep seeing this more and more.
– user432209
Oct 22 '10 at 19:27
Unrelated question about your answer. What is the technique/method called where you put "...;" after the object definition. As a new developer I keep seeing this more and more.
– user432209
Oct 22 '10 at 19:27
Well, I understand that much :). I just thought there might be a specific name for the technique.
– user432209
Oct 22 '10 at 19:53
Well, I understand that much :). I just thought there might be a specific name for the technique.
– user432209
Oct 22 '10 at 19:53
Not sure what the name of it is - object initialization? inline initialization? one of our Java Gurus will know
– NG.
Oct 22 '10 at 19:59
Not sure what the name of it is - object initialization? inline initialization? one of our Java Gurus will know
– NG.
Oct 22 '10 at 19:59
5
5
Per JLS 10.6 and 15.10, the curly braces part is simply an Array Initializer, while the whole thing starting with new is an Array Creation Expression.
– ILMTitan
Oct 22 '10 at 20:30
Per JLS 10.6 and 15.10, the curly braces part is simply an Array Initializer, while the whole thing starting with new is an Array Creation Expression.
– ILMTitan
Oct 22 '10 at 20:30
add a comment |
up vote
11
down vote
A 2D array is not a rectangular grid. Or maybe better, there is no such thing as a 2D array in Java.
import java.util.Arrays;
public class Main
public static void main(String args)
int test;
test = new int[5];//'2D array'
for (int i=0;i<test.length;i++)
test[i] = new int[i];
System.out.println(Arrays.deepToString(test));
Object test2;
test2 = new Object[5];//array of objects
for (int i=0;i<test2.length;i++)
test2[i] = new int[i];//array is a object too
System.out.println(Arrays.deepToString(test2));
Outputs
[, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
[, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
The arrays test
and test2
are (more or less) the same.
add a comment |
up vote
11
down vote
A 2D array is not a rectangular grid. Or maybe better, there is no such thing as a 2D array in Java.
import java.util.Arrays;
public class Main
public static void main(String args)
int test;
test = new int[5];//'2D array'
for (int i=0;i<test.length;i++)
test[i] = new int[i];
System.out.println(Arrays.deepToString(test));
Object test2;
test2 = new Object[5];//array of objects
for (int i=0;i<test2.length;i++)
test2[i] = new int[i];//array is a object too
System.out.println(Arrays.deepToString(test2));
Outputs
[, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
[, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
The arrays test
and test2
are (more or less) the same.
add a comment |
up vote
11
down vote
up vote
11
down vote
A 2D array is not a rectangular grid. Or maybe better, there is no such thing as a 2D array in Java.
import java.util.Arrays;
public class Main
public static void main(String args)
int test;
test = new int[5];//'2D array'
for (int i=0;i<test.length;i++)
test[i] = new int[i];
System.out.println(Arrays.deepToString(test));
Object test2;
test2 = new Object[5];//array of objects
for (int i=0;i<test2.length;i++)
test2[i] = new int[i];//array is a object too
System.out.println(Arrays.deepToString(test2));
Outputs
[, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
[, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
The arrays test
and test2
are (more or less) the same.
A 2D array is not a rectangular grid. Or maybe better, there is no such thing as a 2D array in Java.
import java.util.Arrays;
public class Main
public static void main(String args)
int test;
test = new int[5];//'2D array'
for (int i=0;i<test.length;i++)
test[i] = new int[i];
System.out.println(Arrays.deepToString(test));
Object test2;
test2 = new Object[5];//array of objects
for (int i=0;i<test2.length;i++)
test2[i] = new int[i];//array is a object too
System.out.println(Arrays.deepToString(test2));
Outputs
[, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
[, [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]]
The arrays test
and test2
are (more or less) the same.
answered Oct 22 '10 at 19:35
Ishtar
10.3k11730
10.3k11730
add a comment |
add a comment |
up vote
4
down vote
Java allows you to create "ragged arrays" where each "row" has different lengths. If you know you have a square array, you can use your code modified to protect against an empty array like this:
if (row > 0) col = test[0].length;
add a comment |
up vote
4
down vote
Java allows you to create "ragged arrays" where each "row" has different lengths. If you know you have a square array, you can use your code modified to protect against an empty array like this:
if (row > 0) col = test[0].length;
add a comment |
up vote
4
down vote
up vote
4
down vote
Java allows you to create "ragged arrays" where each "row" has different lengths. If you know you have a square array, you can use your code modified to protect against an empty array like this:
if (row > 0) col = test[0].length;
Java allows you to create "ragged arrays" where each "row" has different lengths. If you know you have a square array, you can use your code modified to protect against an empty array like this:
if (row > 0) col = test[0].length;
answered Oct 22 '10 at 19:21
robert_x44
8,03312235
8,03312235
add a comment |
add a comment |
up vote
3
down vote
There's not a cleaner way at the language level because not all multidimensional arrays are rectangular. Sometimes jagged (differing column lengths) arrays are necessary.
You could easy create your own class to abstract the functionality you need.
If you aren't limited to arrays, then perhaps some of the various collection classes would work as well, like a Multimap.
add a comment |
up vote
3
down vote
There's not a cleaner way at the language level because not all multidimensional arrays are rectangular. Sometimes jagged (differing column lengths) arrays are necessary.
You could easy create your own class to abstract the functionality you need.
If you aren't limited to arrays, then perhaps some of the various collection classes would work as well, like a Multimap.
add a comment |
up vote
3
down vote
up vote
3
down vote
There's not a cleaner way at the language level because not all multidimensional arrays are rectangular. Sometimes jagged (differing column lengths) arrays are necessary.
You could easy create your own class to abstract the functionality you need.
If you aren't limited to arrays, then perhaps some of the various collection classes would work as well, like a Multimap.
There's not a cleaner way at the language level because not all multidimensional arrays are rectangular. Sometimes jagged (differing column lengths) arrays are necessary.
You could easy create your own class to abstract the functionality you need.
If you aren't limited to arrays, then perhaps some of the various collection classes would work as well, like a Multimap.
answered Oct 22 '10 at 19:23
kaliatech
13.5k45370
13.5k45370
add a comment |
add a comment |
up vote
2
down vote
If you have this array:
String example = "Please!", "Thanks", "Hello!", "Hey", "Hi!",
"Why?", "Where?", "When?", "Who?", "Yes!";
You can do this:
example.length;
= 2
example[0].length;
= 2
example[1].length;
= 2
example[0][1].length;
= 3
example[1][0].length;
= 4
I saw a similar answer before but I think it's easier to understand with examples!
– Andy
Oct 4 '17 at 15:41
add a comment |
up vote
2
down vote
If you have this array:
String example = "Please!", "Thanks", "Hello!", "Hey", "Hi!",
"Why?", "Where?", "When?", "Who?", "Yes!";
You can do this:
example.length;
= 2
example[0].length;
= 2
example[1].length;
= 2
example[0][1].length;
= 3
example[1][0].length;
= 4
I saw a similar answer before but I think it's easier to understand with examples!
– Andy
Oct 4 '17 at 15:41
add a comment |
up vote
2
down vote
up vote
2
down vote
If you have this array:
String example = "Please!", "Thanks", "Hello!", "Hey", "Hi!",
"Why?", "Where?", "When?", "Who?", "Yes!";
You can do this:
example.length;
= 2
example[0].length;
= 2
example[1].length;
= 2
example[0][1].length;
= 3
example[1][0].length;
= 4
If you have this array:
String example = "Please!", "Thanks", "Hello!", "Hey", "Hi!",
"Why?", "Where?", "When?", "Who?", "Yes!";
You can do this:
example.length;
= 2
example[0].length;
= 2
example[1].length;
= 2
example[0][1].length;
= 3
example[1][0].length;
= 4
answered Oct 3 '17 at 15:15
Andy
156111
156111
I saw a similar answer before but I think it's easier to understand with examples!
– Andy
Oct 4 '17 at 15:41
add a comment |
I saw a similar answer before but I think it's easier to understand with examples!
– Andy
Oct 4 '17 at 15:41
I saw a similar answer before but I think it's easier to understand with examples!
– Andy
Oct 4 '17 at 15:41
I saw a similar answer before but I think it's easier to understand with examples!
– Andy
Oct 4 '17 at 15:41
add a comment |
up vote
1
down vote
Try this following program for 2d array in java:
public class ArrayTwo2
public static void main(String args) throws IOException,NumberFormatException
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a;
int sum=0;
a=new int[3][2];
System.out.println("Enter array with 5 elements");
for(int i=0;i<a.length;i++)
for(int j=0;j<a[0].length;j++)
a[i][j]=Integer.parseInt(br.readLine());
for(int i=0;i<a.length;i++)
for(int j=0;j<a[0].length;j++)
System.out.print(a[i][j]+" ");
sum=sum+a[i][j];
System.out.println();
//System.out.println("Array Sum: "+sum);
sum=0;
add a comment |
up vote
1
down vote
Try this following program for 2d array in java:
public class ArrayTwo2
public static void main(String args) throws IOException,NumberFormatException
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a;
int sum=0;
a=new int[3][2];
System.out.println("Enter array with 5 elements");
for(int i=0;i<a.length;i++)
for(int j=0;j<a[0].length;j++)
a[i][j]=Integer.parseInt(br.readLine());
for(int i=0;i<a.length;i++)
for(int j=0;j<a[0].length;j++)
System.out.print(a[i][j]+" ");
sum=sum+a[i][j];
System.out.println();
//System.out.println("Array Sum: "+sum);
sum=0;
add a comment |
up vote
1
down vote
up vote
1
down vote
Try this following program for 2d array in java:
public class ArrayTwo2
public static void main(String args) throws IOException,NumberFormatException
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a;
int sum=0;
a=new int[3][2];
System.out.println("Enter array with 5 elements");
for(int i=0;i<a.length;i++)
for(int j=0;j<a[0].length;j++)
a[i][j]=Integer.parseInt(br.readLine());
for(int i=0;i<a.length;i++)
for(int j=0;j<a[0].length;j++)
System.out.print(a[i][j]+" ");
sum=sum+a[i][j];
System.out.println();
//System.out.println("Array Sum: "+sum);
sum=0;
Try this following program for 2d array in java:
public class ArrayTwo2
public static void main(String args) throws IOException,NumberFormatException
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a;
int sum=0;
a=new int[3][2];
System.out.println("Enter array with 5 elements");
for(int i=0;i<a.length;i++)
for(int j=0;j<a[0].length;j++)
a[i][j]=Integer.parseInt(br.readLine());
for(int i=0;i<a.length;i++)
for(int j=0;j<a[0].length;j++)
System.out.print(a[i][j]+" ");
sum=sum+a[i][j];
System.out.println();
//System.out.println("Array Sum: "+sum);
sum=0;
edited Jan 11 '17 at 6:51
abpatil
8881417
8881417
answered Jan 11 '17 at 5:20
Pampanagouda Karmanchi
111
111
add a comment |
add a comment |
up vote
0
down vote
import java.util.Arrays;
public class Main
public static void main(String args)
double test = 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000;
int removeRow = 0, 1, 3, 4, ;
double newTest = new double[test.length - removeRow.length][test[0].length];
for (int i = 0, j = 0, k = 0; i < test.length; i++)
if (j < removeRow.length)
if (i == removeRow[j][0])
j++;
continue;
newTest[k][0] = test[i][0];
k++;
System.out.println(Arrays.deepToString(newTest));
Will print [[300.0], [600.0], [700.0], [800.0], [900.0], [1000.0]] convert to ints to lose decimal point. This solution assumes your input array will always be of size ([x][1])
– RobynVG
Nov 11 at 16:57
add a comment |
up vote
0
down vote
import java.util.Arrays;
public class Main
public static void main(String args)
double test = 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000;
int removeRow = 0, 1, 3, 4, ;
double newTest = new double[test.length - removeRow.length][test[0].length];
for (int i = 0, j = 0, k = 0; i < test.length; i++)
if (j < removeRow.length)
if (i == removeRow[j][0])
j++;
continue;
newTest[k][0] = test[i][0];
k++;
System.out.println(Arrays.deepToString(newTest));
Will print [[300.0], [600.0], [700.0], [800.0], [900.0], [1000.0]] convert to ints to lose decimal point. This solution assumes your input array will always be of size ([x][1])
– RobynVG
Nov 11 at 16:57
add a comment |
up vote
0
down vote
up vote
0
down vote
import java.util.Arrays;
public class Main
public static void main(String args)
double test = 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000;
int removeRow = 0, 1, 3, 4, ;
double newTest = new double[test.length - removeRow.length][test[0].length];
for (int i = 0, j = 0, k = 0; i < test.length; i++)
if (j < removeRow.length)
if (i == removeRow[j][0])
j++;
continue;
newTest[k][0] = test[i][0];
k++;
System.out.println(Arrays.deepToString(newTest));
import java.util.Arrays;
public class Main
public static void main(String args)
double test = 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000;
int removeRow = 0, 1, 3, 4, ;
double newTest = new double[test.length - removeRow.length][test[0].length];
for (int i = 0, j = 0, k = 0; i < test.length; i++)
if (j < removeRow.length)
if (i == removeRow[j][0])
j++;
continue;
newTest[k][0] = test[i][0];
k++;
System.out.println(Arrays.deepToString(newTest));
edited Nov 11 at 17:17
Axel P
99621124
99621124
answered Nov 11 at 16:54
RobynVG
1
1
Will print [[300.0], [600.0], [700.0], [800.0], [900.0], [1000.0]] convert to ints to lose decimal point. This solution assumes your input array will always be of size ([x][1])
– RobynVG
Nov 11 at 16:57
add a comment |
Will print [[300.0], [600.0], [700.0], [800.0], [900.0], [1000.0]] convert to ints to lose decimal point. This solution assumes your input array will always be of size ([x][1])
– RobynVG
Nov 11 at 16:57
Will print [[300.0], [600.0], [700.0], [800.0], [900.0], [1000.0]] convert to ints to lose decimal point. This solution assumes your input array will always be of size ([x][1])
– RobynVG
Nov 11 at 16:57
Will print [[300.0], [600.0], [700.0], [800.0], [900.0], [1000.0]] convert to ints to lose decimal point. This solution assumes your input array will always be of size ([x][1])
– RobynVG
Nov 11 at 16:57
add a comment |
up vote
-1
down vote
public class Array_2D
int arr;
public Array_2D()
Random r=new Random(10);
arr = new int[5][10];
for(int i=0;i<5;i++)
for(int j=0;j<10;j++)
arr[i][j]=(int)r.nextInt(10);
public void display()
for(int i=0;i<5;i++)
for(int j=0;j<10;j++)
System.out.print(arr[i][j]+" ");
System.out.println("");
public static void main(String args)
Array_2D s=new Array_2D();
s.display();
1
this is not related to the question
– alfonso.kim
Apr 20 '17 at 13:12
add a comment |
up vote
-1
down vote
public class Array_2D
int arr;
public Array_2D()
Random r=new Random(10);
arr = new int[5][10];
for(int i=0;i<5;i++)
for(int j=0;j<10;j++)
arr[i][j]=(int)r.nextInt(10);
public void display()
for(int i=0;i<5;i++)
for(int j=0;j<10;j++)
System.out.print(arr[i][j]+" ");
System.out.println("");
public static void main(String args)
Array_2D s=new Array_2D();
s.display();
1
this is not related to the question
– alfonso.kim
Apr 20 '17 at 13:12
add a comment |
up vote
-1
down vote
up vote
-1
down vote
public class Array_2D
int arr;
public Array_2D()
Random r=new Random(10);
arr = new int[5][10];
for(int i=0;i<5;i++)
for(int j=0;j<10;j++)
arr[i][j]=(int)r.nextInt(10);
public void display()
for(int i=0;i<5;i++)
for(int j=0;j<10;j++)
System.out.print(arr[i][j]+" ");
System.out.println("");
public static void main(String args)
Array_2D s=new Array_2D();
s.display();
public class Array_2D
int arr;
public Array_2D()
Random r=new Random(10);
arr = new int[5][10];
for(int i=0;i<5;i++)
for(int j=0;j<10;j++)
arr[i][j]=(int)r.nextInt(10);
public void display()
for(int i=0;i<5;i++)
for(int j=0;j<10;j++)
System.out.print(arr[i][j]+" ");
System.out.println("");
public static void main(String args)
Array_2D s=new Array_2D();
s.display();
answered Apr 20 '17 at 13:00
peter
1
1
1
this is not related to the question
– alfonso.kim
Apr 20 '17 at 13:12
add a comment |
1
this is not related to the question
– alfonso.kim
Apr 20 '17 at 13:12
1
1
this is not related to the question
– alfonso.kim
Apr 20 '17 at 13:12
this is not related to the question
– alfonso.kim
Apr 20 '17 at 13:12
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%2f4000169%2fgetting-the-array-length-of-a-2d-array-in-java%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