Java常問面試內(nèi)容--數(shù)組、聲明、初始化、冒泡、多維數(shù)組、稀疏數(shù)組
更新時(shí)間:2021年07月17日 14:39:28 作者:蛋撻學(xué)姐
這篇文章主要介紹了Java多線程面試題(面試官常問),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
數(shù)組
- 數(shù)組時(shí)相同類型數(shù)據(jù)的有序集合
- 數(shù)組描述的是相同類型的若干個數(shù)據(jù),按照一定的先后次序排列組合而成
- 其中,每一個數(shù)據(jù)稱作一個數(shù)組元素,每一個數(shù)組元素可以通過一個下標(biāo)來訪問它們。
數(shù)組聲明創(chuàng)建
首先必須聲明數(shù)組變量,才能在程序中使用數(shù)組。下面是聲明數(shù)組變量的語法。
- da taType[] arrayRefVar //首選方法
- dateType arrayRefVar[] //效果相同,但不是首選方法
java語言使用new操作符來創(chuàng)建數(shù)組,語法如下:
- dateType[] arrayRefVar = new dataType[arraySize]
數(shù)組的元素是通過索引訪問的,數(shù)組索引從 0 開始
獲取數(shù)組長度
- arrays.length
數(shù)組初始化
- 靜態(tài)初始化
- int[] a = {1,2,3,4};
- Man[] mans = {new Man(1,1), new Man(2,2)};
- 動態(tài)初始化
- Int[] a = new int[2];
- a[0] = 1;
- a[1] = 2;
- 默認(rèn)初始化
- 數(shù)組時(shí)引用類型,它的元素相當(dāng)于是實(shí)例變量,因此數(shù)組一經(jīng)分配空間,其中的每一個元素也被按照實(shí)例變量同樣的方式被陰式初始化。
數(shù)組的四個基本特點(diǎn)
- 數(shù)組的長度是確定的。數(shù)組一旦被創(chuàng)建,它的大小就是不可以改變的
- 數(shù)組的元素必須是相同類型,不允許出現(xiàn)混合類型
- 數(shù)組中的元素可以是任何數(shù)據(jù)類型,包括基本類型和引用類型
- 數(shù)組變量屬引用類型,數(shù)組也可以看成是在堆中的,因此數(shù)組無論保存原始類型還是其他對象類型,數(shù)組對象本身是在堆中的。
數(shù)組邊界
- 下標(biāo)的合法區(qū)間:【0,length-1】,如果越界就會報(bào)錯
- ArrayIndexOutOfBounds Exception:數(shù)組下標(biāo)越界異常
- 小結(jié)
- 數(shù)組是相同數(shù)據(jù)類型的有序集合
- 數(shù)組也是對象。數(shù)組元素相當(dāng)于對象的成員變量。
- 數(shù)組長度是確定的,不可變的。如果越界,則報(bào)錯》ArrayIndexOutOfBoundsException
多維數(shù)組
- 多維數(shù)組可以看成是數(shù)組的數(shù)組
//二維數(shù)組 兩行五列 int a[][] = new int[2][5];
package com.sxl.array; public class Demo04 { public static void main(String[] args) { //二維數(shù)組 int[][] array = {{1,2},{3,4},{5,6}}; for (int i = 0; i < array.length; i++) { for(int j = 0; j < array[i].length; j++){ System.out.println(array[i][j]); } } } }
Arrays類
- 數(shù)組工具類
java.util.Arrays
Arrays
類中的方法都是static靜態(tài)方法,在使用的時(shí)候可以直接使用類名進(jìn)行調(diào)用- 對數(shù)組排序:
sort
方法。 - 比較數(shù)組:通過
equals
方法比較數(shù)組中元素是否相等
package com.sxl.array; import java.util.Arrays; public class Demo01 { public static void main(String[] args) { //靜態(tài)初始化:創(chuàng)建 賦值 int[] array = {1,2,3,4,5}; //動態(tài)初始化 :包含默認(rèn)初始化 int[] b = new int[10]; b[0] = 10; //printArray(array); // printString(array); System.out.println("=============="); int[] result = reverse(array); printArray(result); } public static int[] reverse(int[] array){ int[] result = new int[array.length]; for (int i = 0,j = result.length-1; i < array.length ; i++,j--) { result[j] = array[i]; }//加入Java開發(fā)交流君樣:593142328一起吹水聊天 return result; } public static void printArray(int[] array){ for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println(); System.out.println("================="); //增強(qiáng)循環(huán) for (int arr: array) { System.out.print(arr+ " "); } System.out.println(); System.out.println("================="); String result = Arrays.toString(array); System.out.println(result); } public static void printString(int[] array){ String result = Arrays.toString(array); System.out.println(result); for (int i = 0; i < array.length; i++) { if (i == 0){ System.out.print("[" +array[i] +", "); }else if (i == array.length - 1){ System.out.print(array[i]+"]"); }else System.out.print(array[i]+", "); } } } package com.sxl.array; public class Demo02 { public static void main(String[] args) { new Demo02(); int[] array = {1,2,5,3,9,7,6,3,2}; sort(array); } //冒泡排序 public static void sort(int[] array){ int temp = 0; for (int i = 0; i < array.length; i++) { boolean flag = false; for (int j = 0; j < array.length-1; j++){ if (array[j+1]<array[j]){ temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; flag = true; } } if (flag == false){ break; } } for (int i = 0; i < array.length; i++) { System.out.print(array[i]+" "); } } } package com.sxl.array; public class Demo03 {//加入Java開發(fā)交流君樣:593142328一起吹水聊天 public static void main(String[] args) { int[] array = {1,2,3,4,5}; //打印所有數(shù)據(jù)元素 for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } //計(jì)算所有數(shù)據(jù)元素的和 System.out.println("========="); int sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } System.out.println(sum); //查找最大元素 int max = array[0]; for (int i = 1; i < array.length; i++) { if (max < array[i]){ max = array[i]; } } System.out.println(max); } }
稀疏數(shù)組
package com.sxl.array; public class Demo05 { public static void main(String[] args) { int[][] array = new int[11][11]; array[1][2] = 1; array[2][3] = 2; //輸出原始數(shù)組 for (int[] a: array) { for (int b: a) { System.out.print(b+"\t"); } System.out.println(); } //獲取有效值的個數(shù) int sum = 0; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++){ if (array[i][j]!=0){ sum++; } } } System.out.println("有效值的個數(shù)是:" +sum); //創(chuàng)建一個稀疏數(shù)組 第一行存放: 行數(shù) 列數(shù) 有效數(shù)個數(shù) int[][] array2 = new int[sum+1][3]; array2[0][0] = 11; //行數(shù) array2[0][1] = 11; //列數(shù) array2[0][2] = sum; //有效數(shù)個數(shù) //遍歷原始二維數(shù)組,將非零數(shù)組存入稀疏數(shù)組中 //加入Java開發(fā)交流君樣:593142328一起吹水聊天 int count = 0; for (int i = 0; i < array.length; i++) { for (int j = 0;j < array[i].length; j++){ if (array[i][j]!=0){ count++; array2[count][0] = i; array2[count][1] = j; array2[count][2] = array[i][j]; } } } 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(); } } }
總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
AOP之事務(wù)管理<aop:advisor>的兩種配置方式
這篇文章主要介紹了AOP之事務(wù)管理<aop:advisor>的兩種配置方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11PowerJob的DispatchStrategy方法工作流程源碼解讀
這篇文章主要為大家介紹了PowerJob的DispatchStrategy方法工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01java 工廠模式的講解及優(yōu)缺點(diǎn)的介紹
這篇文章主要介紹了java 工廠模式的講解及優(yōu)缺點(diǎn)的介紹的相關(guān)資料, 簡單工廠模式,又稱為靜態(tài)工廠方法(Static Factory Method)模式,它屬于類創(chuàng)建型模式,需要的朋友可以參考下2017-08-08webuploader在springMVC+jquery+Java開發(fā)環(huán)境下的大文件分片上傳的實(shí)例代碼
這篇文章主要介紹了webuploader在springMVC+jquery+Java開發(fā)環(huán)境下的大文件分片上傳的實(shí)例代碼,需要的朋友可以參考下2017-04-04