zoukankan      html  css  js  c++  java
  • 数组

    二、数组

    1、一维数组

    1、数组的声明

    int[] array;
    int array1[];
    

    2、数组的定义

    /*数组的定义*/
    int[] array = {1,2,3,4};
    int[] array1 = new int[10];
    int[] array2 = new int[]{1,2,3,4};
    

    3、数组的长度

    int[] array1 = new int[10];
    int length = array1.length;
    

    4、声明和定义的区别

    声明就是告诉编译器,这个名字已经被我用了,它不能再出现在别的地方了。

    定义就是指编译器创建了一个对象,且为这个对象分配了内存。

    5、一维数组的拷贝

    一位数组的拷贝有四种方式:

    1. for循环拷贝
    2. clone拷贝
    3. System.arrayCopy()
    4. Arrays.copyOf()

    1、for循环拷贝

    浅拷贝,具体实现就不实现了。

    2、clone拷贝

    浅拷贝

    /*clone拷贝*/
    int[] a = {1,2,3,4,5};
    int[] b = a.clone();
    show(a);
    show(b);
    System.out.println("进行改变");
    b[0] = 6;
    show(a);
    show(b);
    
    Person[] p1 = new Person[3];
    for(int i = 0; i<p1.length; i++) {
        p1[i] = new Person();
    }
    
    Person[] p2 = p1.clone();
    show(p1);
    show(p2);
    System.out.println("进行改变---");
    p2[0].setAge(10);
    show(p1);
    show(p2);
    
    /*
    ----基本数据类型----
    1  2  3  4  5  
    1  2  3  4  5  
    进行改变
    1  2  3  4  5  
    6  2  3  4  5  
    ----引用类型----
    20  20  20  
    20  20  20  
    进行改变---
    10  20  20  
    10  20  20  
    */
    

    3、arraycopy()

    浅拷贝

            /*arraycopy()*/
            int[] a = {1,2,3,4,5};
            int[] b = new int[a.length];
            System.arraycopy(a,0,b,0,a.length);
            show(a);
            show(b);
            System.out.println("进行改变");
            b[0] = 6;
            show(a);
            show(b);
    /*
    1  2  3  4  5
    1  2  3  4  5
    进行改变
    1  2  3  4  5
    6  2  3  4  5  
    */
    

    4、copyOf()

    浅拷贝,copyOf底层采用arraycopy实现。

    /*copyOf()*/
    int[] a = {1,2,3,4,5};
    int[] b = Arrays.copyOf(a,a.length);
    show(a);
    show(b);
    System.out.println("进行改变");
    b[0] = 6;
    show(a);
    show(b);
    

    6、一位数组改变长度

    采用数组copyOf来返回一个新的数组改变其长度。

    2、二维数组

    1、二维数组的声明

    /*二维数组的声明*/
    int [][] a;
    int b[][];
    int []c[];
    

    2、二维数组的定义

    /*二维数组的定义
    * 二维数组的定义一定要表明行
    * 数组直接初始化的化必须和声明连在一起
    * */
    a = new int[3][];
    int d[][] = new int[][]{{1,2,3},{1,2,3}};
    /*b = {{1,2,3},{1,2,3},{1,2,3}};   //error
    int d[][]  = {{1,2,3},{1,2,3},{1,2,3}}; //right*/
    

    3、可变数组

    二维数组 长度可以不一致。

    /*可变数组*/
    int[][] a = new int[3][];
    int[] b1 = new int[1];
    int[] b2 = new int[2];
    int[] b3 = new int[3];
    a[0] = b1;
    a[1] = b2;
    a[2] = b3;
    

    4、二维数组的拷贝

    只需把二维数组当成一个若干一维数组组合的数组

    1、for循环拷贝

    不做演示

    2、clone

    /*clone*/
    /*int d[][]  = {{1,2,3},{1,2,3},{1,2,3}};
    int[][] e = d.clone();
    show(d);
    show(e);*/
    

    3、copyarray

    /*copyarray*/
    /*int d[][]  = {{1,2,3},{1,2,3},{1,2,3}};
    int[][] e = new int[3][];
    System.arraycopy(d,0,e,0,d.length);
    show(d);
    show(e);*/
    

    4、copyOf

    /*copyOf*/
    /*int d[][]  = {{1,2,3},{1,2,3},{1,2,3}};
    int[][] e = Arrays.copyOf(d,d.length);
    show(d);
    show(e);*/
    

    3、Arrays类

    列举了一些常用方法

    1、asList()

    Arrays内部有一个内部静态类ArrayList,该内部类区别于我们平常所使用的ArrayList,该内部类没有add、remove等方法。

    /*测试asList方法*/
    Integer[] a = new Integer[]{1,2,3,4,5};
    List<Integer> list = Arrays.asList(a);
    list.forEach(System.out::println);
    /*
    list.add(2);
    操作异常,因为Arrays.asList方法返回的是Arrays内的静态内部类,
    其没有实现add、remove方法。
    所以会调用其继承的AbstractList类的add方法,
    AbstractList类的add方法里面的add方法又显式的throw new UnsupportedOperationException();
    故不能进行该操作
    */
    
    /*要解决上述问题只需要把上述生成的list放在一个新的ArrayList中即可*/
    List<Integer> list1 = new ArrayList<Integer>(list);
    list1.add(8);
    list1.forEach(System.out::println);
    

    2、 fill()

    /*替换所有元素*/
    int[] a = new int[10];
    Arrays.fill(a,10);
    for (int i:a) {
        System.out.println(i);
    }
    
    System.out.println("---部分替换---");
    /*替换部分元素*/
    Arrays.fill(a,1,3,9);
    for (int i:a) {
        System.out.println(i);
    }
    

    3、sort()

    int[] a = {1,3,5,2,7,6,8};
    Arrays.sort(a);
    for(int i : a) {
        System.out.print(i+" ");
    }
    

    4、equals()

            Integer[] a = {1,2,3,4};
            Integer[] b = {1,2,3,4};
            System.out.println(a==b);
            System.out.println(a.equals(b));
            System.out.println(a.hashCode());
            System.out.println(b.hashCode());
            System.out.println(Arrays.equals(a,b));
            /*其实现是对数组内的元素逐个使用equals方法对比*/
            
            /*结果
            * false
    false
    460141958
    1163157884
    true
            * */
    

    5、hashCode()

    /*Arrays重写了hashCode方法,返回的是数组内元素hashcode之和加上某个数*/
    Integer[] a = {1,2,3,4,5,129};
    Integer[] b = {1,2,3,4,5,129};
    System.out.println(Arrays.hashCode(a));
    System.out.println(Arrays.hashCode(b));
    /*
    918073375
    918073375
    */
    

    6、toString()

            int[] a = {1,2,3,4,5};
            String[] s = {"nishi","zhuba"};
            System.out.println(Arrays.toString(a));
            System.out.println(a.toString());
            System.out.println(Arrays.toString(s));
    
            /*针对多为数组的toString*/
            int[][] b = {{1,2,3},{1,2,3,4}};
            System.out.println(Arrays.deepToString(b));
    /*
    * [1, 2, 3, 4, 5]
    [I@1b6d3586
    [nishi, zhuba]
    [[1, 2, 3], [1, 2, 3, 4]]
    * */
    

    4、参考资料

    Java-Arrays类常用方法详解

  • 相关阅读:
    递归和迭代
    The Rose
    读周国平作品有感
    matlab最小二乘法数据拟合函数详解
    读周国平作品有感
    three.js之创建一条直线
    three.js之创建一个几何体
    Go语言标准库之strconv
    Go语言基础之网络编程
    Go语言基础之并发
  • 原文地址:https://www.cnblogs.com/dearcabbage/p/10814729.html
Copyright © 2011-2022 走看看