數(shù)組
一維數(shù)組
概念
- 記錄單個數(shù)據(jù)內(nèi)容時,聲明一個變量即可
- 在程序中記錄多個類型相同的數(shù)據(jù)內(nèi)容時,聲明一個一維數(shù)組即可
- 一維數(shù)組的本質(zhì)是在內(nèi)存空間中申請一段連續(xù)的存儲單元
- 數(shù)組是相同數(shù)據(jù)類型的多個元素的容器:::
- 元素按線性順序排列,體現(xiàn)為一種 引用數(shù)據(jù)類型
概念
格式
- 數(shù)據(jù)類型[] 數(shù)組名稱 = new 數(shù)據(jù)類型[數(shù)組長度];
- 數(shù)組長度 在聲明的時候進(jìn)行設(shè)定及l(fā)ength屬性進(jìn)行獲取
- 通過下標(biāo)的方式訪問數(shù)組中的每一個元素
數(shù)組的下標(biāo)從 0 開始;對于長度為n的數(shù)組,下標(biāo)的范圍是 0 ~ n-1
內(nèi)存結(jié)構(gòu)分析
- 內(nèi)存結(jié)構(gòu)之棧區(qū) 【基本數(shù)據(jù)類型】
- 棧用于存放程序運(yùn)行過程中所有的局部變量。 一個運(yùn)行的java程序從開始到結(jié)束會有多次變量的聲明
- 存放當(dāng)前變量的值
int num = 2;
- 內(nèi)存結(jié)構(gòu)之堆區(qū) 【引用數(shù)據(jù)類型】
- jvm會在內(nèi)存空間中開辟一個稱為“堆”的存儲空間,這部分空間用于存儲使用new關(guān)鍵字創(chuàng)建的數(shù)組和對象
- 存放當(dāng)前對象的內(nèi)存地址
int[] num = int[2];
一維數(shù)組優(yōu)缺點
-
優(yōu)點
- 直接通過下標(biāo)(索引)的方式訪問指定位置的元素 快速
-
缺點
- 所有元素類型必須相同
- 內(nèi)存空間必須連續(xù),長度一點確定就不能修改
- 增加和刪除元素時可能移動大量元素,效率低
實踐出真理
1.基操
void BaseTest() {
//定義一個一維數(shù)組 int類型、長度為3
int[] array = new int[3];
//方式二 不推薦使用,容易與 變量的聲明區(qū)分,提高代碼可讀性
//int array_1[] = new int[3];
//獲取數(shù)組的長度
int array_Length = array.length;
System.out.println("數(shù)組的長度為: " + array_Length);
for (int i = 0; i < array_Length; i++) {
System.out.println("當(dāng)前下標(biāo)為: " + i + " 的內(nèi)容是: " + array[i]);
}
//在聲明的同時進(jìn)行賦值 靜態(tài)方式簡化版
char[] chars = {'a', 'b', 'c', 'd'};
//方式二 靜態(tài)方式
boolean[] bool = new boolean[]{false, true, true, false};
for (int i = 0; i < chars.length; i++) {
System.out.println("當(dāng)前下標(biāo)為: " + i + " 的內(nèi)容是: " + chars[i] + "\tbool中的內(nèi)容為: " + bool[i]);
}
}
運(yùn)行結(jié)果:
2.增刪改查操作
void AddDelChaIns() {
int[] arr = new int[5];
// 將 “11、22、33、44”按順序從頭(下標(biāo)為0)進(jìn)行賦值
// arr[0] = 11;
// arr[1] = 22;
// arr[2] = 33;
// arr[3] = 44;
//簡化操作
for (int i = 0; i < arr.length - 1; i++) {
arr[i] = (i + 1) * 11;
}
System.out.print("賦值后--數(shù)組中的內(nèi)容為: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
//將 “55” 插入 頭部位置(下標(biāo)為0)
for (int i = arr.length - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = 55;
System.out.print("插入后--數(shù)組中的內(nèi)容為: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
//將 55 從頭部位置刪除
for (int i = 0; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
arr[arr.length - 1] = 0;
System.out.print("刪除后--數(shù)組中的內(nèi)容為: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
//查找 22
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 22) {
arr[i] = 330;
break;
}
}
System.out.print("修改后--數(shù)組中的內(nèi)容為: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
運(yùn)行結(jié)果:
3.數(shù)組間的拷貝(復(fù)制)
void Copy() {
int[] arr = {11, 22, 33, 44, 55};
int[] co = new int[3];
//粗暴
co[0] = arr[1];
co[1] = arr[2];
co[2] = arr[3];
//溫柔一點
for (int i = 0; i < co.length; i++) {
co[i] = arr[i + 1];
}
//util
System.arraycopy(arr, 1, co, 0, 3);
}
- 數(shù)組拷貝 co = arr 操作 :
直接用數(shù)組arr中的內(nèi)容覆蓋co中的內(nèi)容
4.統(tǒng)計用戶輸入的任意整數(shù)中各個數(shù)字出現(xiàn)的次數(shù)
void StatisticsNum() {
int[] numArr = new int[10];
System.out.println("請輸入任意一個正整數(shù):");
Scanner sc = new Scanner(System.in);
int numInput = sc.nextInt();
System.out.println("輸入的數(shù)字為: " + numInput);
int temp = numInput;
int num = 0;
while (temp > 0) {
num = temp % 10;
temp /= 10;
numArr[num] = numArr[num] + 1;
}
for (int i = 0; i < numArr.length; i++) {
if (numArr[i] <= 0) continue;
System.out.print("\n數(shù)字 " + i + "\t出現(xiàn)的次數(shù)為: " + numArr[i]);
}
}
運(yùn)行結(jié)果:
5.保存并計算學(xué)生的總成績與平均分
void StudentScore() {
System.out.println("請輸入學(xué)生人數(shù)");
Scanner sc = new Scanner(System.in);
int studentCount = sc.nextInt();
int[] scores = new int[studentCount];
int score = 0;
int total = 0;
for (int i = 0; i < studentCount; i++) {
System.out.println("請輸入第 " + (i + 1) + " 個學(xué)生的成績");
score = sc.nextInt();
scores[i] = score;
total += score;
}
for (int i = 0; i < studentCount; i++) {
System.out.println("第 " + (i + 1) + " 個學(xué)生的成績?yōu)椋?" + scores[i]);
}
System.out.println("學(xué)生的總成績?yōu)椋?" + total + "\n平均分為: " + total * 1.0 / studentCount);
}
運(yùn)行結(jié)果:
數(shù)組工具類
- java.util.Arrays
- 功能:對數(shù)組中元素的遍歷、查找、排序等操作
實踐出真理
1. 遍歷 toString
```java
int [] array = new int[5];
System.out.println("當(dāng)前數(shù)組中的元素為: "+Arrays.toString(array));
//當(dāng)前數(shù)組中的元素為: [0,0,0,0,0]
```
2. 填充指定值 fill
```java
Arrays.fill(array,3);
System.out.println("當(dāng)前數(shù)組中的元素為: "+Arrays.toString(array));
//當(dāng)前數(shù)組中的元素為: [3,3,3,3,3]
```
3. 判斷兩個數(shù)組元素內(nèi)容與次序是否相同 equals
```java
int[] arr = {4,4,4,4,3};
System.out.println(Arrays.equals(arr,array)); // false
Arrays.fill(arr,3);
System.out.println(Arrays.equals(arr,array)); // true
```
4. 排序 sort 從小到大的順序
int[] arr_Order = {12,52,36,21,10};
Arrays.sort(arr_Order)
System.out.println(Arrays.toString(arr_Order))
// [10,12,21,36,52]
5. 查找參數(shù)指定元素所在的位置 binarySearch
System.out.println(" 36分在數(shù)組中的下標(biāo)位置是: " + Arrays.binarySearch(arr_Order,36))
// 36分在數(shù)組中的下標(biāo)位置是: 3
// 如果要查找的元素不存在數(shù)組中,則輸出 -2
二維數(shù)組
概念與格式
- 本質(zhì)上有多個一維數(shù)組摞在一起組成的數(shù)組
- 二維數(shù)組中的每個元素都是一維數(shù)組:::
-
??!一維數(shù)組中的每個元素才是數(shù)據(jù)內(nèi)容 !!
- 聲明和使用
- 數(shù)據(jù)類型[][] 數(shù)組名稱 = new 數(shù)據(jù)類型[行數(shù)][列數(shù)];
- 數(shù)據(jù)類型[][] 數(shù)組名稱 = {{元素1,元素2,元素3,元素...},{},{},{}};
結(jié)果輸出:int[][] arr_Two = new int[2][3]; int tempValue = 0; //初始化并賦值 //方式一 初始化的同時進(jìn)行賦值 int[][] arr_Two ={{0,1,2},{3,4,5}}; //方式二 通過遍歷的方式進(jìn)行賦值 for (int i = 0; i < arr_Two.length; i++) { for (int j = 0; j < arr_Two[i].length; j++) { arr_Two[i][j] = tempValue++; } } for (int i = 0; i < arr_Two.length; i++) { for (int j = 0; j < arr_Two[i].length; j++) { System.out.print(arr_Two[i][j] + "\t"); } System.out.println(); }二維數(shù)組.png
實踐出真理
-
[楊輝三角]
void YangHuiTriangle() {
System.out.println("請輸入要顯示的行數(shù): ");
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
int[][] yh = new int[row][];
for (int i = 0; i < row; i++) {
yh[i] = new int[i + 1];
for (int j = 0; j <= i; j++) {
if (0 == j || i == j) {
yh[i][j] = 1;
} else {
yh[i][j] = yh[i - 1][j] + yh[i - 1][j - 1];
}
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(yh[i][j] + "\t");
}
System.out.println();
}
}
運(yùn)行結(jié)果:
楊輝三角.png



