java数组定义及使用

java数组定义及使用

3.1数组

数组是一组相关数据的集合,数组按照使用可以分为一维数组、二维数组、多维数组

有点:
不使用数组定义100个整形变量:int1,int2,int3;;;;;;
使用数组定义 int i[100]


数组定义:int i[100],只是一个伪代码,只能表示含义的。


3.2一维数组

可以存放上千万个数据,并且这些数据的类型是完全是相同的。

要使用java的数组,必须经过两个步骤:(1)声明数组,(2)分配内容给该数组,这两个步骤的语法如下:



声明形式一:

◇声明一维数组: 数据类型 数组名[] = null;
◇分配内存给数组:数组名 = 买受人数据类型[长度]

声明形式二:
◇声明一维数组: 数据类型[]  数组名  = null;

public class ArrayDemo01{
    public static void main(String args[]){
        int score[] = null;    //声明数组
        score = new int[3];    //为数组开辟空间
    }
}


变量的命名规则: 第一个单词的首字母小写,之后的每个单词的首字母大写:studentName;

详细解释:

"
栈堆
int score[] = null ;  ->null 表示引用数据类型的默认值
int score[];   此时score尚未指向具体的空间,所以score内容未知,无法直接使用。
为数组开辟空间
score = new int[3];
栈内存
堆内存
"

验证只开辟空间却不赋值时数组默认值是多少?

public class ArrayDemo01{
    public static void main(String args[]){
        int score[] = null;    //声明数组
        score = new int[3];    //为数组开辟空间
        System.out.println("score[0] = " + score[0]);
        System.out.println("score[1] = " + score[1]);
        System.out.println("score[2] = " + score[2]);
    }
}


错误显示:

public class ArrayDemo01{
    public static void main(String args[]){
        int score[] = null;    //声明数组
        System.out.println("score[0] = " + score[0]);
        System.out.println("score[1] = " + score[1]);
        System.out.println("score[2] = " + score[2]);
    }
}




如果删除score = new int[3];时显示结果将如下:
Exception int thread "main"  java.lang.NullPointerException
    at ArrayDemo01.main


一个堆内存可以被多个栈内存空间使用。

下面使用一个步骤一次性完成:
即:
声明数组的同时分配内存空间

数据类型 数组名[] = new 数据类型[个数]

int score[] = new int[10];

数组中元素的表示方法:

想要访问数组里的元素,可以利用索引来完成。
数组的访问也可通过循环方式进行操作中,循环操作的时候只需要改变期索引[下标]即可。

public class ArrayDemo01{
    public static void main(String args[]){
        int score[] = null;    //声明数组
        score = new int[3];    //为数组开辟空间
        System.out.println("score[0] = " + score[0]);
        System.out.println("score[1] = " + score[1]);
        System.out.println("score[2] = " + score[2]);
        for(int x=0;x<3;x++){
            System.out.println("score["+x+"] = " + score[x]);    
        }
    }
}




访问注意:


假设程序中取出的内容超过了这个下标,如"score[3]",则的程序运行的时候会出现以下的错误提示:
java.lang.ArrayIndexOutOfBoundsException:3

public class ArrayDemo01{
    public static void main(String args[]){
        int score[] = null;    //声明数组
        score = new int[3];    //为数组开辟空间
        System.out.println("score[0] = " + score[0]);
        System.out.println("score[1] = " + score[1]);
        System.out.println("score[2] = " + score[2]);
        for(int x=0;x<3;x++){
            System.out.println("score["+x+"] = " + score[x]);    
        }
        System.out.println("score[3] = " + score[3]);
    }
}



Excepiton  in thread "main"  java.lang.ArrayIndexOutOf BoundException : 3
        at ArrayDemo01.main



数组索引超出绑定,就是表示数组越界。


数组中默认的内容都是0,也可以通过下标的方式为数组中的内容赋值。


public class ArrayDemo02{
    public static void main(String args[]){
        int score[] = null;    //声明数组
        score = new int[3];    //为数组开辟空间

        for(int x=0;x<3;x++){     //为每个元素赋值
            score[x] = x*2+1;   //每一个值都是奇数
        }

        for(int x=0;x<3;x++){
            System.out.println("score["+x+"] = " + score[x]);    
        }
    }
}





取得数组的长度
可以使用“数组名称.length”来完成操作。


public class ArrayDemo03{
    public static void main(String args[]){
        int score[] = null;
        score = new int[3];
        System.out.println("数组长度为:" + score.length);    
    }

}




3.3数组的静态初始化

之前所看到的数组,所采用的方式都是动态初始化,即:所有的内容在数组声明的时候并不具体的制定,而都是以默认值的形式以出现。
静态初始化就是指在数组定义之后,直接制定好期内容。




操作格式:
数据类型 数组名[] = {初值0,初值1,....,初值n}

public class ArrayDemo04{
    public static void main(String args[]){
        int score[] = {91,92,93,94,95,96};        //使用静态初始化的方式初始数组。
        for(int x=0;xpublic class ArrayDemo05{
    public static void main(String args[]){
        int score[] = {67,89,87,69,90,100,75,90};  //静态初始化数组
        int max = 0;
        int min = 0;
        max = main = score[0] ;                   //把第一个元素的内容赋值给max和min
        for(int x = 0;xmax){
                max = score[x];
            }
            if(score[x]



3.4.2排序

在数组中,排序是一个比较常见的算法。
通过一个简单的范例,来了解排序的基本操作。

public class ArrayDemo06{
    public static void main(String args[]){
        int score[] = {67,89,87,69,90,100,75,90};
        for(int i=0;i



为了让大家可以更好的观察到每次的执行,下面在每次排序的时候都输出原始内容。



public class ArrayDemo07{
    public static void main(String args[]){
        int score[] = {67,89,87,69,90,100,75,90};
        for(int i=0;i



冒泡排序,自己观察运行的过程,来分析代码是如何操作的.

3.5二维数组


二维数组的声明方式和一维数组类似,内存的分配也是一样是用new这个关键字。
其声明与分配内存的格式如下所示:
1.动太初始化:
数据类型 数组名[][]
数组名 = new 数据类型[行的个数][列的个数];
2.声明加初始化
数据类型  数组名[][] = new 数据类型[行的个数][列的个数]
3.二维数组静态初始化
数据类型 数组名[][] = {{第0行初值},{第0行初值},.....{第n行初值}}


如:
int score[][] = null;        //生命
score = new int[3][4];    //分配空间

int score = new int[3][4];


public  class ArrayDemo08{
    public static void main(String args[]){
        int score[][] = new int[4][3];                //声明并实例化二维数组

        score[0][1] = 30;
        score[1][1] = 30;
        score[2][2] = 30;
        score[3][1] = 30;
        score[1][1] = 30;
        for(int i=0;i





public class ArrayDemo09{
    public static void main(String args[]){
        int score[][] = {{67,61},{78,61,83},{99,100,98,66,95}};            //静态初始化完成,每行的数组元素个数不一样
    
        for(int i=0;i




3.6多维数组

三维数组的声明为int score[][][],而四维数组为int  score[][][][].....,以此类推


public class ArrayDemo10{
    public static void main(String args[]){
        int score[][] = {
        {
            {5,1},{6,7}
        },
        {
            {9,4},{8,3}
        }
        };            //静态初始化完成,每行的数组元素个数不一样
    
        for(int i=0;i


你可能感兴趣的:(Java)