Java数组——一维二维数组的声明及引用以及数组元素的引用

1 数组的概念

  • 数组可以看成是多个相同类型数据组合,对这些数据的统一管理。
  • 数组变量属引用类型,数组也可以看成是对象,数组中的每个元素相当于该对象的成员变量。
  • 数组中的元素可以是任何数据类型,包括基本类型和引用类型。

2 一维数组的声明

  • 一维数组的声明方式:type var[]type[] var
      例如:int a1[];   int[] a2;
          double b[];
          Person[] p1; String s1[];
  • Java语言中声明数组时不能指定其长度(数组中元素的个数),例如:int a[5];//非法
  • Java中使用关键字new 创建数组对象,格式为:数组名 = new 数组元素的类型[数组元素的个数]
    public class Test {
        public static void main(String[] args){
            int[] s;
            s = new int[5];
            for (int i=0; i < 5;i++){
                s[i] = i;
            }
        }
    }
  • 元素为引用数据类型的数组中的每一个元素都需要实例化。
    这里写图片描述

3 一维数组的初始化

  • 动态初始化:数组定义与为数组元素分配空间和赋值的操作分开进行,先分配空间再赋值。
    public class Test {
        public static void main(String[] args){
            int[] a;
            a = new int[3];
            for (int i=0;i<3;i++){
                a[i]=i;
            }
            Date[] day;
            day = new Date[3];
            for (int i=0;i<3;i++){
                day[i]= new Date(2016,10,i+1);
            }
        }
    }

    class Date{
        int year;int month;int day;
        Date(int y,int m,int d){
            year = y; month = m; day = d;
        }
    }
  • 静态初始化:在定义数组的同时就为数组元素分配空间并赋值。
    public class Test {
        public static void main(String[] args){
            int[] a = {3,9,8};
            Date[] day = {new Date(2016,10,1),
                                 new Date(2016,10,2),new Date(2016,10,3)};
            }
        }
    }

    class Date{
        int year;int month;int day;
        Date(int y,int m,int d){
            year = y; month = m; day = d;
        }
    }

4 数组元素的默认初始化
 数组是引用类型,它的元素相当于类的成员变量,因此数组分配空间后,每个元素也被按照成员变量的规则被隐式初始化。
 

    public class Test {
        public static void main(String[] args){
            int[] a = new int[5];
            Date[] days = new Date[3];
            System.out.println(a[3]);//0
            System.out.println(days[2]);//null
        }
    }

    class Date{
        int year;int month;int day;
        Date(int y,int m,int d){
            year = y; month = m; day = d;
        }
    }

 
5 数组元素的引用

  • 定义并用运算符new为之分配空间后,才可以引用数组中的每个元素。
  • 每个数组都有一个**属性**length指明它的长度,例如a.length的值为数组a的长度(元素个数),不是方法,与String的length()做区别。

选择排序(从小到大):遍历一次找出最小的数,放到起始位置

int类型排序:

public class NumSort{
    public static void main(String[] args){
        int[] a = new int[args.length];
        for (int i=0;i<args.length;i++){
            a[i]=Integer.parseInt(args[i]);
        }
        print(a);
        //selectSort(a);
        bubbleSort(a);      
        print(a);
    }
    //选择排序
    private static void selectSort(int[] a){
        int k,temp;
        for(int i=0;i<a.length;i++){
            k=i;
            for(int j=k+1;j<a.length;j++){
                if (a[k]>a[j]){
                    k = j;
                }
            }
            if (k!=i){
                temp = a[i];
                a[i] = a[k];
                a[k] = temp;
            }
        }
    }
    //冒泡排序
    private static void bubbleSort(int[] a){
        int len=a.length;
        for(int i=len-1;i>=1;i--){
            for (int j=0;j<=i-1;j++){
                if (a[j]>a[j+1]){
                    int temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
    }   

    private static void print(int[] a){
        for(int i=0;i<a.length;i++){
            System.out.print(a[i]+" ");
        }
        System.out.println();
    } 
}

运行结果:这里写图片描述

引用对象的排序:

public class TestDataSort{
    public static void main(String[] args){
        Date[] days = new Date[5];
        days[0] = new Date(2006,5,4);
        days[1] = new Date(2006,7,4);
        days[2] = new Date(2008,5,4);
        days[3] = new Date(2004,5,9);
        days[4] = new Date(2004,5,4);

        bubbleSort(days);
        //selectSort(days);

        for(int i=0;i<days.length;i++){
            //直接打印对象的引用会调用toString
            System.out.println(days[i]);
        }
    }
    //冒泡排序
    public static Date[] bubbleSort(Date[] a){
        int len = a.length;
        for (int i=len-1;i>=1;i--){
            for(int j=0;j<=i-1;j++){
                //如果a[j]>a[j+1]
                if (a[j].compare(a[j+1])>0){
                    Date temp = a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }           
        }
        return a;
    }
    //选择排序
    public static Date[] selectSort(Date[] a){
        int k;
        Date temp;
        for (int i=0;i<a.length;i++){
            k=i;
            for (int j=k+1;j<a.length;j++){
                if (a[k].compare(a[j])>0){
                    k=j;
                }
            }
            if (k!=i){
                temp=a[i];
                a[i]=a[k];
                a[k]=temp;
            }
        }
        return a;
    }
}

class Date{
    int year,month,day;
    Date(int y,int m,int d){
        year=y;
        month=m;
        day=d;
    }
    public int compare(Date date){
        return year > date.year ? 1
                    : year < date.year ? -1 
                    : month > date.month ? 1
                    : month < date.month ? -1
                    : day > date.day ? 1
                    : day < date.day ? -1:0;
    }
    //重写toString()
    public String toString(){
        return"Year:Month:Day --"+year+"-"+month+"-"+day;   
    }   
}

运行结果:这里写图片描述

二分法: 找到12所在的位置

public class TestSearch{
    public static void main(String[] args){
        //二分法一定要在排好序的前提下进行
        int a[] = {1,3,6,8,9,10,12,18,20,34};
        int i = 12;
        System.out.println(binarySearch(a,i));
    }

    public static int search(int[] a,int num){
        for (int i=0;i<a.length;i++){
            if(a[i] == num)
                return i;
        }
        return -1;
    }

    public static int binarySearch(int[] a, int num){
        if (a.length == 0){
            return -1;          
        }
        int startPos =0;
        int endPos = a.length-1;
        int m=(startPos+endPos)/2;
        while (startPos<= endPos){
            if (num ==a[m]){
                return m;
            }
            if (num > a[m]){
                startPos = m+1;
            }
            if (num < a[m]){
                endPos = m-1;
            }
            m = (startPos + endPos)/2;
        }

        return-1;
    }

}

6 二维数组
  二维数组可以看成以数组为元素的数组,例如:int [][] ={{1,2},{3,4,5,6},{7,8,9}};
  Java中多维数组的声明和初始化应按从高维到低维(从左到右)的顺序进行,例如: 

    int a[][] = new int[3][];
    a[0] = new int[2];
    a[1] = new int[4];
    a[2] = new int[3];
    int t1[][] = new int[][4];//非法

  这里写图片描述

  静态初始化:int A[][] = {{1,2},{2,3},{3,4,5}};

  动态初始化:   

    int a[][] = new int[3][5];
    int b[][] = new int[3][];
    b[0] = new int[2];
    b[1] = new int[3];
    b[2] = new int[5];

例子程序:

public class TestArray{
    public static void main(String[] args){
        int a[][]={{1,2},{3,4,5,6},{7,8,9}};
        for (int i=0;i<a.length;i++){
            for(int j=0;j<a[i].length;j++){
                System.out.print("a["+i+"]["+j+"]=" +a[i][j]+" ");
            }
            System.out.println();
        }       
    }
}

运行结果:这里写图片描述

String类型的二维数组:

public class TestString{
    public static void main(String[] args){
        String[][] s = new String[3][];
        s[0] = new String[2];
        s[1] = new String[3];
        s[2] = new String[2];
        for (int i =0;i<s.length;i++){
            for(int j=0;j<s[i].length;j++){
                s[i][j] =new String("我的位置是:"+i+","+j);
            }
        }
        for(int i=0;i<s.length;i++){
            for (int j=0;j<s[i].length;j++){
                System.out.print(s[i][j]+" ");
            }
            System.out.println();
        }
    }
}

运行结果:这里写图片描述

  
  数组的拷贝:

  • 使用java.lang.System类的静态方法
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length);
  • 可以用于数组src从第srcPos项元素开始的length个元素拷贝到目标数组从destPos项开始的length个位置
  • 如果源数据数目超过目标数组边界会抛出IndexOutOfBoundsException异常。

例子程序:

public class TestArrayCopy{
    public static void main(String[] args){
        String[] s ={"Microsoft","IBM","Sun","Oracle","Apple"};
        String[] sBak = new String[6];
        System.arraycopy(s,0,sBak,0,s.length);

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

        System.out.println();
        int[][] intArray = {{1,2},{1,2,3},{3,4}};
        int[][] intArrayBak = new int[3][];
        System.arraycopy(intArray,0,intArrayBak,0,intArray.length);
        intArrayBak[2][1] = 100;
        //虽然没有直接对intArray进行操作,但intArray[2][1]的值也会发生变化
        for(int i=0;i<intArray.length;i++){
            for(int j=0;j<intArray[i].length;j++){
                System.out.print(intArray[i][j]+" ");
            }
            System.out.println();
        }
    }
}

运行结果:这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值