Java常問面試內容--數組、聲明、初始化、冒泡、多維數組、稀疏數組
更新時間:2021年07月17日 14:39:28 作者:蛋撻學姐
這篇文章主要介紹了Java多線程面試題(面試官常問),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
數組
- 數組時相同類型數據的有序集合
- 數組描述的是相同類型的若干個數據,按照一定的先后次序排列組合而成
- 其中,每一個數據稱作一個數組元素,每一個數組元素可以通過一個下標來訪問它們。
數組聲明創(chuàng)建
首先必須聲明數組變量,才能在程序中使用數組。下面是聲明數組變量的語法。
- da taType[] arrayRefVar //首選方法
- dateType arrayRefVar[] //效果相同,但不是首選方法
java語言使用new操作符來創(chuàng)建數組,語法如下:
- dateType[] arrayRefVar = new dataType[arraySize]
數組的元素是通過索引訪問的,數組索引從 0 開始
獲取數組長度
- arrays.length
數組初始化
- 靜態(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;
- 默認初始化
- 數組時引用類型,它的元素相當于是實例變量,因此數組一經分配空間,其中的每一個元素也被按照實例變量同樣的方式被陰式初始化。
數組的四個基本特點
- 數組的長度是確定的。數組一旦被創(chuàng)建,它的大小就是不可以改變的
- 數組的元素必須是相同類型,不允許出現混合類型
- 數組中的元素可以是任何數據類型,包括基本類型和引用類型
- 數組變量屬引用類型,數組也可以看成是在堆中的,因此數組無論保存原始類型還是其他對象類型,數組對象本身是在堆中的。
數組邊界
- 下標的合法區(qū)間:【0,length-1】,如果越界就會報錯
- ArrayIndexOutOfBounds Exception:數組下標越界異常
- 小結
- 數組是相同數據類型的有序集合
- 數組也是對象。數組元素相當于對象的成員變量。
- 數組長度是確定的,不可變的。如果越界,則報錯》ArrayIndexOutOfBoundsException
多維數組
- 多維數組可以看成是數組的數組
//二維數組 兩行五列
int a[][] = new int[2][5];
package com.sxl.array;
public class Demo04 {
public static void main(String[] args) {
//二維數組
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類
- 數組工具類
java.util.Arrays Arrays類中的方法都是static靜態(tài)方法,在使用的時候可以直接使用類名進行調用- 對數組排序:
sort方法。 - 比較數組:通過
equals方法比較數組中元素是否相等
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)初始化 :包含默認初始化
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("=================");
//增強循環(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};
//打印所有數據元素
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
//計算所有數據元素的和
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);
}
}
稀疏數組
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;
//輸出原始數組
for (int[] a: array) {
for (int b: a) {
System.out.print(b+"\t");
}
System.out.println();
}
//獲取有效值的個數
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("有效值的個數是:" +sum);
//創(chuàng)建一個稀疏數組 第一行存放: 行數 列數 有效數個數
int[][] array2 = new int[sum+1][3];
array2[0][0] = 11; //行數
array2[0][1] = 11; //列數
array2[0][2] = sum; //有效數個數
//遍歷原始二維數組,將非零數組存入稀疏數組中
//加入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("輸出稀疏數組");
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();
}
}
}
總結
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!
相關文章
IDEA使用MyBatisCodeHelperPro來generator代碼的詳細教程
這篇文章主要介紹了IDEA使用MyBatisCodeHelperPro來generator代碼的詳細教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
詳解SpringMVC?HandlerInterceptor攔截器的使用與參數
本文主要介紹了詳解SpringMVC?HandlerInterceptor攔截器的使用與參數,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
解決@PostConstruct注解導致的程序無法啟動(@PostConstruct的執(zhí)行)
這篇文章主要介紹了解決@PostConstruct注解導致的程序無法啟動(@PostConstruct的執(zhí)行)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

