ArrayTangga.java

November 3, 2009

Dalam buku Dasar Pemrograman Java 2 karya Abdul Kadir, terdapat program yang disebut ArrayTangga.java. Program ini menggunakan array 2 dimensi (baris dan kolom).

Langsung saja, berikut contoh programnya:

/**
 * @File: ArrayTangga.java
 * @url: https://thepetarung.wordpress.com
 * @tanggal: 3 nop 2009
 */

public class ArrayTangga{
    public static void main(String[] args){

        int[][] c = new int[4][]; //alokasikan array 2 dimensi; 4 baris.

        int I,J;

        for(I = 0; I < c.length; I = I + 1)
            c[I] = new int[I+1];

        //Isi masing-masing elemen.
        for(I = 0; I < c.length; I = I + 1)
            for (J = 0; J < c[I].length; J = J + 1)
                c[I][J] = I + J;

        //Menampilkan isi semua elemen array
        for (I = 0; I < c.length; I = I + 1){
            for (J = 0; J < c[I].length; J = J + 1)
                System.out.print(c[I][J] + " ");
            System.out.println(""); //pindah baris
        }
    }
}

Setelah dijalankan, hasilnya sebagai berikut:

0
1 2
2 3 4
3 4 5 6

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: