int twoD[][] = new int[4][5];
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + ' ');
System.out.println();
}
}
}
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
int twoD[][] = new int[4][];
twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[5];
twoD[3] = new int[5];
// Manually allocate differing size second dimensions.
class TwoDAgain {
public static void main(String args[]) {
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<i+1; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<i+1; j++)
System.out.print(twoD[i][j] + ' ');
System.out.println();
}
}
}
0
1 2
3 4 5
6 7 8 9
// Initialize a two-dimensional array.
class Matrix {
public static void main(String args[]) {
double m[][] = {
{ 0*0, 1*0, 2*0, 3*0 },
{ 0*1, 1*1, 2*1, 3*1 },
{ 0*2, 1*2, 2*2, 3*2 },
{ 0*3, 1*3, 2*3, 3*3 }
};
int i, j;
for(i=0; i<4; i++) {
for(j=0; j<4; j++)
System.out.print(m[i][j] + ' ');
System.out.println();
}
}
}
0.0 0.0 0.0 0.0
0.0 1.0 2.0 3.0
0.0 2.0 4.0 6.0
0.0 3.0 6.0 9.0
// Demonstrate a three-dimensional array.
class threeDMatrix {
public static void main(String args[]) {
int threeD[][][] = new int[3][4][5];
int i, j, k;
for(i=0; i<3; i++)
for(j=0; j<4; j++)
for(k=0; k<5; k++)
threeD[i][j][k] = i * j * k;
for(i=0; i<3; i++) {
for(j=0; j<4; j++) {
for(k=0; k<5; k++)
System.out.print(threeD[i][j][k] + ' ');
System.out.println();
}
System.out.println();
}
}
}
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 0 0 0 0
0 2 4 6 8
0 4 8 12 16
0 6 12 18 24
Most Viewed Articles (in Java ) | Latest Articles (in Java) |
/* |
* Copyright (c) 2011, StripBandunk and/or its affiliates. All rights reserved. |
* STRIPBANDUNK PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
*/ |
packagecom.stripbandunk.tutorial.util; |
/** |
* |
* @author Eko Kurniawan Khannedy |
*/ |
publicclassMahasiswa { |
privateString nim; |
privateString nama; |
privateString universitas; |
privateString fakultas; |
privateString jurusan; |
publicMahasiswa() { |
} |
publicMahasiswa(Stringnim, Stringnama) { |
this.nim = nim; |
this.nama = nama; |
} |
// getter dan setter |
} |
/* |
* Copyright (c) 2011, StripBandunk and/or its affiliates. All rights reserved. |
* STRIPBANDUNK PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
*/ |
packagecom.stripbandunk.tutorial.util; |
importjava.util.Comparator; |
/** |
* |
* @author echo |
*/ |
publicclassMahasiswaComparatorimplementsComparator<Mahasiswa> { |
privateboolean descending; |
publicbooleanisDescending() { |
return descending; |
} |
publicvoidsetDescending(booleandescending) { |
this.descending = descending; |
} |
@Override |
publicintcompare(Mahasiswao1, Mahasiswao2) { |
if (descending) { |
return o1.getNim().compareTo(o2.getNim()) *-1; |
} else { |
return o1.getNim().compareTo(o2.getNim()); |
} |
} |
} |
/* |
* Copyright (c) 2011, StripBandunk and/or its affiliates. All rights reserved. |
* STRIPBANDUNK PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
*/ |
packagecom.stripbandunk.tutorial.util; |
importjava.util.Arrays; |
/** |
* |
* @author Eko Kurniawan Khannedy |
*/ |
publicclassProgramMengurutkanMahasiswa { |
/** |
* @param args the command line arguments |
*/ |
publicstaticvoidmain(String[] args) { |
// TODO code application logic here |
Mahasiswa[] mahasiswas = { |
newMahasiswa('10106031', 'Eko Kurniawan Khannedy'), |
newMahasiswa('10106035', 'Aditya Yudha Pradhana'), |
newMahasiswa('10106024', 'Joko Moro'), |
newMahasiswa('10106010', 'Rully Isma Hidayat'), |
newMahasiswa('10106009', 'Purwangga Bagja') |
}; |
MahasiswaComparator comparator =newMahasiswaComparator(); |
comparator.setDescending(true); |
Arrays.sort(mahasiswas, comparator); |
for (Mahasiswa mahasiswa : mahasiswas) { |
System.out.format('%s - %s n', mahasiswa.getNim(), mahasiswa.getNama()); |
} |
} |
} |