java二維數組的定義

來源:互聯網
上載者:User

標籤:main   int   div   long   不規則   new   二維數組   數組   public   

//定義二維數組寫法1  

class numthree
{
  public static void main(String[] args)
  {
    float[][]numthree;            //定義一個float類型的2維數組
    numthree=newfloat[5][5];      //為它分配5行5列的空間大小
    numthree[0][0]=1.1f;           //通過下標索引去訪問    1行1列=1.1
    numthree[1][0]=1.2f;                                 // 2行1列=1.2
    numthree[2][0]=1.3f;                                 // 3行1列=1.3
    numthree[3][0]=1.4f;                                 // 4行1列=1.4
    numthree[4][0]=1.5f;                                 // 5行1列=1.5
    System.out.println(numthree[0][0]); //列印換行輸出嘍
    System.out.println(numthree[1][0]);
    System.out.println(numthree[2][0]);
    System.out.println(numthree[3][0]);
    System.out.println(numthree[4][0]);
  }
}

//定義二維數組寫法2  定義的同時分配空間大小
class numfour
{
  public static void main(String[] args)
  {
       short[][] numfour=newshort[5][8]; //定義一個short類型的數組同時為它分配5行8列的空間大小
       numfour[0][7]=10;
       numfour[1][6]=20;
       numfour[2][5]=30;
       numfour[3][4]=40;
       numfour[4][3]=50;
      System.out.println(numfour[0][7]);
      System.out.println(numfour[1][6]);
      System.out.println(numfour[2][5]);
      System.out.println(numfour[3][4]);
      System.out.println(numfour[4][3]);

  }
}


//定義二維數組寫法3       不規則數組
class numfive
{
  public static void main(String[] args)
  {
    long[][] numfive=newlong[5][];    //定義一個long類型的不規則數組
    numfive[0]=newlong[5];            //為第1行分配5列
    numfive[1]=newlong[6];            //為第2行分配6列
    numfive[2]=newlong[7];            //為第3行分配7列
    numfive[3]=newlong[8];            //為第4行分配8列
    numfive[4]=newlong[9];            //為第5行分配9列

    numfive[0][4]=10000000000L;        //1行5列=10000000000
    numfive[1][5]=20000000000L;        //2行6列=20000000000
    numfive[2][6]=30000000000L;        //3行7列=30000000000
    numfive[3][7]=40000000000L;        //4行8列=40000000000
    numfive[4][8]=50000000000L;        //5行9列=50000000000

    System.out.println(numfive[0][4]);//列印換行輸出嘍
    System.out.println(numfive[1][5]);
    System.out.println(numfive[2][6]);
    System.out.println(numfive[3][7]);
    System.out.println(numfive[4][8]);
    System.out.println(numfive[4][7]); //列印輸出一個沒有定義數組元素的數組java會自動將他初始化值為0
  }
}


//定義2維數組寫法4   定義的同時賦初始值
class numsix
{
  public static void main(String[] args)
  {
    double[][]numsix={{1.111D,2.222D,3.333D},{4.444D,5.555D,6.666D}};//定義double型的數組分配3行3列的空間同時賦值
    System.out.println(numsix[0][0]); //列印換行輸出1行1列=1.111
    System.out.println(numsix[1][1]); //列印換行輸出2行2列=5.555
  }
}


//定義2維數組寫法5  定義不規則的2維數組同時賦初始值
class numseven
{
  public static void main(String[] args)
  {
    int[][] numseven=new int[][]{{10,20,30},{40,50},{60}};//沒什麼好說的如果你在看不懂 那就別學了!
    System.out.println(numseven[0][2]);
    System.out.println(numseven[1][1]);
    System.out.println(numseven[0][0]);
  }
}

//定義2維數組寫法6 定義不規則的2維數組同時賦初始值;
class numeight
{
  public static void main(String[] args)
  {
    int[][]numeight={{100,200,300,400},{500,600,700,800},{900,1000,1100,1200,1300}};
    System.out.println(numeight[0][2]);
    System.out.println(numeight[1][2]);
    System.out.println(numeight[2][1]);
  }
}

java二維數組的定義

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.