java 二維數組和對象數組


1.二維數組:二維數組就是存儲一維數組(內存地址/引用)的數組

2.二維數組的初始化

1) int intA[][]={{1,2},{2,3},{3,4,5}};


2) int [][] intB=new int[3][5];


3) int []intC []=new int[3][];
intC[0]=new int[2];
intC[1]=new int[3];
intC[2]=new int[5];

 1 public class TestArray2 {
 2     public static void main(String[] args) {
 3         //二維數組不是規則的矩陣
 4         int [] intA[] ={{1,2},{2,3,4},{3,4,5,6}};
 5         System.out.println(intA);//[[I@5e265ba4 兩個[[表示是二維的 I表示數組是int型 @5e265ba4是內存地址
 6         
 7         //聲明一一個二維數組,用於存儲3個一維數組,每一個一維數據存多少個數組,不知道 ,null
 8         int [][]intB=new int[3][];
 9         intB[0]=new int[3];  //3
10         intB[1]=new int[]{1,2,3,4};  //3
11         intB[2]=new int[2];  //2
12         System.out.println(intB[1]);//[I@156643d4
13         System.out.println(intB[1][1]);//2
14         
15         //聲明一個二維數組,同時創建出一維數組,每個一維數組的長度均相同
16         //存儲三個一維數組,每個一維數組的長度為4
17         int []intC[]=new int[3][4];
18         System.out.println(intC);
19         
20     }
21 }

 

3.二維數組的遍歷

1) 普通 for 循環
2) 加強 for 循環
3) 普通 for 循環+加強 for 循環

public class TestArray3 {
    public static void main(String[] args) {
        //二維數組
        int [] intA[] ={{1,2},{2,3,4},{3,4,5,6}};
        //(1)普通for循環
        for(int i=0;i<intA.length;i++){ //0,1,2
            for(int j=0;j<intA[i].length;j++){ //每一個一維數組的長度
                System.out.print(intA[i][j]+"\t");
            }
            System.out.println();
        }
        //(2)加強for循環
        System.out.println("\n=========================");
        for(int[] arr:intA){  //int[]二維數組中元素的類型, arr迭代變量, intA二維組的名稱
            for(int i:arr){ //int,一維數組中元素的類型,i,迭代變量,arr,一維數組的名稱
                System.out.print(i+"\t");
            }
            System.out.println();
        }
        //(3)普通與加強for循環的混搭
        System.out.println("\n-------------------------");
        for(int [] arr:intA){//加強for
            for(int i=0;i<arr.length;i++){
                System.out.print(arr[i]+"\t");
            }
            System.out.println();
        }
        System.out.println("\n--------------------------");
        for(int i=0;i<intA.length;i++){
            for(int j:intA[i]){//加強for
                System.out.print(j+"\t");
            }
            System.out.println();
        }
    }
}
View Code

 

4.對象數組存儲表格數據

張三   19   男
李四   20   女
王五   28   男

數組存基本數據類型,也可以存引用數據類型
對象數組:使用數組存儲對象(自定義對象)

public class Person {
    private String name;//姓名
    private int age;//年齡
    private String gender;//性別
    
    //重寫toString以打印想要的輸出 否則只會打印對象的內存地址
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return name+"\t"+age+"\t"+gender;//
    }
    
    public Person(String name, int age, String gender) {
        super();
        this.name = name;
        this.age = age;
        this.gender = gender;
    }
    public Person() {
        super();
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
View Code

 

 1 public class TestArray {
 2     public static void main(String[] args) {
 3         //創建一個Person類型的數組,用於存儲3個Person類型的對象
 4         Person [] pers=new Person[3];
 5         //創建Person類型的對象
 6         Person p1=new Person("張三", 19, "男");
 7         //將p1對象存儲到Person類型的數組中
 8         pers[0]=p1;
 9         
10         pers[1]=new Person("李四", 20, "女");
11         pers[2]=new Person("王五",28,"男");
12         
13         for(int i=0;i<pers.length;i++){
14             System.out.println(pers[i]);
15         }
16         //對象數組中存儲的是對象的引用(內存地址)
17     }
18 }

 

 

5.數組拷貝

地址的拷貝
int[]array={11,22,33,44};
int[] arrayB=new int[5];
arrayB=array   //兩個數組指向同一內存

 

值的拷貝
int[]array={11,22,33,44};
int[] arrayB=new int[5];
arrayB[0]=array[0];

 

System 類中的 arraycopy(…)方法:快速,數組大時用次方法。

 1 public class TestArrayCopy3 {
 2     public static void main(String[] args) {
 3         int [] arrA={11,22,33,44};//源數組
 4         int [] arrB=new int[5];//目標數組
 5         
 6         System.out.println("數組拷貝之前");
 7         for(int num:arrB){
 8             System.out.print(num+"\t");
 9         }
10         //0        0        0        0        0    
11         //拷貝
12         System.arraycopy(arrA, 0, arrB, 1, 2);
13         
14         System.out.println("\n數組拷貝之后");
15         for(int num:arrB){
16             System.out.print(num+"\t");
17         }
18         //0        11        22        0        0
19     }
20 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM