• 二维数组


    1.声明

    类型说明符 数组名[][] / [][]数组名

    1 int a[][];

    2.初始化

    数组名=new  类型说明符[数组长度][];

    数组名=new  类型说明符[数组长度][数组长度];

    1 a[][]=new int[3][]; 
    2 
    3 a[][]=new int[3][4];

    具体说明:a[][]=new int[3][4]表示创建一个有三个元素(a[0],a[1],a[2])的数组,每个元素也是数组,然后每个数组有包含4个元素。

    将该语句拆分成四句:

    1 a[][]=new int[3][]; 
    2 a[0]=new int[4];
    3 a[1]=new int[4];
    4 a[2]=new int[4];

    等价于以下语句:

    1 int a[][]=new int[3][];
    2 
    3 for(int i=0;i<3;i++) {
    4      a[i]=new int[4];
    5 }

    该语句效果如图2.7:

     

    变形:

    a[0]=new int[3]; a[1]=new int[2]; a[2]=new int[1]

    表示一个二维数组,第一行有三个元素,第二行有两个元素,第三行有一个元素

     

    3.应用

    3.1乘法口诀

     1 public class Chengfa { 
     2 
     3 public static void main(String args[]){  
     4 
     5    //确定一个有10个数组(它的元素也为数组)的二维数组
     6    int a[][]=new int[10][];    
     7 
     8    //取出9个数组(a[1],a[2],...a[9])确定空间(元素数目),其余为空
     9    for(int i=1;i<a.length;i++){   
    10 //初始化9个数组的元素数目(空间),a[1]有两个空间,以此类推 11 a[i]=new int [i+1];
    12 //为9个数组的元素赋值,a[1]中有a[1][0]和a[1][1],为a[1][1]赋值,其余为空,以此类推 13 for(int j=1;j<a[i].length;j++){ 14 a[i][j]=i*j; 15 System.out.print(i+"*"+j+"="+a[i][j]+" "); //" "为空格键 16 } 17 18 //内循环结束后执行该语句,即每确定一个i,都将换一行 19 System.out.println(); 20 } 21 } 22 }

    变形:矩阵

     1 public class Juzhen {
     2     public static void main(String args[]){
     3         int a[][]=new int[10][10];     
     4 
     5            //取出9个数组,a[1],a[2]....a[9]
     6         for(int i=1;i<a.length;i++){
     7          //    a[i]=new int [i+1];    该数组的空间(元素数目)已经确定,不需要类似的语句进行确定
     8 
     9        //a[1]中有a[1][0],a[1][1]....a[1][10],为a[1][1].....a[1][9]赋值,其余为空,以此类推
    10             for(int j=1;j<a[i].length;j++){
    11                 a[i][j]=i*j;
    12                 System.out.print(i+"*"+j+"="+a[i][j]+"	");   //"	"为空格键
    13             }       
    14             System.out.println();
    15             }
    16     } 
    17 }

    3.2杨辉三角

     1 public class Yanghui {
     2 public static void main(String args[]){
     3 
     4   //确定一个有10个数组(元素为数组)的二维数组
     5   int a[][]=new int[10][];
     6    //取出a[0],a[1]......a[9]十个数组
     7   for(int i=0;i<a.length;i++){
     8    //为10个数组确定空间(元素数目)
     9    a[i]=new int[i+1];
    10    //将所有数组的第一个和最后一个元素元素赋值为1
    11     a[i][0]=1;
    12     a[i][i]=1;
    13    }
    14 
    15     //取出a[0],a[1]......a[9]十个数组
    16      for(int i=0;i<a.length;i++){
    17 
    18   //从第三个数组开始
    19      if(i>1){
    20       for(int j=0;j<a.length;j++){
    21 
    22   //所有数组的第二个到倒数第二个元素,它们的值为前一个数组所对应的元素和前一个元素的和(a[2][1]=a[1][1]+a[1][0])
    23      if(j>0&&j<i){
    24         a[i][j]=a[i-1][j]+a[i-1][j-1]; 
    25        }
    26       }
    27      }   
    28     }
    29     //通过下标访问数组的元素
    30     for(int i=0;i<a.length;i++){ 
    31      for(int j=0;j<a[i].length;j++){
    32       //这里使用print不进行自动换行,使用“	”进行跳格(tab key即为空格键)
    33    System.out.print(a[i][j]+"	");
    34      }   
    35    //这里每进行一次for循环,都将结果进行换行
    36    System.out.println();
    37     }    
    38  }
    39 }

     

  • 相关阅读:
    中国历史朝代公元对照简表
    [Solved] DashBoard – Excel Service: The data sources may be unreachable, may not be responding, or may have denied you access.
    Delete/Remove Project from TFS 2010
    Sharepoint site showing system account instead of my username on the top right corner.
    你的成功在于你每天养成的习惯
    Internet Information Services is running in 32bit emulation mode. Correct the issue listed above and rerun setup.
    Prepare to back up and restore a farm (Office SharePoint Server 2007)
    Word中字号与磅值的对应关系
    How to: Change the Frequency for Refreshing the Data Warehouse for Team System
    UI Automation in WPF/Silverlight
  • 原文地址:https://www.cnblogs.com/jfl-xx/p/4663481.html
Copyright © 2020-2023  润新知