Java免费教学(三)

二维数组的使用

二维数组是指以一维数组作为元素的数组。
语法:数据类型[行][列]数组名。
二维数组的初始化
静态:例如:

int[][]stuScores={
{80,90,70},
{23,45,56},
{2334,4564,56767}//结尾处不要用逗号;
};

动态:

//方法一:
int[][] stuScores=new int[6][3];
//方法二:
int[][]stuScores;
     stuScores=new int[6][3];
//方法三:
int[][] stuScores=new int[3][];
         stuScores[0]=new int[3];
         stuScore[1]=new int[2];
         stuScore[2]=new int[5];
         //不规则数组声明

访问数组中的元素:

int[][] arr=new int[4][3];
//确定你访问的数据在那一行
arr[1];
//确定你访问的数据在那一列
arr[1][2];

下面举个列子来循环输入二维数组,循环输出二维数组,,循环改变二维数组的数据:


public class HelloWorld {    
public static void main(String[] args) {       
 /********** Begin **********/      
   int i=0,j=0;       
    int[][] scores={           
     {92,85},           
      {91,65},           
       {90,33}       
        };      
    for(i=0;i<scores.length;i++){        
        for(j=0;j<scores[i].length;j++){             
          System.out.println(scores[i][j]);            
          scores[i][j]=j+1;                      
      }      
    }      
    for(i=0;i<scores.length;i++){            
        for(j=0;j<scores[i].length;j++){               
            System.out.println(scores[i][j]); 
            }         
      }

          /********** End **********/    
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

茫渡。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值