zoukankan      html  css  js  c++  java
  • Java——数组

    java中拥有大量的其他方式,可以用于持有对象,但是数组与其他容器最大的区别就是:

    • 效率 

      在java中数组是效率最高的存储和随机访问对象引用序列的方式。

    • 类型

      数组可以持有基本类型,但是使用泛型之前的容器不能。在使用泛型之前,其他容器在处理对象的时候,都将它们视作根类object处理。在创建一个数组的时候就已经确定了持有某种具体类型

    • 大小

      数组的大小在创建的时候就已经确定了,但是容器的大小不是固定的。就比如ArrayList通过创建一个新的实例,然后将旧实例中的所有的引用移到新实例中,从而实现更多空间的分配。

    一、数组是第一级对象

      无论使用那种类型的数组,数组标识符其实只是一个引用,指向在堆中创建的一个真实对象。

      数组可以分为对象数组和类型数组,这两种数组在使用上几乎是相同的,唯一的区别就是对象数组保存的是引用,基本类型直接保存基本类型的值。

    import java.util.Arrays;
    
    class fruit{
        static long counter = 0;
        final long id = counter++;
        String name;
        public String toString() {
            return "fruit" + id;    
        }
    }
    
    public class Class15 {
        public static void main(String[] args) {
            fruit[] f = new fruit[5];
            System.out.println(Arrays.toString(f));
            for(int i = 0; i < f.length; i++) {
                f[i] = new fruit();
            }
            System.out.println(Arrays.toString(f));
            int[] i = new int[5];
            System.out.println(Arrays.toString(i));
            
        }
    }

    输出:

    [null, null, null, null, null]
    [fruit0, fruit1, fruit2, fruit3, fruit4]
    [0, 0, 0, 0, 0]

    新生成一个对象数组的时候,其中所有地点引用都自动初始化为null,并没有存有对象,但是基本类型数组会存入基本类型的默认值。

    二、返回一个数组

      如果你想写一个函数返回一个数组,在C和C++函数返回的是指向这个数组的指针。

      在java中,就可以直接返回一个数组,这一部分是由垃圾回收器负责的当你需要它的时候它将一直存在,使用完之后就会被清理。

    class fruit{
        static long counter = 0;
        final long id = counter++;
        String name;
        public String toString() {
            return "fruit" + id;    
        }
        static fruit[] getfruit(int i) {
            fruit[] results = new fruit[i];
            for(int j = 0; j < i; j++) {
                results[j] = new fruit();
            }
            return results;
        }
    }
    
    public class Class15 {
        public static void main(String[] args) {
            fruit[] f = fruit.getfruit(10);
            for(int j = 0; j < f.length; j++) {
                System.out.print(f[j].toString() + ",");
            }        
        }
    }

    输出:

    fruit0,fruit1,fruit2,fruit3,fruit4,fruit5,fruit6,fruit7,fruit8,fruit9,

    三、多维数组。

    public class Class15 {
        public static void main(String[] args) {
            int[][] a = {
                    {1,2,3},
                    {4,5,6},
            };
            System.out.println(Arrays.deepToString(a));
            int[][] b = new int[2][3];
            System.out.println(Arrays.deepToString(b));
            int[][][] c = new int[2][3][4];
            System.out.println(Arrays.deepToString(c));        
        }
    }

    Arrays.deepToString()可以将多维数组转换成多个String。

    输出:

    [[1, 2, 3], [4, 5, 6]]
    [[0, 0, 0], [0, 0, 0]]
    [[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]

    可以看到int[][] b = new int[x][y],就是x行y列的矩阵,int[][][] c = new int[x][y][z]就是x个y行z列的矩阵。

    数组从构成矩阵的每一个向量都是可以具有任意的长度(被称作粗糙数组)

    public class Class15 {
        public static void main(String[] args) {
            Random rand = new Random(47);
            int[][][] a = new int[rand.nextInt(5)][][];
            for(int i = 0; i < a.length; i++) {
                a[i] = new int[rand.nextInt(5)][];
                for(int j = 0; j < a[i].length; j++) {
                    a[i][j] = new int[rand.nextInt(5)];
                }
            }
            System.out.println(Arrays.deepToString(a));
        }
    }

    输出:

    [[], [[0], [0], [0, 0, 0, 0]], [[], [0, 0], [0, 0]]]

    四、Arrays中的一些实用方法

    • equals()用于比较两个数组是否相等(deepEquals()用于多维数组),
    • fill()用于填充数组,
    • sort()用于数组排序,
    • binarySearch()用于在已经排序的数组从查找元素,
    • toString()用于产生数组的String表示,hashCode用于产生数组的散列码。
    public class Class15 {
        public static void main(String[] args) {
            Random rand = new Random();
            int[] i = new int[5];
            Arrays.fill(i, 1);
            System.out.println("i" + Arrays.toString(i));
            int[] j = new int[7];
            Arrays.fill(j, 2);
            System.out.println("j" + Arrays.toString(j));
            System.out.println("i、j对比结果:" + Arrays.equals(i, j));
            
            System.arraycopy(i, 0, j, 0, i.length);
            System.out.println("j" + Arrays.toString(j));
            for(int k = 0; k < j.length; k++) {
                j[k] = rand.nextInt(10);
            }
            System.out.println("j" + Arrays.toString(j));
            Arrays.sort(j);
            System.out.println("j排序后" + Arrays.toString(j));
            int loc = Arrays.binarySearch(j, 1);
            System.out.println("j[" + loc + "]=1");
        }
    }

    输出:

    j[2, 2, 2, 2, 2, 2, 2]
    i、j对比结果:false
    j[1, 1, 1, 1, 1, 2, 2]
    j[5, 1, 6, 3, 7, 3, 9]
    j排序后[1, 3, 3, 5, 6, 7, 9]
    j[0]=1
    
    
  • 相关阅读:
    【Nodejs】cheerio简单示例
    【复习】请求转发与请求重定向的区别:
    常用的 default.properties 文件 + 常用的 struts-default.xml 文件 + 常用的 struts-plugin.xml 文件 + 常用的 struts.xml 文件 + 常用的 struts.properties文件 + 常用的 web.xml 文件
    log4j WARN 的解决办法
    Caused by: 元素类型为 "package" 的内容必须匹配 "(result-types?,interceptors?,default-interceptor-ref?,default-action-ref?,default-class-ref?,global-results?,global-exception-mappings?,action*)"。
    Myeclipse2017无法修改项目的Web Context Root问题
    springMVC的注解@RequestParam与@PathVariable的区别
    easyUI的控件
    easyui datagrid json 格式
    java泛型中<?>和<T>有什么区别?
  • 原文地址:https://www.cnblogs.com/xxbbtt/p/7750056.html
Copyright © 2011-2022 走看看