Selasa, 10 Januari 2012

Array Dimensi

ARRAY DIMENSI

  A.  // Menampilkan Array 1 dimensi
       public class Dimensi {
     public static void main (String[] args) {
    
     int[] data1 = new int[] {8,7,8,9,3,5,6};
     for (int i=0; i<data1.length; i++) {
           System.out.println("data[" + i + "]= " + data1[i]);
           }}}

  B.  //Menampilkan Array 2 dimensi
       1. public class Dimensi {
     public static void main (String[] args) {
    
     String[][] daftarharga = new String [2][3];

     daftarharga [0][0] = "Gula";
     daftarharga [1][0] = "2000";
    
     daftarharga [0][1] = "Teh";
     daftarharga [1][1] = "1500";
    
     daftarharga [0][2] = "Keju";
     daftarharga [1][2] = "2900";
     /*
     System.out.println(daftarharga [0][0]);
     System.out.println(daftarharga [1][0]);
    
     System.out.println(daftarharga [0][1]);
     System.out.println(daftarharga [1][1]);
    
     System.out.println(daftarharga [0][2]);
     System.out.println(daftarharga [1][2]);
     */
     //jika memakai perulangan:
     for (int i=0; i<3; i++){
     System.out.println(daftarharga [0][i]);
     System.out.println(daftarharga [1][i]);
     }}}

    2.   public class Dimensi {
     public static void main (String[] args) {
    
     String nilaibaru[][] = new String[2][4];
     nilaibaru[0][0] = "Yudi";
     nilaibaru[0][1] = "8";
     nilaibaru[0][2] = "9";
     nilaibaru[0][3] = "7";
    
     nilaibaru[1][0] = "Agus";
     nilaibaru[1][1] = "10";
     nilaibaru[1][2] = "8";
     nilaibaru[1][3] = "9";

     for(int i=0;i<nilaibaru.length;i++){
     for (int j=0;j<nilaibaru[i].length;j++){
     System.out.println(nilaibaru[i][j]);
     }}}}

    3.   public class Dimensi {
     public static void main (String[] args) {
    
     String[][]daftarhobi = new String [][]
     {{"aku","baca","tidur"},
     {"kamu","makan","tidur","minum"},
     {"dia","bangun"}};
System.out.println("-------------------------");
    
     System.out.println(daftarhobi[0][0]);
     System.out.println(daftarhobi[0][1]);
     System.out.println(daftarhobi[0][2]);
    
     System.out.println(daftarhobi[1][0]);
     System.out.println(daftarhobi[1][1]);
     System.out.println(daftarhobi[1][2]);
     System.out.println(daftarhobi[1][3]);
    
     System.out.println(daftarhobi[2][0]);
     System.out.println(daftarhobi[2][1]);
     }}

0 komentar:

Posting Komentar

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hosted Desktop