JAVA基础学习-第六讲 数组

242 阅读7分钟

JAVA基础教程

学习JAVA网站 : how2j

第六讲 数组

6.1 创建数组

数组是一个固定长度的,包含了相同类型数据的 容器

  • 声明数组

int[] a; 声明了一个数组变量。但是,仅仅是这一句声明,不会创建数组

有时候也会写成int a[]; 没有任何区别,就是你看哪种顺眼的问题。

public class HelloWorld {
    public static void main(String[] args) {
        // 声明一个数组
        int[] a;
    }
}
  • 创建数组

创建数组的时候,要指明数组的长度。new int[5]

如果变量代表一个数组,比如a,我们把a叫做引用

public class HelloWorld {
    public static void main(String[] args) {
        //声明一个引用
        int[] a;
        //创建一个长度是5的数组,并且使用引用a指向该数组
        a = new int[5];
         
        int[] b = new int[5]; //声明的同时,指向一个数组
         
    }
}
  • 访问数组

数组下标基0。下标0,代表数组里的第一个数。

  • 数组长度

.length属性用于访问一个数组的长度,数组访问下标范围是0到长度-1。一旦超过这个范围,就会产生数组下标越界异常。

public class HelloWorld {
    public static void main(String[] args) {
        int[] a;
        a = new int[5];
        System.out.println(a.length); //打印数组的长度
         
        a[4]=100; //下标4,实质上是“第5个”,即最后一个
        a[5]=101; //下标5,实质上是“第6个”,超出范围 ,产生数组下标越界异常
         
    }
}

6.2 初始化数组

  • 分配空间与赋值分步进行

分配空间与赋值分步进行

public class HelloWorld {
    public static void main(String[] args) {
        int[] a = new int[5]; //分配了长度是5的数组,但是没有赋值

        //没有赋值,那么就会使用默认值
        //作为int类型的数组,默认值是0
        System.out.println(a[0]);
         
        //进行赋值
        a[0] = 100;
        a[1] = 101;
        a[2] = 103;
        a[3] = 120;
        a[4] = 140;
    }
}
  • 分配空间,同时赋值
public class HelloWorld {
    public static void main(String[] args) {
        //写法一: 分配空间同时赋值
        int[] a = new int[]{100,102,444,836,3236};
 
        //写法二: 省略了new int[],效果一样
        int[] b = {100,102,444,836,3236};
         
        //写法三:同时分配空间,和指定内容
        //在这个例子里,长度是3,内容是5个,产生矛盾了
        //所以如果指定了数组的内容,就不能同时设置数组的长度
        int[] c = new int[3]{100,102,444,836,3236};
         
    }
}

6.3 排序

  • 选择法排序

选择法排序的思路:

把第一位和其他所有的进行比较,只要比第一位小的,就换到第一个位置来。比较完后,第一位就是最小的。 然后再从第二位和剩余的其他所有进行比较,只要比第二位小,就换到第二个位置来;比较完后,第二位就是第二小的

public class HelloWorld {
    public static void main(String[] args) {
        int a [] = new int[]{18,62,68,82,65,9};
        //可以发现一个规律
        //移动的位置是从0 逐渐增加的
        //所以可以在外面套一层循环     
        for (int j = 0; j < a.length-1; j++) {
            for (int i = j+1; i < a.length; i++) {
                if(a[i]<a[j]){  
                    int temp = a[j];
                    a[j] = a[i];
                    a[i] = temp;
                }
            }
        }
        //把内容打印出来
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println(" ");       
    }
}
  • 冒泡法排序

冒泡法排序的思路: 第一步:从第一位开始,把相邻两位进行比较,如果发现前面的比后面的大,就把大的数据交换在后面,循环比较完毕后,最后一位就是最大的。 第二步: 再来一次,只不过不用比较最后一位。 以此类推

public class HelloWorld {
    public static void main(String[] args) {
        int a [] = new int[]{18,62,68,82,65,9};
        //可以发现一个规律
        //后边界在收缩
        //所以可以在外面套一层循环
        for (int j = 0; j < a.length; j++) {
            for (int i = 0; i < a.length-j-1; i++) {
                if(a[i]>a[i+1]){  
                    int temp = a[i];
                    a[i] = a[i+1];
                    a[i+1] = temp;
                }
            }
        }
        //把内容打印出来
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println(" ");       
    }
}

6.4 增强型for循环

public class HelloWorld {
    public static void main(String[] args) {
        int values [] = new int[]{18,62,68,82,65,9};
        //常规遍历
        for (int i = 0; i < values.length; i++) {
            int each = values[i];
            System.out.println(each);
        }
         
        //增强型for循环遍历
        for (int each : values) {
            System.out.println(each);
        } 
    }
}

6.5 复制数组

数组的长度是不可变的,一旦分配好空间,是多长,就多长,不能增加也不能减少。

  • 复制数组

把一个数组的值,复制到另一个数组中:

System.arraycopy(src, srcPos, dest, destPos, length)

src: 源数组 srcPos: 从源数组复制数据的起始位置 dest: 目标数组 destPos: 复制到目标数组的起始位置 length: 复制的长度

public class HelloWorld {
    public static void main(String[] args) {
        int a [] = new int[]{18,62,68,82,65,9};
        int b[] = new int[3];//分配了长度是3的空间,但是没有赋值
         
        //通过数组赋值把,a数组的前3位赋值到b数组
        //方法一: for循环
        for (int i = 0; i < b.length; i++) {
            b[i] = a[i];
        }
        
        //方法二: System.arraycopy(src, srcPos, dest, destPos, length)
        //src: 源数组
        //srcPos: 从源数组复制数据的起始位置
        //dest: 目标数组
        //destPos: 复制到目标数组的启始位置
        //length: 复制的长度       
        System.arraycopy(a, 0, b, 0, 3);
         
        //把内容打印出来
        for (int i = 0; i < b.length; i++) {
            System.out.print(b[i] + " ");
        }
    }
}

6.6 二维数组

这是一个一维数组, 里面的每一个元素,都是一个基本类型int

int a[] =new int[]{1,2,3,4,5};

这是一个二维数组,里面的每一个元素,都是一个一维数组;所以二维数组又叫数组的数组

int b[][] = new int[][]{
   {1,2,3},
   {4,5,6},
   {7,8,9}
};
  • 初始化二维数组
public class HelloWorld {
    public static void main(String[] args) {
       //初始化二维数组
       int[][] a = new int[2][3]; //有两个一维数组,每个一维数组的长度是3
       a[1][2] = 5;  //可以直接访问一维数组,因为已经分配了空间
          
       //只分配了二维数组
       int[][] b = new int[2][]; //有两个一维数组,每个一维数组的长度暂未分配
       b[0]  = new int[3]; //必须事先分配长度,才可以访问
       b[0][2] = 5;
        
       //指定内容的同时,分配空间
       int[][] c = new int[][]{
               {1,2,4},
               {4,5},
               {6,7,8,9}
       };
    }
}

6.7 Arrays

Arrays是针对数组的工具类,可以进行 排序,查找,复制填充等功能。 大大提高了开发人员的工作效率。

image.png

  • 数组复制 CopyofRange

使用System.arraycopy进行数组复制类似的, Arrays提供了一个copyOfRange方法进行数组复制。 不同的是System.arraycopy,需要事先准备好目标数组,并分配长度。 copyOfRange 只需要源数组就就可以了,通过返回值,就能够得到目标数组了。 除此之外,需要注意的是 copyOfRange 的第3个参数,表示源数组的结束位置,是取不到的

import java.util.Arrays;
 
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
 
        // copyOfRange(int[] original, int from, int to)
        // 第一个参数表示源数组
        // 第二个参数表示开始位置(取得到)
        // 第三个参数表示结束位置(取不到)
        int[] b = Arrays.copyOfRange(a, 0, 3);
 
        for (int i = 0; i < b.length; i++) {
            System.out.print(b[i] + " ");
        }
 
    }
}
  • 转换为字符串

如果要打印一个数组的内容,就需要通过for循环来挨个遍历,逐一打印

但是Arrays提供了一个toString()方法,直接把一个数组,转换为字符串,这样方便观察数组的内容

import java.util.Arrays;
  
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
        String content = Arrays.toString(a);
        System.out.println(content);
  
    }
}
  • 排序

Arrays工具类提供了一个sort方法,只需要一行代码即可完成排序功能。

import java.util.Arrays;
  
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
        System.out.println("排序之前 :");
        System.out.println(Arrays.toString(a));
        Arrays.sort(a);
        System.out.println("排序之后:");
        System.out.println(Arrays.toString(a));
  
    }
}
  • 搜索

查询元素出现的位置:需要注意的是,使用binarySearch进行查找之前,必须使用sort进行排序. 如果数组中有多个相同的元素,查找结果是不确定的.

import java.util.Arrays;
 
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
 
        Arrays.sort(a);
 
        System.out.println(Arrays.toString(a));
        //使用binarySearch之前,必须先使用sort进行排序
        System.out.println("数字 62出现的位置:"+Arrays.binarySearch(a, 62));
    }
}
  • 判断是否相同

比较两个数组的内容是否一样 第二个数组的最后一个元素是8,和第一个数组不一样,所以比较结果是false

import java.util.Arrays;
 
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
        int b[] = new int[] { 18, 62, 68, 82, 65, 8 };
 
        System.out.println(Arrays.equals(a, b));
    }
}
  • 填充
import java.util.Arrays;
  
public class HelloWorld {
    public static void main(String[] args) {
        int a[] = new int[10];
  
        Arrays.fill(a, 5);
        System.out.println(Arrays.toString(a));
  
    }
}