JavaSE 數組、多維數組、稀疏數組
1 數組基礎
1.1 數組定義
- 數組是相同類型數據的有序組合。
- 每個數據稱爲一個數組元素,每個數據元素可以通過一個下標來訪問。
- 數組下標從0開始,到n-1結束。
1.2 數組的聲明與初始化
數組的聲明
-
使用前必須聲明。語法如下:
dataType[] arrayRefVar;//首選 //or dataType arrayRefVar[];//效果相同,但不是首選
-
Java語言使用new操作符來創建,語法如下:
dataType[] arayRefVar = new dataType[arraySize]; //例: int[] nums = new int[10]; //聲明一個數組並創建
-
數組一旦創建,不可改變長度。
-
獲取數組長度可以使用Arrays.length()方法。
數組的初始化
-
靜態初始化
int[] a = {1,2,3};
-
動態初始化(包含默認初始化)
int[] b = new int[10];//此時數組中元素均爲默認值 b[0] = 10;
-
數組默認初始化
數組是引用類型,它的元素相當於類的實例變量,一經分配空間,其中每個元素也按照實例變量同樣的方式被隱式初始化。
1.3 數組的使用
話不多說,直接展示代碼:
package com.targetu.array;
public class ArrayDemo01 {
public static void main(String[] args) {
int[] a = {1,2,3,4,5,6};
//原數組打印
printArray(a);
//計算所有元素的和
int sum = 0;
for (int i = 0; i<a.length; i++){
sum += a[i];
}
System.out.println("sum: " +sum);
//查找最大元素
int max = a[0];
for (int i = 0; i < a.length; i++) {
if (a[i] > max){
max = a[i];
}
}
System.out.println("max: " +max);
//反轉數組並打印
printArray(reverse(a));
}
//打印數組
public static void printArray(int[] arrays){
for (int array : arrays) {
System.out.print(array + " ");
}
System.out.println();
}
//反轉數組
public static int[] reverse(int[] arrays){
int[] result = new int[arrays.length];
//反轉操作
for(int i = 0, j = result.length-1; i < arrays.length; i++, j--){
result[j] = arrays[i];
}
return result;
}
}
//運行結果:
//1 2 3 4 5 6
//sum: 21
//max: 6
//6 5 4 3 2 1
2 Java內存說明
-
Java內存:
類型 功能 堆 (1)存放所有new出來的對象和數組
(2)可以被所有的線程共享,不會存放別的對象引用棧 (1)存放基本變量類型(包含這個基本類型的基本數值)
(2)存放引用對象的變量(存放引用在堆裏面的具體地址)
(3)數組保存在堆中方法區 (1)可以被所有的線程共享
(2)包含了所有的class和static變量 -
數組聲明使用過程流程圖展示:(使用的是一款在線畫圖工具processOn,非常nice!)
3 Java重點補充
3.1 二維數組
-
多維數組可以看成是數組的數組,比如二維數組就是一個特殊的一維數組,其每一個元素是一個一維數組。
-
二維數組舉例:
int a[][] = new int[3][2];
此二維數組a可以看成一個三行兩列的數組。
圖示:
-
二維數組使用
package com.targetu.array; public class ArrayDemo02 { public static void main(String[] args) { //創建2*3的二維數組 int[][] array = {{1,1},{2,2},{3,3}}; //打印數組元素 for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++){ System.out.print(array[i][j]+" "); } System.out.println(); } } } //運行結果: //1 1 //2 2 //3 3
3.2 Arrays類
-
數組的工具類java.util.Arrays
-
類中方法均爲static開頭,故可直接調用
-
案例展示
package com.targetu.array; //此例中展示打印、排序、填充的使用 import java.util.Arrays; public class ArrayDemo03 { public static void main(String[] args) { int[] a = {1,1,2,33,21,44,9,10}; //打印數組元素Arrays.toString() System.out.println(Arrays.toString(a)); //數組排序:升序 Arrays.sort(a); System.out.println(Arrays.toString(a)); //數組填充 Arrays.fill(a,2,4,0); System.out.println(Arrays.toString(a)); } }
3.3 冒泡排序
注意本例中對冒泡排序的優化!
package com.targetu.array;
import java.util.Arrays;
public class ArrayDemo04 {
public static void main(String[] args) {
int[] a = {1,2,3,5,2,9,4,7};
System.out.println(Arrays.toString(mysort(a)));
}
public static int[] mysort(int[] array){
//外層循環,判斷要走多少次
int temp = 0;
for (int i = 0; i < array.length; i++) {
boolean flag = false; //通過flag標示,若後續數組元素已排好序,直接退出循環
//內層循環,比較前後兩個數,若前者大於後者,交換順序
for (int j = 0; j < array.length-1-i; j++){
if (array[j] > array[j+1]){
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
flag = true;
}
}
if (!flag){
break;
}
}
return array;
}
}
//運行結果:
//[1, 2, 2, 3, 4, 5, 7, 9]
注:冒泡排序是最基礎的排序,其中的優化算法需重點學習。
4 稀疏數組(重點在案例部分!)
-
稀疏數組是一種數據結構。
-
若數據量很小,其他值則均爲默認值0,會造成資源浪費。
-
稀疏數組處理方式:(使用三元組的方式)
- 記錄數組一共有幾行幾列,有多少個不同值。
- 把具有不同值的元素和行列及值記錄在一個小規模的數組中,從而縮小程序規模。
-
案例展示(共包含三個步驟)
- 原始數組的創建與輸出
- 原始數組轉換爲稀疏數組並輸出
- 稀疏數組轉換爲原始數組並輸出
package com.targetu.array;
public class ArrayDemo05 {
public static void main(String[] args) {
// 一、創建原始數組
//1. 創建一個二維數組 11*11 0:沒有棋子 1:黑棋 2:白棋
int[][] array1 = new int[11][11];
array1[1][2] = 1;
array1[2][3] = 2;
//2. 輸出原始數組
System.out.println("原始數組爲:");
for (int[] ints : array1) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
System.out.println("=========================================");
// 二、原始數組轉換爲稀疏數組
//1. 首先應該獲取有效值的個數
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (array1[i][j]!=0){
sum++;
}
}
}
System.out.println("有效值個數爲:" + sum);
//2. 創建一個稀疏數組
int[][] array2 = new int[sum+1][3];
array2[0][0] = 11;
array2[0][1] = 11;
array2[0][2] = sum;
//3. 遍歷二維數組,將非零的值,存放到稀疏數組中
int count = 0;
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
if (array1[i][j]!=0){
count++;
array2[count][0] = i;
array2[count][1] = j;
array2[count][2] = array1[i][j];
}
}
}
//4. 輸出稀疏數組
System.out.println("稀疏數組爲:");
for (int i = 0; i < array2.length; i++) {
System.out.println(array2[i][0]+"\t"
+array2[i][1]+"\t"
+array2[i][2]);
}
System.out.println("=========================================");
// 三、稀疏數組轉換爲原始數組
//1. 讀取稀疏數組
int[][] array3 = new int[array2[0][0]][array2[0][1]];
//2. 給其中的元素賦值(注意從i = 1 開始讀取)
for (int i = 1; i < array2.length; i++) {
array3[array2[i][0]][array2[i][1]] = array2[i][2];
}
//3. 打印輸出還原後的數組
System.out.println("還原後數組爲:");
for (int[] ints : array3) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
System.out.println("=========================================");
}
}
//運行結果:
/*
原始數組爲:
0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 2 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 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 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 0 0 0 0 0
=========================================
有效值個數爲:2
稀疏數組爲:
11 11 2
1 2 1
2 3 2
=========================================
還原後數組爲:
0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 2 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 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 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 0 0 0 0 0
=========================================
Process finished with exit code 0
*/
注:
- 稀疏數組中第一行存儲的是原始數組中行、列數及元素個數信息。
- 讀取稀疏數組時,從第二行開始讀取。(即從i = 1開始)
寫在最後
Love is patient, love is kind!
Give Demut and Dottie!