How to do Jagged Arrays in Java?
This is a brief tutorial on how to do jagged arrays in Java.
Most of the time, when we thing of arrays, we think of M*N rectangular arrays, where M represents the rows and N represents the columns. These type of arrays are easy to create in Java:
public class arrayExample1 {
public static void main(String[] args) {
//Array is declared and initialized.
int[][] array = new int[10][5];
//Fill the array
for (int i = 0; i<10; i++) {
for (int j = 0; j<5; j++) {
array[i][j] = j;
}
}
//Test the array
for (int i = 0; i<10; i++) {
for (int j = 0; j<5; j++) {
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
}
The output would be something like this:
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
But Rommel Rico can also create jagged arrays in Java! That is, we can create two-dimensional arrays that have M rows but each row has X columns. 'Whaaat!?', you say. Look:
public class arrayExample2 {
public static void main(String[] args) {
//Declare, Initialize, and fill the array
int[][] array = {{0,1,2,3,4},
{0,1,2},
{0,1,2,3},
{0,1},
{0},
{0},
{0,1,2,3,4,5},
{0},
{0,1,2,3},
{0,1,2,3}};
//Test the array
for (int i = 0; i
The output would be something like this:
0 1 2 3 4
0 1 2
0 1 2 3
0 1
0
0
0 1 2 3 4 5
0
0 1 2 3
0 1 2 3
Hi, my name is Rommel Rico. I work as a Sr. Programmer Analyst at the University of San Diego. My passions include entrepreneurship, computers, languages, football (soccer), art, reddit, theater, science fiction, classical music, aviation, running, and video games. At the moment, I'm really into StarCraft 2.
Sparks
1 Jul, 2010
Could you possibly do a post on how jagged arrays can be used and when they would be preferred over normal arrays? Thanks!
Rommel
1 Jul, 2010
Done. http://rommelrico.com/how-jagged-arrays-can-be-used-and-when-they-would-be-preferred-over-normal-arrays/
Sparks
3 Jul, 2010
Awesome
I’ll check it out here shortly!
meena
11 Aug, 2010
Awful!!! I’ll never check it out….:P
sala nalaik….
LeoO
15 Mar, 2011
yeah, you make me open my eclipse to try this, it´s true; there is no agreement to Initialize or print an array, good observation.
sorry, but with my Eclipse open i start to make some, experiment and find this :
public class ClaseA {
public ClaseA() {
throw new RuntimeException();
}
public static void main(String arguments[]) {
int b[][] = new int[3][3];
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length; j++) {
System.out.print(b[i][j]);
}
System.out.println();
}
int a[][] = new int[3][];
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
This is the Ouput:
000
000
000
null
null
null
jeje, the second array has "no auto-Initialize"
rare for me
thanks for the info dude! see you at SC n_n
peace!
LeoO
15 Mar, 2011
hey! forget about the Constructor in ClaseA!! , it was other experiment
just ignore it
Rakesh
13 Feb, 2012
sir tell me best java(basic) book or link
sunil
14 Feb, 2012
Could you tell me the best spring book or link….
Denver Sylvester
12 Mar, 2012
Stunning essay, acquired the enjoyment of reading
เกมส์ พนักงาน
12 Mar, 2012
It can be tough to write about this topic. I think you did an excellent job though! Thanks for this! 21766
\lansoprazole\
16 Mar, 2012
As soon as I discovered this web site I went on reddit to share some with the enjoy with them. 972278
Harland Olivarria
17 Mar, 2012
Well I definitely liked reading it. This information procured by you is very practical for correct planning.
reverse phone lookup
18 Mar, 2012
Nice one for the right to discuss this, I think boldy a ton and consequently adoration reading through more this important idea. If ever possibility, since you discover understanding, you feelings upgrading your web page as of furthermore facts? This is very useful for my lifestyle.
Tarzan vibrator
19 Mar, 2012
How may be the new year going? I hope to read a lot more fascinating posts like last year 391384
Tarzan vibrator
20 Mar, 2012
More than and more than once again I like to take into consideration this troubles. As a matter of fact it wasn
ici
4 May, 2012
Excellent post, I conceive site owners need to larn a whole lot from this site its really user friendly . 578654