:::info JAVA课程中讲到数组这类数据结构的笔记,做此记录 :::

一维数组

创建

  1. double [] array; // 声明数组方式一
  2. double array []; // 生命数组方式二

创建好编译器会有初始值
JAVA数组学习笔记 - 图1

初始化

  1. double [] array = new double[10]; // 生命数组并且初始化数组大小
  2. double [] array = {1.9, 2.9, 3.4, 3.5}; // 用值初始化数组

相关属性

是一个对象,有自己的属性自己的方法

String里面长度length有(),也就是String里面是方法,而数组没有括号()(写成array.length),理解为成员变量/属性,而不是方法(封装好了许多功能,体现出语言优越性)

相关操作

  • [x] Initializing arrays with input values

    1. java.util.Scanner input = new java.util.Scanner(System.in);
    2. for (int i=0;i<mylist.length;i++)
    3. mylist[i] = input.nextDouble();
  • [x] Initializing arrays with random values

    1. for (int i = 0; i < myList.length; i++) {
    2. myList[i] = Math.random() * 100;
    3. }
  • [x] Printing arrays

    1. for (int i = 0; i < myList.length; i++) {
    2. System.out.print(myList[i] + " ");
    3. }
  • [x] Summing all elements

    1. double total = 0;
    2. for (int i = 0; i < myList.length; i++) {
    3. total += myList[i];
    4. }
  • [x] Finding the largest element

    1. double max = myList[0];
    2. for (int i = 1; i < myList.length; i++) {
    3. if (myList[i] > max) max = myList[i];
    4. }
  • [ ] Finding the smallest index of the largest element

  • Random shuffling

JAVA数组学习笔记 - 图2

  • Shifting elements

JAVA数组学习笔记 - 图3

数组的值传递

java核心知识点之一

数组名(数组地址)作为函数参数

java当中是值传递的,没有引用没有指针的概念,所以在做函数参数当中,一直都是值传递,python也是。

Array类

import java.util.Arrays;

  • Array类中常用的函数

    sort()、parallelSort()、fill()、toString()、binarySearch()(Binary Search二分查找(事先得排好序))、equals()

JAVA数组学习笔记 - 图4
JAVA数组学习笔记 - 图5

多维数组

二维数组的创建、初始化

  1. dataType[][] refVar = new dataType[10][10];
  2. int[][] array = {
  3. {1, 2, 3},
  4. {4, 5, 6},
  5. {7, 8, 9},
  6. {10, 11, 12}
  7. };

数组长度的计算

  1. array.length = 4
  2. array[0].length = 3
  3. array[4].length ==> ArrayIndexOutOfBoundsException

二维数组的相关操作

  • [x] Initializing arrays with input values

    1. java.util.Scanner input = new Scanner(System.in);
    2. System.out.println("Enter " + matrix.length + " rows and " +
    3. matrix[0].length + " columns: ");
    4. for (int row = 0; row < matrix.length; row++) {
    5. for (int column = 0; column < matrix[row].length; column++) {
    6. matrix[row][column] = input.nextInt();
    7. }
    8. }
  • [x] Initializing arrays with random values

    1. for (int row = 0; row < matrix.length; row++) {
    2. for (int column = 0; column < matrix[row].length; column++) {
    3. matrix[row][column] = (int)(Math.random() * 100);
    4. }
    5. }
  • [x] Printing arrays

    1. for (int row = 0; row < matrix.length; row++) {
    2. for (int column = 0; column < matrix[row].length; column++) {
    3. System.out.print(matrix[row][column] + " ");
    4. }
    5. System.out.println();
    6. }
  • [x] Summing all elements

    1. int total = 0;
    2. for (int row = 0; row < matrix.length; row++) {
    3. for (int column = 0; column < matrix[row].length; column++) {
    4. total += matrix[row][column];
    5. }
    6. }
  • [x] Summing all elements by column

    1. for (int column = 0; column < matrix[0].length; column++) {
    2. int total = 0;
    3. for (int row = 0; row < matrix.length; row++)
    4. total += matrix[row][column];
    5. System.out.println("Sum for column " + column + " is "
    6. + total);
    7. }
  • [x] Which row has the largest sum

  • Finding the smallest index of the largest element
  • Random shuffling
    1. for (int i = 0; i < matrix.length; i++) {
    2. for (int j = 0; j < matrix[i].length; j++) {
    3. int i1 = (int)(Math.random() * matrix.length);
    4. int j1 = (int)(Math.random() * matrix[i].length);
    5. // Swap matrix[i][j] with matrix[i1][j1]
    6. int temp = matrix[i][j];
    7. matrix[i][j] = matrix[i1][j1];
    8. matrix[i1][j1] = temp;
    9. }
    10. }

多维数组

  1. double[][][] scores = {
  2. {{7.5, 20.5}, {9.0, 22.5}, {15, 33.5}, {13, 21.5}, {15, 2.5}},
  3. {{4.5, 21.5}, {9.0, 22.5}, {15, 34.5}, {12, 20.5}, {14, 9.5}},
  4. {{6.5, 30.5}, {9.4, 10.5}, {11, 33.5}, {11, 23.5}, {10, 2.5}},
  5. {{6.5, 23.5}, {9.4, 32.5}, {13, 34.5}, {11, 20.5}, {16, 7.5}},
  6. {{8.5, 26.5}, {9.4, 52.5}, {13, 36.5}, {13, 24.5}, {16, 2.5}},
  7. {{9.5, 20.5}, {9.4, 42.5}, {13, 31.5}, {12, 20.5}, {16, 6.5}}
  8. };