Java 数组声明的几种方式

2018-06-18 02:57:07来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

Java数组定义声明的几种方法:

1. 类型名称[] 变量名=new 类型名称[length];

2.类型名称[] 变量名={?,?,?};

3.类型名称[] 变量名=new 类型名称[]{?,?,?};

代码样例:

public class Example1 {

    public static void main(String []args){
        //数组定义的几种方法
//        1. 类型名称[] 变量名=new 类型名称[length];
        int []a=new int[4];
        a[0]=1;
        a[1]=2;
        a[2]=3;
        a[3]=4;
        System.out.println(a);
        System.out.println(a[0]);
        System.out.println(a[1]);
        System.out.println(a[2]);
        System.out.println(a[3]);
        
//        2.类型名称[] 变量名={?,?,?};
        int[] b={1,2,3,4};
        System.out.println(b[0]);
        System.out.println(b[1]);
        System.out.println(b[2]);
        System.out.println(b[3]);
        
//        3.类型名称[] 变量名=new 类型名称[]{?,?,?};
        int[]c=new int[]{1,2,3,6};
        System.out.println(c[0]);
        System.out.println(c[1]);
        System.out.println(c[2]);
        System.out.println(c[3]);
    }

}
三种方法总览

分开展示:

1. 类型名称[] 变量名=new 类型名称[length];

//    1. 类型名称[] 变量名=new 类型名称[length];
int []a=new int[4];
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
System.out.println(a);
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
System.out.println(a[3]);
方法一:

 

2.类型名称[] 变量名={?,?,?};

//    2.类型名称[] 变量名={?,?,?};
int[] b={1,2,3,4};
System.out.println(b[0]);
System.out.println(b[1]);
System.out.println(b[2]);
System.out.println(b[3]);
方法二:

 

3.类型名称[] 变量名=new 类型名称[]{?,?,?};

//    3.类型名称[] 变量名=new 类型名称[]{?,?,?};
int[]c=new int[]{1,2,3,6};
System.out.println(c[0]);
System.out.println(c[1]);
System.out.println(c[2]);
System.out.println(c[3]);
方法三:

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:[Java 教程 00] 计算机基础

下一篇:spring,springmvc,mybatis整合ssm框架出现ORA-02289:序列不存