1. 程式人生 > >一維陣列的初始化與遍歷

一維陣列的初始化與遍歷

       這裡主要寫一些一維陣列的初始化操作與遍歷方式,給初學者一些幫助,同時對自己也算是總結吧。

       一維陣列的初始化分為兩種:1、基本型別陣列的初始化;2、引用型別陣列的初始化;

        至於遍歷方式也分兩種:1、普通的for(){}迴圈;2、foreach迴圈;

        先說,基本型別陣列的初始化,在內部會用這兩中遍歷方式遍歷陣列:

package com.google.array.array01;

/**
 * 基本型別陣列:陣列中的元素是基本型別的資料,這種陣列就成為基本型別的陣列:
 * 有8種:byte,short,int,long,float,double,char,boolean
 */
public class BaseTypeArray {

    public static void main(String[] args){

        //先定義一個數組:arr[0]=1,arr[1]=8,arr[2]=4,arr[3]=3,arr[4]=25,arr[5]=47,arr[6]=45,
        int[] arr={1,8,4,3,25,47,45};

        //使用for迴圈遍歷陣列:1,8,4,3,25,47,45,
        circulation01(arr);


        //使用foreach迴圈遍歷陣列:
        circulation02(arr);
    }


    /**
     * 陣列的定義:用兩種方式
     * 這裡使用int型別
     */
    public static void defineArray(){
        int[] arr;
        int arr02[];
    }

    /**
     * 陣列的賦值:3種
     */
    public static void initArray(){

        //第一種賦值方式:
        int[] arr = new int[3];
        arr[0]=0;
        arr[1]=1;
        arr[2]=2;

        //第二種賦值方式:
        int[] arr02 = new int[]{0,1,2};

        //第三種賦值方式:
        int[] arr03={0,1,2};
    }

    /**
     * for迴圈遍歷陣列
     * @param arr
     */
    public static void circulation01(int[] arr){

        for(int i=0;i<arr.length;i++){
            System.out.print("arr["+i+"]="+arr[i]+",");
        }
    }


    /**
     * foreach遍歷陣列
     * @param arr
     */
    public static void circulation02(int[] arr){

        for(int a:arr){
            System.out.print(a+",");
        }
    }
}

再說引用型別的陣列,同樣用for和foreach遍歷:

先定義一個實體類:

package com.google.array.array02;

public class Student {

    private String name;
    private int age;



    public Student() {
    }
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }



    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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

然後在對這個引用型別的實體類進行操作:

package com.google.array.array02;

/**
 * 引用型別陣列的引用:
 */
public class RefTypeArray {

    public static void main(String[] args){
        /**
         *   student[0]=Student{name='張1', age=1}
             name-0=張1
             age-0=1
             student[1]=Student{name='張2', age=2}
             name-1=張2
             age-1=2
             student[2]=Student{name='張3', age=3}
             name-2=張3
             age-2=3
         */
        dynamicInit();

        /**
         *  Student{name='趙6', age=12}
            Student{name='趙7', age=15}
            Student{name='趙8', age=17}
         */
        staticInt();
    }

    /**
     * 動態初始化引用型別的陣列:
     */
    public static void dynamicInit(){

        //建立學生物件:
        Student stu1=new Student("張1",1);
        Student stu2=new Student("張2",2);
        Student stu3=new Student("張3",3);

        //建立一個數組容器,尺寸為3,用來存放學生元素:
        Student[] student=new Student[3];
        student[0]=stu1;
        student[1]=stu2;
        student[2]=stu3;

        print1(student);
    }

    /**
     * 靜態初始化陣列:
     */
    public static void staticInt(){

        //第一種方式:
        Student[] student={new Student("趙6",12),new Student("趙7",15),new Student("趙8",17)};

        //第二種方式:有點麻煩
        /*Student stu1=new Student("趙6",12);
        Student stu2=new Student("趙7",15);
        Student stu3=new Student("趙8",17);
        Student[] student2={stu1,stu2,stu3};*/

        print2(student);
    }

    /**
     * 採用for迴圈:
     * @param student
     */
    public static void print1(Student[] student){

        for(int i=0;i<student.length;i++){
            System.out.println("student["+i+"]="+student[i]);
            System.out.println("name-"+i+"="+student[i].getName());
            System.out.println("age-"+i+"="+student[i].getAge());
        }


    }

    /**
     * 採用foreach迴圈:
     * @param student
     */
    public static void print2(Student[] student){

        for(Student stu:student){
            System.out.println(stu);
        }
    }


}

至於記憶體分析,這裡就不說了,只要記住,陣列是一個引用,

對於基本型別,內部存放的是數值;

對於引用型別,內部存放的仍然是一個引用;