zoukankan      html  css  js  c++  java
  • Java 数组声明的几种方式

    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]);
    方法三:
  • 相关阅读:
    [Windows内核分析]KPCR结构体介绍 (CPU控制区 Processor Control Region)
    利用C++实现模块隐藏(R3层断链)
    [动态规划]最少硬币问题
    [动态规划]高数Umaru系列(9)——哈士奇(背包问题)
    Windows中利用共享内存来实现不同进程间的通信
    [分治算法]骨牌铺方格
    [分治、递推法、动态规划]最大子段和
    [分治算法]因式分解
    013 CephFS文件系统
    012 Ceph多区域网关
  • 原文地址:https://www.cnblogs.com/aihuadung/p/8525267.html
Copyright © 2011-2022 走看看