<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)是多个相同类型数据按一定顺序排列的集合,并使用一个名字命名,并通过编号的方式对这些数据进行统一管理。
常见概念:数组名、角标、下标、索引、元素、数组长度
数组特点:
数组是有序排列的
数组属于引用数据类型的变量。数组的元素既可以是基本数据类型,也可以是引用数据类型
创建数组对象会在内存中开辟一整块连续空间
数组的长度一旦确定就不能修改
数组分类:
按照维度:一维数组、二维数组……
按照数组元素类型:基本数据类型元素的数组、引用数据类型元素的数组
一维数组
-
一维数组的声明和初始化
- 数组一旦初始化完成,其长度就确定了
<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>
-
如何调用数组的指定位置的元素
- 数组的角标(或索引)从 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>
-
如何获取数组的长度
- 属性:
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>
- 如何遍历数组
<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>
-
数组元素的默认初始化值
数组元素是整型: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>
-
数组的内存解析
栈(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 的元素而存在。从数组的底层运行机制来看,其实没有多维数组
- 二维数组的声明和初始化
<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>
- 如何调用数组的指定位置的元素
<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>
- 获取数组长度
<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>
- 遍历二维数组
<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>
-
数组元素的默认初始化值
二维数组分为外层数组的元素、内层数组的元素
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)是多个相同类型数据按一定顺序排列的集合,并使用一个名字命名,并通过编号的方式对这些数据进行统一管理。
常见概念:数组名、角标、下标、索引、元素、数组长度
数组特点:
数组是有序排列的
数组属于引用数据类型的变量。数组的元素既可以是基本数据类型,也可以是引用数据类型
创建数组对象会在内存中开辟一整块连续空间
数组的长度一旦确定就不能修改
数组分类:
按照维度:一维数组、二维数组……
按照数组元素类型:基本数据类型元素的数组、引用数据类型元素的数组
一维数组
-
一维数组的声明和初始化
- 数组一旦初始化完成,其长度就确定了
<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>
-
如何调用数组的指定位置的元素
- 数组的角标(或索引)从 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>
-
如何获取数组的长度
- 属性:
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>
- 如何遍历数组
<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>
-
数组元素的默认初始化值
数组元素是整型: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>
-
数组的内存解析
栈(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 的元素而存在。从数组的底层运行机制来看,其实没有多维数组
- 二维数组的声明和初始化
<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>
- 如何调用数组的指定位置的元素
<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>
- 获取数组长度
<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>
- 遍历二维数组
<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>
-
数组元素的默认初始化值
二维数组分为外层数组的元素、内层数组的元素
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>