:::info JAVA课程中讲到数组这类数据结构的笔记,做此记录 :::
一维数组
创建
double [] array; // 声明数组方式一
double array []; // 生命数组方式二
初始化
double [] array = new double[10]; // 生命数组并且初始化数组大小
double [] array = {1.9, 2.9, 3.4, 3.5}; // 用值初始化数组
相关属性
是一个对象,有自己的属性自己的方法
String里面长度length有(),也就是String里面是方法,而数组没有括号()(写成array.length),理解为成员变量/属性,而不是方法(封装好了许多功能,体现出语言优越性)
相关操作
[x] Initializing arrays with input values
java.util.Scanner input = new java.util.Scanner(System.in);
for (int i=0;i<mylist.length;i++)
mylist[i] = input.nextDouble();
[x] Initializing arrays with random values
for (int i = 0; i < myList.length; i++) {
myList[i] = Math.random() * 100;
}
[x] Printing arrays
for (int i = 0; i < myList.length; i++) {
System.out.print(myList[i] + " ");
}
[x] Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
[x] Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) max = myList[i];
}
[ ] Finding the smallest index of the largest element
- Random shuffling
- Shifting elements
数组的值传递
java核心知识点之一
数组名(数组地址)作为函数参数
java当中是值传递的,没有引用没有指针的概念,所以在做函数参数当中,一直都是值传递,python也是。
Array类
import java.util.Arrays;
- Array类中常用的函数
sort()、parallelSort()、fill()、toString()、binarySearch()(Binary Search二分查找(事先得排好序))、equals()
多维数组
二维数组的创建、初始化
dataType[][] refVar = new dataType[10][10];
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
数组长度的计算
array.length = 4
array[0].length = 3
array[4].length ==> ArrayIndexOutOfBoundsException
二维数组的相关操作
[x] Initializing arrays with input values
java.util.Scanner input = new Scanner(System.in);
System.out.println("Enter " + matrix.length + " rows and " +
matrix[0].length + " columns: ");
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
matrix[row][column] = input.nextInt();
}
}
[x] Initializing arrays with random values
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
matrix[row][column] = (int)(Math.random() * 100);
}
}
[x] Printing arrays
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
System.out.print(matrix[row][column] + " ");
}
System.out.println();
}
[x] Summing all elements
int total = 0;
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
total += matrix[row][column];
}
}
[x] Summing all elements by column
for (int column = 0; column < matrix[0].length; column++) {
int total = 0;
for (int row = 0; row < matrix.length; row++)
total += matrix[row][column];
System.out.println("Sum for column " + column + " is "
+ total);
}
[x] Which row has the largest sum
- Finding the smallest index of the largest element
- Random shuffling
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
int i1 = (int)(Math.random() * matrix.length);
int j1 = (int)(Math.random() * matrix[i].length);
// Swap matrix[i][j] with matrix[i1][j1]
int temp = matrix[i][j];
matrix[i][j] = matrix[i1][j1];
matrix[i1][j1] = temp;
}
}
多维数组
double[][][] scores = {
{{7.5, 20.5}, {9.0, 22.5}, {15, 33.5}, {13, 21.5}, {15, 2.5}},
{{4.5, 21.5}, {9.0, 22.5}, {15, 34.5}, {12, 20.5}, {14, 9.5}},
{{6.5, 30.5}, {9.4, 10.5}, {11, 33.5}, {11, 23.5}, {10, 2.5}},
{{6.5, 23.5}, {9.4, 32.5}, {13, 34.5}, {11, 20.5}, {16, 7.5}},
{{8.5, 26.5}, {9.4, 52.5}, {13, 36.5}, {13, 24.5}, {16, 2.5}},
{{9.5, 20.5}, {9.4, 42.5}, {13, 31.5}, {12, 20.5}, {16, 6.5}}
};