Java数组

一、Java数组定义分类声明的优点

1、数组的定义

数组是有序数据的集合,数组中的每个元素具有相同的数组名和下标来做唯一标识。

2、数组的分类

一维数组、二维数组、多维数组

3、数组的优点

  • 不使用数组定义100个整型变量:int i1,int i2,int i3......int i100;
  • 使用数组:int i[100] (注意这个是伪代码)

二、Java数组内存分配

1、声明数组

  • 声明形式一:type arrayName[];
  • 声明形式二:type[] arrayName;

示例:

//声明数组
int arrayDemo[];
int[] score;

2、内存的分配

  • 为数组分配内存空间,如果不分配内存,将不能访问它的任何元素。我们使用new关键字 来为数组分配内存空间
声明及分配空间.jpg
代码
public class Test35 {
    public static void main(String[] args) {    
        int[] score = null;  //数组的声明
        score=new int[3];//为数组开辟内存空间,实例化
        for (int i = 0; i < 3; i++) {
            System.out.println(score[i]);
        }
    }
}
结果:
0
0
0
由于数组没有被赋值,所以显示默认值,int类型的默认值是0,string类型的默认值是空。

赋值:
public class Test35 {
    public static void main(String[] args) {    
        int[] score = null;  //数组的声明
        score=new int[3];//为数组开辟内存空间,实例化
        
        //数组的下标是从0开始的
        //score.length表示score数组的长度
        for (int i = 0; i < score.length; i++) {
            score[i]=i*2+1;
        }
        for (int i = 0; i < score.length; i++) {
            System.out.println(score[i]);
        }
    }
}
结果:
1
3
5

三、Java数组静态初始化

  • 数组初始化分为两种:动态初始化、静态初始化
  • 之前我们所创建的数组,所采用的方式都是动态初始化,也就是所有的内容不会具体制定,都是默认值。
  • 静态初始化是指在数组创建之初直接制定其内容。
代码
public class Test36 {
    public static void main(String[] args) {
        //静态初始化
        int score[]={2,5,6,4,6,7};  //声明
        for (int i = 0; i < score.length; i++) {
            System.out.println(score[i]);
        }
    }
}
结果:
2
5
6
4
6
7

四、Java数组的使用

【示例-1】求取数组中的最大值和最小值。

代码
public class Test37 {
    public static void main(String[] args) {
        int score[] = {43,34,5,66,12};
        int max,min;
        max = min = score[0];
        for (int i = 0; i < score.length; i++) {
            if (score[i]>max) {
                max = score[i];
            }
            if (score[i]< min) {
                min = score[i];
            }
        }
        System.out.println("最大值:"+max);
        System.out.println("最小值:"+min);
    }
}
结果:
最大值:66
最小值:5

【示例-2】冒泡排序

代码
public class Test38 {
    public static void main(String[] args) {
        int score[] = {12,45,23,10,100};
        for (int i = 0; i < score.length-1; i++) {
            for (int j = i+1; j < score.length; j++) {
                if (score[i]<score[j]) {
                    int temp = score[i];
                    score[i] = score[j];
                    score[j] = temp;
                }
            }
            System.out.print("第"+(i+1)+"次排序:");
            for (int j = 0; j < score.length; j++) {
                System.out.print(score[j]+"  ");
            }
            System.out.println("");
        }
        System.out.print("最终结果:");
        for (int i = 0; i < score.length; i++) {
            System.out.print(score[i]+"  ");
        }
    }
}
结果:
第1次排序:100  12  23  10  45  
第2次排序:100  45  12  10  23  
第3次排序:100  45  23  10  12  
第4次排序:100  45  23  12  10  
最终结果:100  45  23  12  10  

五、Java二维数组声明内存分配介绍及使用

1、声明

  • 如果把一维数组看成是线性图形,那么二维数组就是一个平面图形
  • 二维数组的声明和一维数组类似,内存分配也是使用new关键字。
  • 声明与分配内存:
    声明:type arrayName[][];
    初始化:arrayName[][]=new type[行][列]; //注意:行、列是方便理解加上的
代码

没有赋值:

public class Test39 {
    public static void main(String[] args) {
        int score[][];
        score = new int[5][5];
        //也可以写成:int score[][] = new int[5][5];
        for (int i = 0; i < score.length; i++) {
            for (int j = 0; j < score[i].length; j++) {
                System.out.print(score[i][j]+"  ");
            }
            System.out.println();
        }
    }
}
结果:
0  0  0  0  0  
0  0  0  0  0  
0  0  0  0  0  
0  0  0  0  0  
0  0  0  0  0  

赋值之后:
public class Test39 {
    public static void main(String[] args) {
        int score[][];
        score = new int[5][5];
        score[0][0] = 9;
        score[0][3] = 8;
        score[3][2] = 7;
        for (int i = 0; i < score.length; i++) {
            for (int j = 0; j < score[i].length; j++) {
                System.out.print(score[i][j]+"  ");
            }
            System.out.println();
        }
    }
}
结果:
9  0  0  8  0  
0  0  0  0  0  
0  0  0  0  0  
0  0  7  0  0  
0  0  0  0  0  

2、静态初始化

二维数组静态初始化:type arrayName[][]={{valuse},{valuse},{valuse}};

例:
int score[][] = {{100,90},{67,70},{50,78,80}};


图.jpg
代码
public class Test40 {
    public static void main(String[] args) {
        int score[][] = {{100,90},{67,70},{50,78,80}};
        for (int i = 0; i < score.length; i++) {
            for (int j = 0; j < score[i].length; j++) {
                System.out.print(score[i][j]+"  ");
            }
            System.out.println();
        }
    }
}
结果:
100  90  
67  70  
50  78  80   
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 数组类型和数组引用变量详解 数组类型为什么要用数组?Java数组的两大特征:定义数组时,不能指定数组的长度变量分为...
    Ansaxnsy阅读 2,912评论 2 3
  • 05.01_Java语言基础(数组概述和定义格式说明)(了解) A:为什么要有数组(容器)为了存储同种数据类型的多...
    苦笑男神阅读 632评论 0 0
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,780评论 18 399
  • 终于下雪了!下了雪的冬天才有冬天的味道。 地面上、房顶上、树枝上,到处银装素裹。小时候,最喜欢在雪地里奔跑,瞅瞅自...
    笑对红尘阅读 642评论 18 30