Minggu, 06 Mei 2012
Array satu dimensi dan dua dimensi
public class Array1Dimensi {
public static void main(String[] args){
int bab[] = new int[10];
for(int i=0;i<10;i++){
bab[i]=i+1;
}
for(int i=0;i<10;i++){
System.out.println("array ke-"+i+" = "+bab[i]);
}
}
}
public class Array1Dimensiinput {
public static void main(String[] args){
int bab[] = new int[Integer.parseInt(JOptionPane.showInputDialog("Masukkan Batas Array!!"))];
for(int i=0;i<bab.length;i++){
bab[i]=Integer.parseInt(JOptionPane.showInputDialog("Masukkan data ke-"+(i+1)));
}
for(int i=0;i<bab.length;i++){
System.out.println("array ke-"+i+" = "+bab[i]);
}
}
}
public class Array2Dimensi {
public static void main(String[] args){
int bab[][] = new int[2][3];
int index=1;
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
bab[i][j]=index;
index++;
}
}
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
System.out.print(bab[i][j]+" ");
}
System.out.println();
}
System.out.println();
System.out.println();
for(int i=0;i<1;i++){
for(int j=0;j<3;j++){
System.out.print(bab[i][j]+" "+bab[i+1][j]);
System.out.println();
}
System.out.println();
}
}
}
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar