java一维数组

数组的定义

数组是存储变量的空间.

数组是一个变量,存储相同类型的一组数据.

一维数组的格式

数组基本要素
标识符
数组元素
元素下标:从 0 开始
元素类型

三种格式

        //数据类型[ ]  数组名   =   new   数据类型[大小]  ;
        int[] a = new int[2];
        //数据类型[] 数组名 = new 数据类型[]{内容 1,内容 2,内容 3…内容 n};
        int[] b = new int[]{16,5,7,5};
        //数据类型[] 数组名 ={内容 1,内容 2,内容 3…内容 n};
        int[] c ={1,3,4,5,6,8};

数组元素根据类型不同,有不同的初始值


public class Demo4 {
    public static void main(String[] args) {
        //整型
        byte[] a1=new byte[2];
        short[] a2=new short[2];
        int[] a3= new int[2];
        long[] a4=new long[2];
        //浮点型
        float[] b1=new float[2];
        double[] b2=new double[2];
        //布尔型
        boolean[] c1=new boolean[2];
        //字符型
        char[] d1=new char[2];
        //字符串型
        String[] e1=new String[2];
        System.out.println(a1[0]);
        System.out.println(a2[0]);
        System.out.println(a3[0]);
        System.out.println(a4[0]);
        System.out.println(b1[0]);
        System.out.println(b2[0]);
        System.out.println(c1[0]);
        System.out.println(d1[0]);
        System.out.println(e1[0]);

    }
}

 

 增强for循环遍历

for(数据类型 变量名 :遍历的目标){ //数据类型 变量名:声明一个变量用来接收遍历目标遍历后的元素}

public class Demo5 {
    public static void main(String[] args) {
        int[] a= new int[]{12,44,557,88,33,88};
        for (int b:
             a) {
            System.out.println(b);
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值