泛型方法的定义和使用_【第4章:数组与方法】_数组的定义及使用

 数组的定义

public class Hello{

       public static void main(String args[]){

              int score[]=null;

              //为当前定义数组开辟3个空间

              score=new int[3];

              System.out.println(score[0]);

              System.out.println(score[1]);

              System.out.println(score[2]);

       }

}

默认情况下数组没有被填充每个元素时这里由于是int型数组,所以每个元素的值默认为0

f4a24ebeb910b1e0b7c842cc2c965c29.png

int score[]=null;也可以直接简写为int score[];

public class Hello{

       public static void main(String args[]){

              int score[];

              score=new int[3];

              System.out.println(score[0]);

              System.out.println(score[1]);

              System.out.println(score[2]);

       }

}

f4a24ebeb910b1e0b7c842cc2c965c29.png

数组长度可以用该数组变量的length获取

public class Hello{

       public static void main(String args[]){

              int score[];

              score=new int[3];

              System.out.println(score.length);

       }

}

491eed0074439e8c693c1dde192ac603.png

数组的填充(初始化)

用数组指定下标进行填充数组叫做数组的动态初始化

public class Hello{

       public static void main(String args[]){

              int score[];

              score=new int[3];

              for(int x=0;x

                     score[x]=x+1;

              }

              for(int x=0;x

                     System.out.println(score[x]);

              }

       }

}

8b9d8054e57bcdff15c2e8e47caade11.png

数组的静态初始化

上面的初始化方式是指定下标进行初始化的,也可以定义时候直接一起定义,这样连开辟空间都不用了,这种叫做静态初始化。

public class Hello{

       public static void main(String args[]){

              int score[]={91,92,94,95,97};

              for(int x=0;x

                     System.out.println(score[x]);

              }

       }

}

19698194913cea09e2a261b1cbbfdb79.png

数组求最值

public class Hello{

       public static void main(String args[]){

              int score[]={91,92,94,95,97,33,1,5,9};

              int max=0;

              int min=0;

              max=min=score[0];

              for(int x=0;x

                     if(score[x]>max){

                            max=score[x];

                     }

                     if(score[x]

                            min=score[x];

                     }

              }

              System.out.println(max);

              System.out.println(min);

       }

}

8de61e6b383f598c525b04d4d46ba2d6.png

排序

public class Hello{

       public static void main(String args[]){

              int score[]={4,1,2,7,5,8,3,9,21,15,25,19};

              for(int i=1;i

                     for(int j=0;j

                            if(score[i]

                                   int temp=score[i];

                                   score[i]=score[j];

                                   score[j]=temp;

                            }

                     }

                     System.out.print("第"+i+"次排序的结果:\t");

                     for(int j=0;j

                            System.out.print(score[j]+"\t");

                     }

                     System.out.println("");

              }

              for(int i=0;i

                     System.out.print(score[i]+"\t");

              }

       }

}

aafc87a7103151bdd55abd6220938615.png

二维数组

public class Hello{

       public static void main(String args[]){

              int score[][]=new int[4][3];

              score[0][1]=30;

              score[1][0]=31;

              score[2][2]=32;

              score[3][1]=33;

              score[1][1]=30;

              for(int i=0;i

                     System.out.println(i);

                     for(int j=0;j

                            System.out.println("\t"+score[i][j]);

                     }

              }

       }

}

开辟第一维4个空间,第一维的每个二维开辟3个空间。

98338aacb61e2a99fa365e435a8d7f56.png

静态二维数组

public class Hello{

       public static void main(String args[]){

              int score[][]={

                     {67,61},{78,89,83},{99,100,98,66,65}

              };

              for(int i=0;i

                     System.out.println(i);

                     for(int j=0;j

                            System.out.println("\t"+score[i][j]);

                     }

              }

       }

}

65d47c867f85662098c80e11f5e11554.png

三维数组

public class Hello{

       public static void main(String args[]){

              int score[][][]={

                     {

                            {67,61},{78,89,83},{99,100,98,66,65}

                     },

                     {

                            {63,60},{71,19,23},{9,10,108,6,75}

                     }

              };

              for(int i=0;i

                     System.out.println(i);

                     for(int j=0;j

                            System.out.println("\t"+i+"_"+j);

                            for(int z=0;z

                                   System.out.println("\t\t"+score[i][j][z]);

                            }

                     }

              }

       }

}

fde645ffcacba61bd65c45b9f169a2d9.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值