码迷,mamicode.com
首页 > 编程语言 > 详细

Java 数组声明的几种方式

时间:2018-03-08 00:07:58      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:java数组   amp   sed   code   static   src   代码   声明   alt   

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]);
方法三:

 

Java 数组声明的几种方式

标签:java数组   amp   sed   code   static   src   代码   声明   alt   

原文地址:https://www.cnblogs.com/aihuadung/p/8525267.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!