java中的数组

一、数组的概念

  数组是同种类型数据的集合,数组是引用数据类型,也就是说数组的初始化需要在内存中开辟空间,即需要new关键字。

二、数组的初始化

  数组的初始化分为两种:动态初始化、静态初始化

  1.动态初始化:

    格式为:数据类型[] 数组名称  =  new 数据类型[数组长度];

    例如:int[] array = new int[6]; 表示声明一个长度为6的int类型的数组,元素的默认值为0(元素的默认值为对应数据类型的默认值)

  2.静态初始化:

    格式为:数据类型[] 数组名称 = new 数据类型[]{数组元素,数组元素,数组元素...}

    例如:int[] array = new int[]{1,2,3,4,5,6}; 表示声明一个数组,并且存储着元素1,2,3,4,5,6

三、数组的使用

  1.数组的访问

    数组是线性结构的,所以我们可以通过数组的下标访问任意一个位置的元素,注意:元素的下标是从0开始的,即你要访问第3个元素,下标就是2,如果使用的数组的下标

  大于等于数组的长度(下标也不能为负数),在程序运行时就会报错,错误为数组越界异常,如下图所示:

  

   2.数组的遍历

    数组可以通过for循环进行遍历,既可以使用普通for循环,又可以使用高级for循环。

 public static void main(String[] args) {
        int[] array = new int[]{1,2,3,4,5,6,7,8,9};
        System.out.print("普通for循环遍历:");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+"、");
        }
    }

  输出结果:

  

public static void main(String[] args) {
        int[] array = new int[]{1,2,3,4,5,6,7,8,9};
        System.out.print("高级for循环遍历:");
        for (int i : array) {
            System.out.print(i+"、");
        }
    }

  输出结果:

  

   3.数组相关的方法

    (1)数组中的元素排序(使用冒泡排序)

public static void main(String[] args) {
        int[] array = new int[]{9,6,3,5,4,1,7,2,8};
        //进行排序、冒泡排序
        int length = array.length;
        for (int i = 0; i < length - 1; i++) {
            for (int j = 0; j < length -1 - i; j++) {
                if (array[j] > array[j+1]){
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }
            }
        }
        System.out.print("从低到高排序后的数组:");
        for (int i : array) {
            System.out.print(i+"、");
        }
    }

  输出结果:

  

    (2)数组中的元素反转(第一种方式,该方式会产生垃圾,产生的垃圾的原因是因为需要先创建一个新数组,然后将新数组倒序遍历依次赋值,最后将原来数组变量的引用指向新数组,原来的堆       空间的地址没有数组变量引用了,所以就产生了垃圾)

public static void main(String[] args) {
        int[] array = new int[]{1,2,3,4,5,6,7,8,9};
        //创建一个相等长度的数组
        int[] temp = new int[array.length];
        //对要反转的数组进行遍历并对新数组进行赋值
        for (int i = 0; i < array.length; i++) {
            temp[array.length -1 - i] = array[i];
        }
        array = temp;
        System.out.print("反转后的数组:");
        for (int i : array) {
            System.out.print(i+"、");
        }
    }

  输出结果:

  

     (3)数组中的元素反转(第二种方式,这种方式不会创建新的数组,也不会产生垃圾)

public static void main(String[] args) {
        int[] array = new int[]{1,2,3,4,5,6,7,8,9};
        int length = array.length;
        for (int i = 0; i < length/2; i++) {
            int temp = array[i];
            array[i] = array[length-1-i];
            array[length-1-i] = temp;
        }
        System.out.print("反转后的数组:");
        for (int i : array) {
            System.out.print(i+"、");
        }
    }

  输出结果:

  

  4.对象数组

    一般来说,对象数组比较常用,下面举个例子:

class Car{

    //名称
    private String name;
    //车型
    private String type;

    //有参构造
    public Car(String name, String type) {
        this.name = name;
        this.type = type;
    }

    public void info(){
        System.out.println("汽车名称:"+name+",汽车车型:"+type);
    }
}
public class ArrayDemo {

    public static void main(String[] args) {
        Car[] carArray1 = new Car[3];
        System.out.println("未初始化数组输出:");
        for (Car car : carArray1) {
            System.out.println(car);
        }
        System.out.println("初始化数组后输出:");
        Car[] carArray2 = new Car[]{
                new Car("宝马","7系"),
                new Car("奔驰","S级"),
                new Car("奥迪","A9")
        };
        for (Car car : carArray2) {
            car.info();
        }
    }
}

  输出结果为:

  

  

原文地址:https://www.cnblogs.com/li666/p/11898833.html