Java 学习笔记day5_数组

<mark style="box-sizing: border-box; background: rgb(255, 255, 0); color: rgb(0, 0, 0);">day5</mark>

Eclipse的使用配置

https://www.eclipse.org/downloads/
<mark style="box-sizing: border-box; background: rgb(255, 255, 0); color: rgb(0, 0, 0);">day5</mark>

Eclipse的使用配置

https://www.eclipse.org/downloads/

数组

数组(Array)是多个相同类型数据按一定顺序排列的集合,并使用一个名字命名,并通过编号的方式对这些数据进行统一管理。

常见概念:数组名、角标、下标、索引、元素、数组长度

数组特点:

  • 数组是有序排列的

  • 数组属于引用数据类型的变量。数组的元素既可以是基本数据类型,也可以是引用数据类型

  • 创建数组对象会在内存中开辟一整块连续空间

  • 数组的长度一旦确定就不能修改

数组分类:

  • 按照维度:一维数组、二维数组……

  • 按照数组元素类型:基本数据类型元素的数组、引用数据类型元素的数组

一维数组

  1. 一维数组的声明和初始化

    • 数组一旦初始化完成,其长度就确定了

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n161" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> public Static void main(String[] args){
int[] id;
//静态初始化:数组的初始化和数组元素的赋值操作同时进行
id = new int[]{1,2,3,4,5};
//int[] id = {1,2,3,4,5};
//动态初始化:数组的初始化和数组元素的赋值操作分开进行
String[] name = new String[3];
}</pre>

  1. 如何调用数组的指定位置的元素

    • 数组的角标(或索引)从 0 开始,到数组长度的 -1 结束

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n169" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> name[0] = "张三";
name[1] = "里斯";
name[2] = "汪芜";</pre>

  1. 如何获取数组的长度

    • 属性:length

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n177" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> System.out.println(name.length);//3
System.out.println(id.length);//5</pre>

  1. 如何遍历数组

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n182" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> for(int i = 0;i < name.length;i++){
System.out.println(names[i]);
}</pre>

  1. 数组元素的默认初始化值

    • 数组元素是整型:0

    • 浮点型:0.0

    • char型:0 或/u0000而非‘0’

    • boolean型:false

    • 引用数据类型:null

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n198" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> public class ArrayTest{
public static void main(String[] args){
int[] arr = new int[4];
for(int i = 0;i <arr.length;i++{
System.out.println(arr[i]);
}

short[] arr1 = new short[4];
for(int i = 0;i <arr1.length;i++{
System.out.println(arr1[i]);
}

char[] arr2 = new char[4];
for(int i = 0;i <arr2.length;i++{
System.out.println(arr2[i]);
}
if(arr2[0] == 0){
System.out.println("right");
}

boolean[] arr3 = new boolean[4];
for(int i = 0;i <arr3.length;i++{
System.out.println(arr3[i]);
}

String[] arr4 = new String[4];
System.out.println(arr[2]);
}
}
}
</pre>

  1. 数组的内存解析

    栈(stack)

    堆(heap)

    方法区:常量池、静态域

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n206" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> //练习题1:
//升景坊单间短期出租4个月,550元/月(水电煤公摊,网费35元/月),空调、卫生间、厨房齐全。屋内均是IT行业人士,喜欢安静。所以要求来租者最好是同行或者刚毕业的年轻人,爱干净、安静
public class Array Test {
public static void main(String] args){
int[] arr = new int[] {8, 2, 1,0, 3};
int[] index=new int[]{2,0,3,2,4,0,1,3,2,3,3};
String tel = "";
for(int i = 0;i < index.length;i++){
tel += arr[index[i]];
}
System.out.println("联系方式:"+tel);
}
}</pre>

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n208" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> //练习题2:
import java.util.Scanner;
//ctrl + shift + O:自动导包

/*

  • 从键盘读入学生成绩,找出最高分,并输出学生成绩等级
  • 成绩>=最高分-10 等级为‘A’
  • 成绩>=最高分-10 等级为‘A’
  • 成绩>=最高分-10 等级为‘A’
  • 其余 等级为‘D’
  • 提示:先读入学生人数,根据人数创建int数组,存放学生成绩

*/

public class Array Test2 {
public static void main(String[] args) {
//使用Scanner读取学生人数
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生人数:");
//shift + enter:当前行内任意位置回车到下一行
//ctrl + shift + enter:当前行内任意位置回车到上一行
//ctrl + D 删除当前行
int number = scanner.nextInt();

//创建数组,存储学生成绩,动态初始化
int[] scores = new int[number];

//给数组元素赋值
System.out.println("请输入" + number + "个学生成绩");
for(int i = 0;i < scores.length;i++){
scores[i] = scanner.nextInt();
}

//获取数组中的元素最大值
int maxScore = 0;
for(int i = 0;i < scores.length;i++){
if(maxScore < scores[i]) {
maxScore = scores[i];
}
}

//根据每个学生成绩与最高分的差值,得到每个学生的等级,并输出等级和成绩
char level;
for(int i = 0;i < scores.length;i++){
if(maxScore - scores[i] <= 10){
level = 'A';
}else if(maxScore - scores[i] <= 10){
level = 'B';
}else if(maxScore - scores[i] <= 20){
level = 'C';
}else{
level = 'D';
}
System.out.println("student " + i + " score is" + scores[i] + ",grade is" + level);
}
}
}
</pre>

多维数组

  • 如果说可以把一维数组当成几何中的线性图形,那么二维数组就相当于一个双表头表格

  • 对于二维数组的理解,我们可以看成是一维数组 array1 又作为另一个一维数组 array2 的元素而存在。从数组的底层运行机制来看,其实没有多维数组

  1. 二维数组的声明和初始化

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n220" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> public class ArrayTest2{
public static void main(String[] args){
//一维数组:
int[] arr = new int[]{1,2,3};

//二维数组:
//静态初始化:
int[][]arr1 = new int[][]{{1,2,3},{4,5},{6,7,8}}
//动态初始化1:
String[][]arr2 = new String[3][2];
//动态初始化2:
String[][]arr3 = new String[3][];
}
}</pre>

  1. 如何调用数组的指定位置的元素

<pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n224" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px 8px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">System.out.println(arr1[0][1]);//第一组第二个:2
System.out.println(arr1[2][2]);//第三组第三个:8</pre>

  1. 获取数组长度

<pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n228" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px 8px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">System.out.println(arr1.length);//3
System.out.println(arr1[0].length);//3</pre>

  1. 遍历二维数组

<pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n232" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px 8px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">for(int i = 0;i < arr1.length;i++){
for(int j = 0;j <arr1[i].length;j++){
System.out.println(arr1[i][j] + " ");
}
System.out.println();
}</pre>

  1. 数组元素的默认初始化值

    二维数组分为外层数组的元素内层数组的元素

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

    • 外层元素:arr[0]arr[1]

    • 内层元素:arr[0][0]arr[1][2]

    针对初始化方式一:int[][] arr = new int[4][3];

    • 外层元素的初始化值为:地址值

    • 内层元素的初始化值为:与一维数组的初始化情况相同

    针对初始化方式二:int[][] arrr = new int[4][];

    • 外层元素的初始化值为:null

    • 内层元素的初始化值为:不能调用,否则报错

<pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n256" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px 8px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class ArrayTest2 {
public static void main(String[] args) {
int[][] arr1 = new int[3][2];
System.out.println(arr1[0]);//地址值:[I@182decdb
System.out.println(arr1[0][0]);//0

    System.out.println(arr1);//二维地址值

    String[][] arr2 = new String[4][2];
    System.out.println(arr2[1]);//地址值
    System.out.println(arr2[1][1]);//null

    String[][] arr3 = new String[4][];
    System.out.println(arr3[1]);//null
    System.out.println(arr3[1][1]);//空指针异常
}

}
</pre>

数组

数组(Array)是多个相同类型数据按一定顺序排列的集合,并使用一个名字命名,并通过编号的方式对这些数据进行统一管理。

常见概念:数组名、角标、下标、索引、元素、数组长度

数组特点:

  • 数组是有序排列的

  • 数组属于引用数据类型的变量。数组的元素既可以是基本数据类型,也可以是引用数据类型

  • 创建数组对象会在内存中开辟一整块连续空间

  • 数组的长度一旦确定就不能修改

数组分类:

  • 按照维度:一维数组、二维数组……

  • 按照数组元素类型:基本数据类型元素的数组、引用数据类型元素的数组

一维数组

  1. 一维数组的声明和初始化

    • 数组一旦初始化完成,其长度就确定了

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n161" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> public Static void main(String[] args){
int[] id;
//静态初始化:数组的初始化和数组元素的赋值操作同时进行
id = new int[]{1,2,3,4,5};
//int[] id = {1,2,3,4,5};
//动态初始化:数组的初始化和数组元素的赋值操作分开进行
String[] name = new String[3];
}</pre>

  1. 如何调用数组的指定位置的元素

    • 数组的角标(或索引)从 0 开始,到数组长度的 -1 结束

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n169" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> name[0] = "张三";
name[1] = "里斯";
name[2] = "汪芜";</pre>

  1. 如何获取数组的长度

    • 属性:length

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n177" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> System.out.println(name.length);//3
System.out.println(id.length);//5</pre>

  1. 如何遍历数组

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n182" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> for(int i = 0;i < name.length;i++){
System.out.println(names[i]);
}</pre>

  1. 数组元素的默认初始化值

    • 数组元素是整型:0

    • 浮点型:0.0

    • char型:0 或/u0000而非‘0’

    • boolean型:false

    • 引用数据类型:null

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n198" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> public class ArrayTest{
public static void main(String[] args){
int[] arr = new int[4];
for(int i = 0;i <arr.length;i++{
System.out.println(arr[i]);
}

short[] arr1 = new short[4];
for(int i = 0;i <arr1.length;i++{
System.out.println(arr1[i]);
}

char[] arr2 = new char[4];
for(int i = 0;i <arr2.length;i++{
System.out.println(arr2[i]);
}
if(arr2[0] == 0){
System.out.println("right");
}

boolean[] arr3 = new boolean[4];
for(int i = 0;i <arr3.length;i++{
System.out.println(arr3[i]);
}

String[] arr4 = new String[4];
System.out.println(arr[2]);
}
}
}
</pre>

  1. 数组的内存解析

    栈(stack)

    堆(heap)

    方法区:常量池、静态域

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n206" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> //练习题1:
//升景坊单间短期出租4个月,550元/月(水电煤公摊,网费35元/月),空调、卫生间、厨房齐全。屋内均是IT行业人士,喜欢安静。所以要求来租者最好是同行或者刚毕业的年轻人,爱干净、安静
public class Array Test {
public static void main(String] args){
int[] arr = new int[] {8, 2, 1,0, 3};
int[] index=new int[]{2,0,3,2,4,0,1,3,2,3,3};
String tel = "";
for(int i = 0;i < index.length;i++){
tel += arr[index[i]];
}
System.out.println("联系方式:"+tel);
}
}</pre>

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n208" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> //练习题2:
import java.util.Scanner;
//ctrl + shift + O:自动导包

/*

  • 从键盘读入学生成绩,找出最高分,并输出学生成绩等级
  • 成绩>=最高分-10 等级为‘A’
  • 成绩>=最高分-10 等级为‘A’
  • 成绩>=最高分-10 等级为‘A’
  • 其余 等级为‘D’
  • 提示:先读入学生人数,根据人数创建int数组,存放学生成绩

*/

public class Array Test2 {
public static void main(String[] args) {
//使用Scanner读取学生人数
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生人数:");
//shift + enter:当前行内任意位置回车到下一行
//ctrl + shift + enter:当前行内任意位置回车到上一行
//ctrl + D 删除当前行
int number = scanner.nextInt();

//创建数组,存储学生成绩,动态初始化
int[] scores = new int[number];

//给数组元素赋值
System.out.println("请输入" + number + "个学生成绩");
for(int i = 0;i < scores.length;i++){
scores[i] = scanner.nextInt();
}

//获取数组中的元素最大值
int maxScore = 0;
for(int i = 0;i < scores.length;i++){
if(maxScore < scores[i]) {
maxScore = scores[i];
}
}

//根据每个学生成绩与最高分的差值,得到每个学生的等级,并输出等级和成绩
char level;
for(int i = 0;i < scores.length;i++){
if(maxScore - scores[i] <= 10){
level = 'A';
}else if(maxScore - scores[i] <= 10){
level = 'B';
}else if(maxScore - scores[i] <= 20){
level = 'C';
}else{
level = 'D';
}
System.out.println("student " + i + " score is" + scores[i] + ",grade is" + level);
}
}
}
</pre>

多维数组

  • 如果说可以把一维数组当成几何中的线性图形,那么二维数组就相当于一个双表头表格

  • 对于二维数组的理解,我们可以看成是一维数组 array1 又作为另一个一维数组 array2 的元素而存在。从数组的底层运行机制来看,其实没有多维数组

  1. 二维数组的声明和初始化

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="java" cid="n220" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> public class ArrayTest2{
public static void main(String[] args){
//一维数组:
int[] arr = new int[]{1,2,3};

//二维数组:
//静态初始化:
int[][]arr1 = new int[][]{{1,2,3},{4,5},{6,7,8}}
//动态初始化1:
String[][]arr2 = new String[3][2];
//动态初始化2:
String[][]arr3 = new String[3][];
}
}</pre>

  1. 如何调用数组的指定位置的元素

<pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n224" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px 8px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">System.out.println(arr1[0][1]);//第一组第二个:2
System.out.println(arr1[2][2]);//第三组第三个:8</pre>

  1. 获取数组长度

<pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n228" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px 8px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">System.out.println(arr1.length);//3
System.out.println(arr1[0].length);//3</pre>

  1. 遍历二维数组

<pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n232" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px 8px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">for(int i = 0;i < arr1.length;i++){
for(int j = 0;j <arr1[i].length;j++){
System.out.println(arr1[i][j] + " ");
}
System.out.println();
}</pre>

  1. 数组元素的默认初始化值

    二维数组分为外层数组的元素内层数组的元素

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

    • 外层元素:arr[0]arr[1]

    • 内层元素:arr[0][0]arr[1][2]

    针对初始化方式一:int[][] arr = new int[4][3];

    • 外层元素的初始化值为:地址值

    • 内层元素的初始化值为:与一维数组的初始化情况相同

    针对初始化方式二:int[][] arrr = new int[4][];

    • 外层元素的初始化值为:null

    • 内层元素的初始化值为:不能调用,否则报错

<pre spellcheck="false" class="md-fences mock-cm md-end-block md-fences-with-lineno" lang="java" cid="n256" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(19, 27, 41); position: relative !important; border-radius: 3px; color: rgb(239, 183, 82); padding: 8px 0px 6px 8px; margin-bottom: 15px; margin-top: 15px; width: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class ArrayTest2 {
public static void main(String[] args) {
int[][] arr1 = new int[3][2];
System.out.println(arr1[0]);//地址值:[I@182decdb
System.out.println(arr1[0][0]);//0

    System.out.println(arr1);//二维地址值

    String[][] arr2 = new String[4][2];
    System.out.println(arr2[1]);//地址值
    System.out.println(arr2[1][1]);//null

    String[][] arr3 = new String[4][];
    System.out.println(arr3[1]);//null
    System.out.println(arr3[1][1]);//空指针异常
}

}
</pre>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,816评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,729评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,300评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,780评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,890评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,084评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,151评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,912评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,355评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,666评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,809评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,504评论 4 334
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,150评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,882评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,121评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,628评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,724评论 2 351

推荐阅读更多精彩内容