Java數(shù)組

注:數(shù)組長度一旦確定后就不能修改

一.
1.Java數(shù)組是順序表的數(shù)據(jù)結(jié)構(gòu)

  1. 數(shù)組(Array),是多個相同類型數(shù)據(jù)按一定順序排列的集合
    注: 數(shù)組里面的元素可以是任何數(shù)據(jù)類型,包括基本數(shù)據(jù)類型和引用數(shù)據(jù)類型

二. 數(shù)組的初始化兩種方法:

一般變量的初始化和聲明:
int num;//聲明
num = 10;//初始化

int id = 1001;//聲明 + 初始化


一維數(shù)組:

靜態(tài)初始化:數(shù)組的初始化和數(shù)組元素的賦值操作同時進行
int[] ids;//聲明一個數(shù)組,里面的元素必須是int的類型
ids = new int[]{1001,1002,1003,1004}; //數(shù)組 是 引用數(shù)據(jù)類型,賦值必須要new,
string 類型賦值本來也是要有new的,只不過Java內(nèi)部幫你省了,當然,還是可以加上new的

或者

int[] ids = new int[]{1001,1002,1003,1004};

2.動態(tài)初始化:數(shù)組的初始化和數(shù)組元素的賦值操作分開進行
String[] names = new String[5];
names[0] = "王銘";
names[1] = "王赫";
names[2] = "張學良";
names[3] = "孫居龍";

注:
int[] a ;
a[0] = 0;
a[1] = 1;
以上這樣是報錯的,數(shù)組一定要指定長度的 或者 直接初始化值的

多維數(shù)組:

//靜態(tài)初始化
int[][] arr1 = new int[][]{{1,2,3},{4,5},{6,7,8}};

//動態(tài)初始化1
String[][] arr2 = new String[3][2];

//動態(tài)初始化2
String[][] arr3 = new String[3][]; //注:第二個[]為空,就是第一維數(shù)組里面的元素必須是數(shù)組

int[][] arr = new int[3][];
int b = 11;
int[] a = new int[]{1,2,3};

arr[0]; // null
arr[0] = a 或 arr[0] = new int[6] ; //第一維數(shù)組里面的元素必須是數(shù)組,是整數(shù)的話會報錯


以下這種情況要熟悉:
float[][] arr1 = new float[4][3];
System.out.println(arr1[0]);//地址值
System.out.println(arr1[0][0]);//0.0

double[][] arr3 = new double[4][];
System.out.println(arr3[1]);//null
System.out.println(arr3[1][0]);//報錯


提示:
一維數(shù)組:int[] x或者int x [] 寫法都可以
二維數(shù)組:int[][] y或者int[] y[]或者int y [][] 這三種寫法都可以

三. 獲取數(shù)組的元素 和 長度、遍歷數(shù)組
1.如何調(diào)用數(shù)組的指定位置的元素:通過角標的方式調(diào)用。
//數(shù)組的角標(或索引)從0開始的,到數(shù)組的長度-1結(jié)束。

2.獲取數(shù)組的長度。
//屬性:length

float[][] arr1 = new float[4][3];
arr1.length ; // 4
arr1[0].length // 3

3.遍歷數(shù)組
for(int i = 0;i < names.length;i++){
System.out.println(names[i]);
}

二維遍歷:

for(int i = 0;i < arr.length;i++){
for(int j = 0;j < arr[i].length;j++){
arr[i][j];
}
}

四. 數(shù)組元素的默認初始化值 : 數(shù)組動態(tài)初始化后,如果不給數(shù)組賦值的話,它默認是有值的 (注:和類的屬性一樣,都是有默認值的,類的屬性默認初始化值也是這些)

    > 數(shù)組元素是整型:0
    > 數(shù)組元素是浮點型:0.0
    > 數(shù)組元素是char型:ASCII值為0的值 或 '\u0000',而非'0'
    > 數(shù)組元素是boolean型:false

    > 數(shù)組元素是引用數(shù)據(jù)類型:null

聲明多個數(shù)組:
int[] array1,array2;

int[] age = new int[5];
for(int i = 0; i<age.length;i++){
System.out.print(age[i]);
}
: 00000

五 . 數(shù)組的復(fù)制

//數(shù)組的復(fù)制(區(qū)別于數(shù)組變量的賦值:arr1 = arr)
String[] arr1 = new String[arr.length];
for(int i = 0;i < arr1.length;i++){
arr1[i] = arr[i];
}

六.數(shù)組的常見異常

數(shù)組中的常見異常:

    1. 數(shù)組角標越界的異常:ArrayIndexOutOfBoundsExcetion
      int[] arr = new int[]{1,2,3,4,5};

// for(int i = 0;i <= arr.length;i++){
// System.out.println(arr[i]);
// }

// System.out.println(arr[-2]);
// System.out.println(arr[5]);

    1. 空指針異常:NullPointerException
      空指針異常:NullPointerException
      //情況一:
      // int[] arr1 = new int[]{1,2,3};
      // arr1 = null;
      // System.out.println(arr1[0]);

      //情況二:
      // int[][] arr2 = new int[4][];
      // System.out.println(arr2[0][0]);

      //情況三:
      String[] arr3 = new String[]{"AA","BB","CC"};
      arr3[0] = null;
      System.out.println(arr3[0].toString());

七. 數(shù)組也是嚴格遵守類型的

注:數(shù)組變量 里面,存放的不僅是地址值,還有類型等

int[] arr = new int[4];
float[] arr1 = new float[4];
arr = arr1; //雖然arr 和arr1里面存放的都是內(nèi)存地址,但是arr和arr1指向的數(shù)組類型不同,所以不能賦值

int[] a = new int[]{1,2,3,4};
int [] b = a; //沒問題
int c = a; //報錯

同理:
Person p1 = new Person();
Person p2 = new Person(); //沒毛病
int p3 = p1 ; //報錯

int[] arr = new int[4];
int[][] arr1 = new int[4][3];
arr = arr1; //報錯,一個一維,另一個二維,類型不同

3.二維數(shù)組第一層元素類型必須為數(shù)組,其他類型會報錯

正確的:
int[] arr = new int[4];
int[][] arr1 = new int[4][3];
arr1[0] = arr;

錯誤的:
int[] arr = new int[4];
int[][] arr1 = new int[4][3];
arr1[0] = 11;

八.對象數(shù)組
class Person {

String name;
int age;
/**
 * sex:1 表明是男性
 * sex:0 表明是女性
 */
int sex;

public void study(){
    System.out.println("studying");
}

public int showAge(){
    System.out.println("age:" + age);
    return 1111111;
}

public  int addAge(int i){
    System.out.println(showAge());
    
    age += i;
    return age;
}

}

class Persons {

String name;
int age;
/**
 * sex:1 表明是男性
 * sex:0 表明是女性
 */
int sex;

public void study(){
    System.out.println("studying");
}

public int showAge(){
    System.out.println("age:" + age);
    return 1111111;
}

public  int addAge(int i){
    System.out.println(showAge());
    
    age += i;
    return age;
}

}

Person[] a = new Person[5];
Person p1 = new Person();
Persons p2 = new Persons();
a[0] = p1; //沒問題
a[1] = p2; //報錯,數(shù)組a 里面只能放Person類的實例對象

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 數(shù)組的概念: 數(shù)組是一個容器,存放一組相同數(shù)據(jù)類型變量的容器 數(shù)組的格式: 數(shù)據(jù)類型[] 數(shù)組名; Java寫法 ...
    i_4178閱讀 541評論 0 0
  • 從今天開始,還是想好好學一門語言,然后再學其他的,學習的內(nèi)容基本都是從各種博客網(wǎng)站上copy的,但是看一遍鞏固記憶...
    小丸子sherry閱讀 383評論 0 0
  • 數(shù)組類型和數(shù)組引用變量詳解 數(shù)組類型為什么要用數(shù)組?Java數(shù)組的兩大特征:定義數(shù)組時,不能指定數(shù)組的長度變量分為...
    Ansaxnsy閱讀 2,976評論 2 3
  • C數(shù)組 一維數(shù)組: 定義方式: datatype arrayname[length] 數(shù)組是一個整體,在內(nèi)存中是連...
    Big_data閱讀 1,646評論 0 3
  • 在ViewPager中使用Fragment的情況下,可以給ViewPager設(shè)置兩種Adapter,一種是Frag...
    Rreply閱讀 5,957評論 1 6

友情鏈接更多精彩內(nèi)容