您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關java中數(shù)組有什么用的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
數(shù)組是相同類型數(shù)據(jù)的有序集合 數(shù)組描述的是相同類型的若干數(shù)據(jù),按照一定先后次序排序組合而成 其中,每一個數(shù)據(jù)稱作一個數(shù)組元素,每個數(shù)組元素可以通過下標訪問它們
首先必須聲明數(shù)組變量,才能在程序中使用數(shù)組。
Java語言使用new操作符來創(chuàng)建數(shù)組,語法如下
dataType[] arrayRefVar; //首選 dataType arrayRefVar[]; //效果相同,但不是首選
數(shù)組的元素是通過索引訪問的,數(shù)組索引從0開始
dataType[] arrayRefVar = new dataType[arraySize]; //int[] nums=new int[10]
獲取數(shù)組長度:arrays.length
int[] nums; //1.聲明一個數(shù)組 nums = new int[3]; //2.創(chuàng)建一個數(shù)組 //3.給數(shù)組元素賦值 nums[0]=1; nums[1]=2; nums[2]=3; for (int num : nums) { //打印數(shù)組所有元素 System.out.println(num); }
靜態(tài)初始化
//靜態(tài)初始化:創(chuàng)建+賦值 int[] a={1,2,3}; Man[] mans={new Man(1,1),new Man(2,2)}
動態(tài)初始化
//包含默認初始化 int[] a=new int[2]; //默認值為0 a[0]=1; a[1]=2;
默認初始化
數(shù)組是引用類型,它的元素相當于類的實例變量,因此數(shù)組一經(jīng)分配空間,其中的每個元素也被按照實例變量同樣的方式被隱式初始化。
其長度是確定的,數(shù)組一旦被創(chuàng)建,它的大小就是不可改變的。
其元素必須是相同類型,不允許出現(xiàn)混合類型。
數(shù)組中的元素可以是任何數(shù)據(jù)類型,包括基本類型和引用類型。
數(shù)組變量屬于引用類型,數(shù)組也可以看作對象,其中每個元素相當于該對象的成員變量。
數(shù)組本身就是對象,Java中對象是在堆中的,因此數(shù)組無論保存原始類型還是其他對象類型, 數(shù)組本身是在堆中的。
下標的合法區(qū)間:[0, length-1],如果越界就會報錯;
public static void main(String[] args) { int[] a=new int[2]; system.out.println(a[2]); }
ArraylndexOutOfBoundsException:數(shù)組下標越界異常!
小結:
數(shù)組是相同數(shù)據(jù)類型(數(shù)據(jù)類型可以為任意類型)的有序集合數(shù)組也是對象。
數(shù)組元素相當于對象的成員變量
數(shù)組長度的確定的,不可變的。 如果越界,則報:ArraylndexOutofBounds
int[] arrays = {1,2,3,4,5}; //打印全部的數(shù)組元素 JDK1.5 沒有下標 for (int array : arrays) { System.out.println(array); }
//打印數(shù)組元素 public static void printArray(int[] a){ for (int i = 0; i < a.length; i++) { System.out.print(a[i]+" "); } }
//反轉數(shù)組 public static int[] reverse(int[] arrays){ int[] result = new int[arrays.length]; //反轉的操作 for (int i = 0; i < arrays.length; i++) { result[i] = arrays[arrays.length-i-1]; } return result; }
多維數(shù)組可以看成數(shù)組的數(shù)組,比如二維數(shù)組就是一個特殊的數(shù)組,其每一個元素都是一個一維數(shù)組。
int arr[][] = new int[3][2]; //二維數(shù)組,三行兩列
int[][] array = {{1,2},{3,4},{5,6}}; //打印二維數(shù)組所有元素 for (int i = 0; i < array.length; i++) { //arrays.length=3 for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j]+" "); } System.out.println(); }
數(shù)組的工具類java.util.Arrays
由于數(shù)組對象本身并沒有什么方法可以供我們使用,但API提供了一個工具類Arrays供我們使用。
Array類中的方法都是static修飾的靜態(tài)方法,使用時直接使用類名進行調(diào)用,可以不用對象調(diào)用。
常用功能
給數(shù)組賦值:fill方法。
排序:sort方法,升序。
比較數(shù)組:equals方法比較數(shù)組中元素值是否相等。
查找數(shù)組元素:binarySearch對排序好的數(shù)組進行二分查找法操作。
int[] a = {1,2,3,4,9000,32145,451,21}; System.out.println(a); // [I@28d93b30 (hashcode) //Arrays.toString 打印數(shù)組元素 System.out.println(Arrays.toString(a)); //[1, 2, 3, 4, 9000, 32145, 451, 21] //二分法查找某值 返回下標 System.out.println(Arrays.binarySearch(a, 9000)); // 4 //填充 Arrays.fill(a,2,4,0); //數(shù)組[a[2]~a[4])之間填充0 System.out.println(Arrays.toString(a)); //[1, 2, 0, 0, 9000, 32145, 451, 21] //升序排序 Arrays.sort(a);
1、冒泡排序是八大排序最出名的排序算法。
2、代碼:兩層循環(huán),外層冒泡輪數(shù),里層依次比較。
3、當我們看到嵌套循環(huán),應該立馬就可以得出這個算法的時間復雜度為O(n2)。
//冒泡排序 //1.比較數(shù)組中兩個相鄰的元素,如果第一個數(shù)大于第二個數(shù),交換它們位置 //2.每一次比較,都會產(chǎn)生一個最大或最小的數(shù)字(升序為最大數(shù)) //3.下一輪則可以少一次排序 //4.依次循環(huán),直到結束 public static int[] sort(int[] array){ int temp=0; //外層循環(huán),次數(shù)length-1 for (int i = 0; i < array.length-1; i++) { //內(nèi)層循環(huán):如果第一個數(shù)大于第二個數(shù),交換它們位置 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; } } } return array; } public static void main(String[] args) { int[] a={8,1,35,47,19,-2}; int[] sort = sort(a); System.out.println(Arrays.toString(sort)); //[-2, 1, 8, 19, 35, 47] }
優(yōu)化
//冒泡排序算法 int[] a = {6,2,4,5,2,1,3}; int temp = 0; for(int i = 0;i<a.length-1;i++){ boolean flag = false; for(int j = 0;j<a.length-1-i;j++){ if(a[j] > a[j+1]){ temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; flag = true; } } } if(!flag){ break; } System.out.println("排序后的結果是:"); for(int i : arr){ System.out.print(i); }
當一個數(shù)組中大部分元素為0,或者為同一值的數(shù)組時,可以使用稀疏數(shù)
組來保存該數(shù)組稀疏數(shù)組的處理方式是:
記錄數(shù)組一共有幾行幾列,有多少個不同值
把具有不同值的元素和行列及值記錄在一個小規(guī)模的數(shù)組中,從而縮小程序的規(guī)模
如下圖:左邊是原始數(shù)組,右邊是稀疏數(shù)組
需求:編寫五子棋游戲中,有存盤退出和續(xù)上盤的功能
分析問題:因為該二維數(shù)組的很多值是默認值0,因此記錄了很多沒有意義的數(shù)據(jù)。
解決:稀疏數(shù)組
//創(chuàng)建一個二維數(shù)組 11*11 0:沒有棋子,1:黑棋 2:白棋 int[][] array1 = new int[11][11]; array1[1][2] = 1; array1[2][3] = 2; //輸出原始的數(shù)組 System.out.println("原始的數(shù)組:"); for (int[] array : array1) { for (int i : array) { System.out.print(i+"\t"); } System.out.println(); } //轉換為稀疏數(shù)組保存 //1.有效值的個數(shù) int sum = 0; //有效值總數(shù) for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if(array1[i][j]!=0){ sum++; } } } //2.創(chuàng)建一個稀疏數(shù)組 int[][] array2 = new int[sum+1][3]; array2[0][0] = 11; array2[0][1] = 11; array2[0][2] = sum; //3.遍歷二維數(shù)組,將有效值存放到稀疏數(shù)組 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.輸出稀疏數(shù)組 System.out.println("稀疏數(shù)組:"); for (int i = 0; i < array2.length; i++) { for (int j = 0; j < array2[i].length; j++) { System.out.print(array2[i][j]+"\t"); } System.out.println(); }
/* 結果:
輸出原始的數(shù)組
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
稀疏數(shù)組
11 11 2
1 2 1
2 3 2
*/
感謝各位的閱讀!關于“java中數(shù)組有什么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。