Java中数组介绍

//一维数组

     public class Star{
    	public static void main(String[] args) {
        /*//1,数组的声明和初始化
        //静态数组  直接指定数组的元素
        int[] inds;
        inds = new int[]{1,2,3,4,5};
        //动态数组  指定数组的长度
        int[] inds1;
        inds1 = new int[3];
        //2,调用数组的索引 指定数组的元素
        inds1[0] = 1;
        inds1[1] = 3;
        inds1[2] = 3;
        //3,获取数组的长度
        System.out.println(inds.length);
        //4,遍历数组
        for(int i = 0;i< inds.length;i++){
            System.out.println(inds[i]);
        }
        //5,数组元素的默认初始化值
        //整型 默认值为0
        //浮点型  默认值0.0
        //char 类型  默认值为空
        //布尔型 默认值为false
        //引用数据类型 默认值为null
        public class Star{
    		public static void main(String[] args) {
		        String[] inds = new String[2];
		        for(int i = 0;i<inds.length;i++){
		            System.out.println(inds[i]);
		        }
		        char[] ind = new char[2];
		        //遍历数组数据 输入 ind.iter
		        for (char c : ind) {
		            //然后直接打印c 就是数组中的所有数据
		            System.out.println(c);

//二维数组

public class Star{
    public static void main(String[] args) {
        //二维数组 静态
        int[][] nums = new int[][]{{1,2,3},{3,4,5},{1,2}};
        //调取指定位置的元素
        System.out.print(nums[0][1]);//2
        System.out.println("--------");
        //二维数组 动态
        int[][] nums1 =  new int[2][2];
        //未赋值时可使用此方法 获取元素默认初始值
        nums1[0] = new int[3];
        System.out.println(nums1[0][0]);
        //获取二维元素的长度
        System.out.println(nums.length);//3
    }}

//二维数组的遍历

public class Star{
    public static void main(String[] args) {
        //二维数组 静态
        int[][] nums = new int[][]{{1,2,3},{3,4,5},{1,2}};
        //二维数组的遍历  先遍历外圈数组 nums.iter 然后再遍历里面的小数组 num.iter
        for (int[] num : nums) {
            for (int i : num) {
                System.out.println(i);
            }
        }
    }}

//数组元素的默认初始值

		int[][] ind = new int[4][3]
		外层元素的初始化值为:地址值
		内层元素的初始化值同一维数组的数据类型
		int[][] inds = new int[2][]
		外层元素的初始化值为null
		则内层元素调用会报空指针异常

//打印10行杨辉三角

public class Star{
    public static void main(String[] args) {
        //打印10行杨辉三角
        //定义二维数组
        int[][] arr = new int[10][];
        //给元素赋值,先赋值一维数组
        for(int i = 0;i < arr.length;i++){
            arr[i] = new int[i+1];
            //首末元素都为1
            arr[i][0] = 1;
            arr[i][i] = 1;
            //非首末元素赋值;
            for(int j = 1;j < arr[i].length-1;j++){
                arr[i][j] = arr[i-1][j-1] + arr[i-1][j] ;
            }
            }
        //遍历打印
        for(int i = 0;i<arr.length;i++){
            for(int j = 0;j < arr[i].length;j++){
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值