Java之数组

来源:互联网 发布:内存卡数据恢复软件 编辑:程序博客网 时间:2024/06/14 11:45

数组的概述

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

一维数组

一位数组的声明方式:

type var[]
type[] var
例:int a1[]
double b[]
Person[] p1
String s1
注意:Java语言中声明数组不能指定其长度(数组中元素的个数),例:int a[5]
一维数组创建的内存图
int[] s
这里写图片描述
s=new int[5]
for(int i = 0;i<5;i++){s[i] = i}
这里写图片描述

元素为引用数据类型的数组内存图
代码如下:

class Date{    int year;    int month;    int day;    Date(int year,int month,int day){        this.year = year;        this.month = month;        this.day = day;    }}public class Test {    public static void main(String[] args){        Date[] days;        days = new Date[3];        for(int i = 1;i<=3;i++){            days [i-1] =new Date(2017,7,i);        }    }}

这里写图片描述

数组的初始化

1、动态初始化
数组定义与为数组元素分配空间和赋值的操作时分开的(如上面例子中数组days)
2、静态初始化
在定义数组的时候就为数组元素分配空间并赋值
例如将上面代码改为

class Date{    int year;    int month;    int day;    Date(int year,int month,int day){        this.year = year;        this.month = month;        this.day = day;    }}public class Test {    public static void main(String[] args){        Date[] days = {            new Date(2017,7,1),            new Date(2017,7,2),            new Date(2017,7,3)        };    }}

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

数组元素的引用

定义并运用运算符new为之分配空间后,才可以引用数组中的每个元素,数组元素的饮用方式为

数组名 [索引(index)]
索引为数组元素下标,可以是整型常量或者整型表达式
如:a[3],b[i],c[6*i]
数组元素下标从0开始,长度为n的数组的合法下标取值范围为0~n-1

每个数组都有一个**属性**length指明它的长度
a.length的值为数组a的长度(元素个数)

数组其他

1、输出数组元素
只需要一个简单的循环就可以

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

输出结果

1 5 6 4 3 7 8 9 2 

java中main方法里面的String[] args(String args[])的意思

String[] 是指一个String类型的数组,args是函数的形参,可理解为“入口参数”,Java本身不存在不带Stringargs[]的main方法

String[] args 这个字符串数组是保存运行main函数时输入的参数的,例如main方法所在的类名为Test那么你在cmd运行 java Test a b c 时,那么args[0] = a ,args[1]=b, args[2]=c,并且可以在程序中调用这些变量

看一个例子

public class TestArray {        public static void main(String[] args) {            if (args.length < 3) {                System.out.println("Usage: java Test \"n1\" \"op\" \"n2\"");                System.exit(-1);        //非正常退出,0为正常退出            }            double d1 = Double.parseDouble(args[0]);            double d2 = Double.parseDouble(args[2]);            double d = 0;            if (args[1].equals("+"))                d = d1 + d2;            else if (args[1].equals("-"))                d = d1 - d2;            else if (args[1].equals("x"))                d = d1 * d2;            else if (args[1].equals("/"))                d = d1 / d2;            else {                System.out.println("Error operator!");                System.exit(-1);            }            System.out.println(d);        }}

执行如下
这里写图片描述
实际上就是一个实现计算的小程序

上面例子中有一个parseDouble方法解释一下
一般情况下基本数据类型分配在栈上面,但有一系列基础类型的包装类可以通过创建对象使其分配在堆内存,如class Byte,class Boolean, class Integer(包装int类型)等

可以通过这些包装类的例上例中parseDouble()方法将String类型转换为Double类型(查询API文档可知方法实际是static double parseDouble(String s)),其他基本数据类型方法类似
但是像类似于asdaf这样的字符串无法转换为Double类型,则报NumberFormatException错误

具体可参照本人另一篇博客
Java常用类之基础数据类型包装类


2、将(1)中例子排序(选择排序)后输出

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

输出结果

1 5 6 4 3 7 8 9 2 1 2 3 4 5 6 7 8 9 

方法的返回值类型可以是数组(的引用)类型,如下

public class BubbleSort {    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);        //排序前        for (int i = 0; i < days.length; i++) {            System.out.println(days[i]);        }        System.out.println();        bubbleSort(days);        //排序后        for (int i = 0; i < days.length; i++) {            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++) {                if (a[j].compare(a[j + 1]) > 0) {                    Date temp = a[j];                    a[j] = a[j + 1];                    a[j + 1] = 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;    }    public String toString() {        return "Year:Month:Day -- " + year + "-" + month + "-" + day;    }}

冒泡排序日期,输出结果

排序前Year:Month:Day -- 2006-5-4Year:Month:Day -- 2006-7-4Year:Month:Day -- 2008-5-4Year:Month:Day -- 2004-5-9Year:Month:Day -- 2004-5-4排序后Year:Month:Day -- 2004-5-4Year:Month:Day -- 2004-5-9Year:Month:Day -- 2006-5-4Year:Month:Day -- 2006-7-4Year:Month:Day -- 2008-5-4

3、排好序的数进行二分查找(简单快速)

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

二维数组

二维数组可以看成以数组为元素的数组,例如int a[][] = {{1,2},{3,4,5},{6}}

Java中多维数组的声明和初始化应按照从高维到低维的顺序进行

int a[][] = new int[3][];a[0] = new int[2];a[1] = new int[4];a[2] = new int[3];//不能空缺第一维大小,故下面定义是非法的//int[][] b = new int[][3];

内存情况如下
这里写图片描述

二维数组初始化

类似于一维数组,但是不能int a[3][2] = {{1,2},{3,4},{5,6}}[]中数字Java自己确定

String[][] s;            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();            }

输出结果

我的位置时是 0 ,0 我的位置时是 0 ,1 我的位置时是 1 ,0 我的位置时是 1 ,1 我的位置时是 1 ,2 我的位置时是 2 ,0 我的位置时是 2 ,1 

数组的拷贝

使用的是Java.lang.System类的静态方法

public static void arraycopy    (Object src,int srcPos,Oject dest,int destPos,int length)

数组src从第srcPos项元素开始的length个元素拷贝到目标数组从destPos项开始的length个位置

如果源数据数目超过目标数组边界会抛出IndexOutOfBoundsException异常

public class TestArrayCopy {    public static void main(String args[]) {        String[] s = { "Mircosoft", "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;//修改的是同一个值        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();        }    }}

输出结果

Mircosoft IBM Sun Oracle Apple null(当引用为空值时为null1  2  1  2  3  3  100  
原创粉丝点击