Bentuk umum pendeklarasian variable array dua dimensi di Java adalah sebagai berikut:
tipeData[ ][ ] namaVariable [= new tipeData [jumlahBaris] [jumlahKolom]];
atau
tipeData namaVariable[ ][ ] [= new tipeData [jumlahBaris] [jumlahKolom]];
Cara pendeklarasian variable array dua dimensi sama dengan cara pendeklarasian variable array satu dimensi lihat disini.
Mengakses variable array dua dimensi, mengisi variable array dua dimensi, mengambil variable array dua dimensi juga sama dengan cara yang berlaku pada variable array satu dimensi, yaitu dengan menyebutkan nomor indeks dari elemen. Perbedaanya hanya pada dua dimensi harus menyebutkan indeks baris dan indeks kolom.
Menghitung jumlah elemen pada array dua dimensi
long[][] gede = new long[5][5]; //deklarasi variable array dua dimensi
gede.length; // akan melaporkan jumlah baris, sedangkan
gede[i].length; // akan melaporkan jumlah kolom pada baris ke-i
Contoh program Array/Matrik 2 dimensi :
package javaapplication1;
class fzan2
{
public static void main(String[] args)
{
double m[][];
m = new double[4][4];
m[0][0] = 1;
m[1][1] = 1;
m[2][2] = 1;
m[3][3] = 1;
System.out.println(m[0][0]+" "+m[0][1]+" "+m[0][2]+" "+m[0][3]);
System.out.println(m[1][0]+" "+m[1][1]+" "+m[1][2]+" "+m[1][3]);
System.out.println(m[2][0]+" "+m[2][1]+" "+m[2][2]+" "+m[2][3]);
System.out.println(m[3][0]+" "+m[3][1]+" "+m[3][2]+" "+m[3][3]);
}
}
class fzan2
{
public static void main(String[] args)
{
double m[][];
m = new double[4][4];
m[0][0] = 1;
m[1][1] = 1;
m[2][2] = 1;
m[3][3] = 1;
System.out.println(m[0][0]+" "+m[0][1]+" "+m[0][2]+" "+m[0][3]);
System.out.println(m[1][0]+" "+m[1][1]+" "+m[1][2]+" "+m[1][3]);
System.out.println(m[2][0]+" "+m[2][1]+" "+m[2][2]+" "+m[2][3]);
System.out.println(m[3][0]+" "+m[3][1]+" "+m[3][2]+" "+m[3][3]);
}
}
wow
ReplyDeletegmana caranya bikin readmore kayak gtu? keren oey
hehehe pengen ya... ne aq share ^_^
ReplyDeletehttp://fzan-aquana.blogspot.com/2010/12/read-more-versi-baru.html
http://lh5.ggpht.com/_RVpTV2JOOxA/Szuird1YgpI/AAAAAAAABeU/j-5kkOqIqls/s128/t4belajarblogger45.gif
ReplyDeletehttp://lh6.ggpht.com/_RVpTV2JOOxA/SzukUo1EOrI/AAAAAAAABgs/fZxgnyDVy98/s128/t4belajarblogger83.gifhttp://lh3.ggpht.com/_RVpTV2JOOxA/Szhttphttp://lh5.ggpht.com/_RVpTV2JOOxA/Szhttp://lh6.ggpht.com/_RVpTV2JOOxA/SzujyTKhgiI/AAAAAAAABfw/JYWvu0pweyk/s128/t4belajarblogger68.gifujyfk4-CI/AAAAAAAABf0/iDuQc1VuhK0/s128/t4belajarblogger69.gif://lh6.ggpht.comhttp://lh5.ggpht.com/_RVpTV2JOOxA/Szuj8XeYxJI/AAAAAAAABf8/cM1Vq0qvbIE/s128/t4belajarblogger71.gif/_RVpTV2JOOxA/Szuj8SuJbTI/AAAAAAAABgA/Xz_QAiIaddA/s128/t4belajarblogger72.gifukJWa6http://lh4.ggpht.com/_RVpTV2JOOxA/Szuhttp://lh5.ggpht.com/_RVpTV2JOOhttp://lh6.ggpht.com/_RVpTV2JOOxA/Szuj85ST3dI/AAAAAAAABgM/KWGKTw5fINA/s128/t4belajarblogger75.gifxA/SzukIy6-03I/AAAAAAAABgQ/DV9zF6bJ6QE/s128/t4belajarblogger76.gifkJDtcY3I/AAAAAAAABgY/BtWxrW6MGKY/s128/t4belajarblogger78.gifbKI/AAAAAAAABgc/h1LCYMIJFXA/s128/t4belajarblogger79.gifhthttp://lh6.ggpht.com/_RVpTV2JOOxA/SzukI_b78PI/AAAAAAAAhttp://lh6.ggpht.com/_RVpTV2JOOxA/Szuj8p90apI/AAAAAAAABgE/vxA4mvtfxqQ/s128/t4belajarblogger73.gifBgU/QCvzWuY9kbU/s128/t4belajarblogger77.giftp://lh3.ggpht.com/_RVpTV2JOOxA/SzukUq497MI/AAAAAAAAhttp://lh3.ggpht.com/_RVpTV2JOOxA/SzukUfu-uXI/AAAAAAAABgk/F9SlfUxosuI/s128/t4belajarblogger81.gifBgo/C4HD0GXgwFM/s128/t4belajarblogger82.gif
ReplyDeletethanks kak artikelnya...
ReplyDeleteThank gan artikel nya sangat membantu :14
ReplyDelete