• 【数组拷贝+二维数组遍历】



    前言

    数组的拷贝

    被拷贝的数组长度最重要
    原理:创建一个新数组,把原数组的元素放进新数组里,

    第一种方式:

    public class Test {
        public static void main(String[] args) {
            int[] arrays1={1,4,3,6,7};
            int[] copy=new int[arrays1.length];//新数组的长度要等于原数组的长度
    
            for (int i = 0; i < arrays1.length; i++) {
                copy[i]=arrays1[i];
            }
            System.out.println(Arrays.toString(copy));
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    第二种方式:利用工具类拷贝

    Arrays.copyOf(被拷贝的数组名,数组长度)

    //利用工具类进行拷贝
    public class Test {
        public static void main(String[] args) {
            int[] array ={2,4,6,7,8};
            //要传原数组名+原数组名长度再赋值给新数组
            int[] copy=Arrays.copyOf(array,array.length);//工具类需要用一个数组接收
            System.out.println(Arrays.toString(copy));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Arrays.copyOfRange(数组名,拷贝数组的起始下标,拷贝数组的结束下标);

        //比如我要拷贝array数组的0下标到4下标
    public class Test {
        public static void main(String[] args) {
            int[] array={12,3,7,19,88,77};
            int[] copy=Arrays.copyOfRange(array,0,4);//[0,4)
            System.out.println(Arrays.toString(copy));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    arraycopy(原数组名,拷贝原数组的哪个下标,新数组名,拷贝到新数组的哪个下标,原数组的长度)
    题目练习:
    在这里插入图片描述

    //需要初始化新数组

    
    public class Test {
        public static void main(String[] args) {
            int[] array={2,4,6,8,10};
            int[] copy=new int[array.length];//初始化一个新数组
            System.arraycopy(array,0,copy,0,array.length);//5个参数
            System.out.println(Arrays.toString(copy));//打印
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    二维数组

    二维数组的三种定义

    public class Test {
        public static void main(String[] args) {
            int[][] array1={{1,2,3},{4,5,6}};
            int[][] array2=new int[][]{{1,2,3},{4,5,6}};
            //不规则二维数组:
            int[][] array3=new int[2][];//行不能省略
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    打印二维数组的某个元素

    //二维数组的打印
    public class Test {
        public static void main(String[] args) {
            int[][] array = {{1,2,3},{4,5,6}};
            System.out.println(array[1][1]);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    遍历二维数组

    public class Test {
        public static void main(String[] args) {
            int[][] array={{1,2,3},{6,7,8}};
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 3; j++) {
                    System.out.print(array[i][j] +" ");
                }
                System.out.println();
                
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    二维数组的每个元素是一维数组

    在这里插入图片描述

            System.out.println(array.length);//二维数组有几个一维数组
            System.out.println(array[0].length);//第一个一维数组中有几个元素
            System.out.println(array[1].length);//第二个一维数组中有几个元素
    
    • 1
    • 2
    • 3

    遍历二维数组(优化)

    public class Test {
        public static void main(String[] args) {
            int[][] array={{1,2,3},{4,5,6}};
            
            for (int i = 0; i < array.length ; i++) {
                for (int j = 0; j < array[i].length; j++) {
                    System.out.print(array[i][j]+" ");
                }
                System.out.println();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    打印出一个数组(不是数组元素)的方法

    public class Test {
        public static void main(String[] args) {
            int[][] array={{1,2,3},{4,5,6}};
            System.out.println(Arrays.deepToString(array));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    总结

    数组的复习就到这里,接下来是类和对象,得抓紧时间复习了,落下好多课程,这两天没有更新是因为去完成了一件有意义的事,心情放松了很多,这个周末和好朋友在一起是开学以来最开心的事了。

  • 相关阅读:
    VSCode设置成中文语言环境
    postgresql 15源码浅析(5)—— pg_control
    【C语言】const 关键字
    我的idea安装的几个插件
    进阶JS-去重
    zxing详细使用说明 java生成二维码、条形码
    mongoose 应用程序开发流程
    PMP每日一练 | 考试不迷路-11.07(包含敏捷+多选)
    线性表--顺序表-1
    设计模式 --单例模式
  • 原文地址:https://blog.csdn.net/2301_76496134/article/details/133927700