JAVA 數組,java數組
數組對每門程式設計語言都是重要的資料結構之一,java語言提供的數組是用來儲存固定大小的同類型元素的。
當你需要儲存一組資料類型相同的變數或者對象時,我們不可能給每個變數都定義一個變數名,這樣的操作會使代碼臃腫、工作量大且無意義。這時我們就需要數組來儲存這些資料。數組根據需要的不同分為一維數,二維數組和多維陣列。下面我們來看下數組的聲明、建立和初始化。
一、數組的聲明
要在程式中使用數組,就需要先聲明數組,下面是聲明陣列變數的文法:
一位元組的聲明方法:
type[] 數組名;(首選方法)
type 數組名[]; (效果一樣,但不推薦)
數組在聲明時不能指定器長度(數組中元素的個數)
二、數組的建立
Java中使用new關鍵字來建立數組的,文法如下:
方式1(推薦,更能表現現數組的類型)
type[] 數組名 = new type[數組中元素個數];
eg:
int[] a = new int[3];
數組名,也即應用a,指向數組元素的首地址。
方式2(定義時直接初始化)
type[] 變數名 = new type[]{逗號分隔的數組元素}
其中紅色部分可以省掉,所以又分兩種:
1、int[] num=new int[]{1,2,2,3,3};
2、int[] num1= {1,3,6,87,4};
其中 int[] num=new int[]{1,2,2,3,3};第二個方括弧不能加數組長度,因為數組個數是由後面花括弧決定的。
Java中的每個數組都有length屬性,表示數組的長度。
length屬性是public final int的,即length是唯讀。數組長度一旦確定,就不能改變大小。
二維數組的定義方法:
1 int[][] table = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 5, 6, 3 } }; 2 3 int[][] table1 = null; 4 table1 = new int[3][3]; 5 6 int[][] table2 = new int[3][3]; 7 table2[0][0] = 11; 8 table2[0][1] = 121; 9 table2[0][2] = 11;10 table2[1][0] = 11;11 table2[1][1] = 121;12 table2[1][2] = 11;13 table2[2][0] = 11;14 table2[2][1] = 121;15 table2[2][2] = 11;16 System.out.println();
二維數組的遍曆
1 for (int row = 0; row < table.length; row++) {2 for (int column = 0; column < table[row].length; column++) {3 System.out.print(table[row][column] + "\t");4 }5 System.out.println();6 } 三、數組的初始化
1.動態初始化:數組定義與為數組分配空間和賦值的操作分開進行;
2.靜態初始化:在定義數位同時就為數組元素分配空間並賦值;
3.預設初始化:數組是參考型別,它的元素相當於類的成員變數,因此數組分配空間後,每個元素也被按照成員變數的規則被隱士初始化。
動態執行個體:
1 public class Arrays 2 { 3 public static void main(String args[]) { 4 int a[] ; 5 a = new int[3] ; 6 a[0] = 0 ; 7 a[1] = 1 ; 8 a[2] = 2 ; 9 String string[] ; 10 string= new String[3] ; 11 string[0] = "張三"; 12 string[1] = "李四" ; 13 string[2] = "小明"; 14 } 15 }
靜態執行個體:
1 public class Arrays 2 { 3 public static void main(String args[]) { 4 int a[] = {0,1,2} ; 5 String[] string = {"張三","李四","小敏"} ; 6 } 7 }
預設執行個體:
1 public class Arrays 2 { 3 public static void main(String args[]) { 4 int[] a = new int [5] ; 5 System.out.println("" + a[3]) ; 6 } 7 }
處理數組:
數組的元素和數組大小是確定的,並且數組的元素有多個,所以當處理數組時,我們通常使用基本for迴圈和增強for迴圈
樣本:
該執行個體完整地展示了如何建立、初始化和操縱數組:
1 public class Array { 2 2 3 3 public static void main(String[] args) { 4 4 double[] List = {1.9, 2.9, 3.4, 3.5}; 5 5 6 6 // 列印所有數組元素 7 7 for (int i = 0; i <List.length; i++) { 8 8 System.out.println(List[i] + " "); 9 9 }10 10 // 計算所有元素的總和11 11 double total = 0;12 12 for (int i = 0; i < List.length; i++) {13 13 total += List[i];14 14 }15 15 System.out.println("Total is " + total);16 16 // 尋找最大元素17 17 double max = List[0];18 18 for (int i = 1; i < List.length; i++) {19 19 if (List[i] > max) {20 20 max = List[i];21 21 }22 22 }23 23 System.out.println("Max is " + max);24 24 }25 25 }Array1.java 增強for迴圈遍曆數組
JDK 1.5 引進了一種新的迴圈類型,它能在不使用下標的情況下遍曆數組。
該執行個體用來顯示數組myList中的所有元素:
1 public class Array { 2 3 public static void main(String[] args) { 4 int[] List = {1,9,2,9,3,4,3,5}; 5 6 // 列印所有數組元素 7 for (int a:List) { 8 System.out.println(a); 9 }10 }11 }
數組的複製
1、使用for迴圈複製
1 public class ArrayCopy { 2 3 public static void main(String[] args) { 4 int[] number = { 11, 2, 23, 12, 4, 34, 5 }; 5 int[] number1 = new int[number.length]; 6 7 if (number != null && number.length > 0) { 8 for (int i = 0; i < number.length; i++) { 9 number1[i] = number[i];10 }11 12 for (int value : number1) {13 System.out.print(value + "\t");14 }15 }16 }17 }Array2.java
2、copyOf方法複製數組
1 public class Text { 2 public static void main(String[] args) { 3 int[] a={2,5,2,2,2,6,2,3}; 4 5 Text t=new Text(); 6 int[] m=t.copyOf(a, a.length); 7 for(int n :m){ 8 System.out.print(n+" "); 9 }10 11 System.out.println();12 13 int[] b=Arrays.copyOf(a, a.length);14 for(int n : b){15 System.out.print(n+" ");16 }17 }18 public static int[] copyOf(int[] b,int newLength){19 return b;20 }21 }Array3.java
3、將指定數組的指定範圍複製到一個新數組(copyOfRange)
1 public class Text {2 public static void main(String[] args) {3 int[] a = { 2, 4, 2, 77, 22, 777, 34 };4 int[] b = Arrays.copyOfRange(a, 0, 3);5 for (int n : b) {6 System.out.print(n + " ");7 }8 }9 }
數組排序 1 public class Bubble { 2 3 public static void main(String[] args) { 4 int a = 100; 5 int b = 46; 6 /* 7 * int temp = 0; temp = a; a = b; b = temp; 8 */ 9 /*10 * a = a + b; b = a - b; a = a - b;11 */12 /*13 * a = a ^ b; b = a ^ b; a = a ^ b;14 */15 16 System.out.println("a=46,b=100 " + "\t" + a + " " + b);17 18 // 冒泡排序19 int[] number = { 11, 2, 23, 12, 4, 34, 5 };20 // 11, 2, 23, 12, 4, 34, 521 22 System.out.println("排序前:");23 for (int num : number) {24 System.out.print(num + "\t");25 }26 27 for (int i = 0; i < number.length; i++) { // 控制拿出數組中的第幾個元素28 for (int k = 0; k < number.length; k++) {29 if (number[i] > number[k]) {30 int temp = 0;31 temp = number[i];32 number[i] = number[k];33 number[k] = temp;34 }35 }36 }37 38 System.out.println("\n排序後:");39 for (int num : number) {40 System.out.print(num + "\t");41 }42 }43 }Array4.java
使用sort方法排序
1 public class Text { 2 public static void main(String[] args) { 3 int[] a = { 2, 4, 2, 77, 22, 777, 34 }; 4 Arrays.sort(a); 5 for(int b:a){ 6 System.out.print(b+" "); 7 } 8 } 9 10 }
產生一個不重複的隨機數組 1 public class NoRepeatArray { 2 public static void main(String[] args) { 3 System.out.println(); 4 Random rd = new Random(); 5 System.out.println(rd.nextInt(5) + 1); // 0-9之間的隨機數 6 7 // 從1-5之間產生5個不重複的隨機數 8 int[] numbers = new int[5]; 9 // 定義儲存值數組的索引10 int index = 0;11 System.out.println("---------------------------------\n");12 for (int i = 1; i <= 5; i++) {13 Random rd1 = new Random();14 int num = rd1.nextInt(5) + 1;15 16 if (numbers[0] != 0) {17 boolean flag = false; // 控制是否存在重複元素18 19 // 遍曆生產數組中的元素20 for (int j = 0; j < numbers.length; j++) {21 22 if (num != numbers[j]) {23 flag = true;24 } else {25 flag = false;26 break;27 }28 }29 30 if (flag == true) {31 numbers[index++] = num;32 } else {33 34 // 發現有重複元素重新產生新的隨機數35 i--;36 }37 38 } else {39 numbers[index++] = num;40 }41 42 }43 44 for (int val : numbers) {45 System.out.print(val + "\t");46 }47 }48 }Array5.java
數組的比較
如果兩個數組以相同順序包含相同的元素,則兩個數組是相等的。此外,如果兩個數組引用都為 null,則認為它們是相等的。
1 public class Text { 2 public static void main(String[] args) { 3 int[] a = { 2, 4, 2, 77, 22, 777, 34 }; 4 int[] b = { 2, 4, 2, 77, 22, 777, 34 }; 5 int[] c = { 4, 2, 22, 77, 2, 777, 34 }; 6 7 boolean flag = Arrays.equals(a, b); 8 System.out.println(flag); 9 10 boolean flag1=Arrays.equals(a, c);11 System.out.println(flag1);12 }13 }
以上執行個體編譯運行結果如下:
true
false