网站首页 > 技术文章 正文
叩丁狼教育Java基础教程
1.1. 数组操作(重点)
1.1.1. 基本操作(重点)
int[] nums = new int[]{1,3,5,7};
叩丁狼教育Java基础教程
- n 获取数组长度,语法:int size = 数组名.length;
int size = nums.length;-> 输出结果4
- 获取元素值,语法:元素类型 变量名 = 数组名[index];
- 数组的索引从0开始,最大索引值是数组长度-1,那么索引范围是 [ 0,数组名.length - 1 ]。
获取第一个元素:intele1 = nums[0];输出1
获取第二个元素:intele2 = nums[1];输出3
获取第四个元素:intele4 = nums[3];输出7
- 设置元素值,语法:数组名[index] = 值;
设置第二个元素值为30nums[1] = 30;
获取第二个元素:int ele2 = nums[1];输出30
- 常见的异常
1,NullPointerException:空指针异常(空引用异常)
操作了一个尚未初始化或者没有分配内存空间的数组
2,ArrayIndexOutOfBoundsException:数组的索引越界异常
操作的数组的索引不在[0,数组名.length-1]范围内
- 迭代数组,也叫遍历数组(获取出数组中每一个元素)
获取第一个元素:intele1 = nums[0];输出1
获取第二个元素:intele2 = nums[1];输出30
获取第三个元素:intele3 = nums[2];输出5
获取第四个元素:intele4 = nums[3];输出7
发现,循环遍历的次数是数组元素的个数,每一次获取元素只有索引在变化,范围是[0 , 数组名.length - 1]。
int[] nums = new int[] { 1, 3, 5, 7 };
for (int index = 0; index < nums.length; index++) {
int ele = nums[index];//index依次是0、1、2、3
System.out.println(ele);
}
- 使用for-each(增强for循环)操作数组
for(数组元素类型 变量: 数组){
//TODO
}
使用for-each操作数组更简单,因为可以不关心索引,其底层原理依然是上述的for循环操作数组。
int[] nums = new int[] { 1, 3, 5, 7 };
for (int ele : nums) {
System.out.println(ele);
}
1.1.2. 使用循环操作数组(重点)
int[] nums = new int[]{11,22,33,44,22,55};
需求1:找出数组中元素22第一次出现的索引位置
public class ArrayDemo3 {
public static void main(String[] args) {
int key = 22;//被搜索的元素值
int[] nums = new int[] { 11, 22, 33, 44, 22, 55 };
for (int index = 0; index < nums.length; index++) {
if (nums[index] == key) {
System.out.println(index);
break;//找到就结束循环
}
}
}
}
需求2:求出int类型数组中最大元素值
public class ArrayDemo4 {
public static void main(String[] args) {
int[] nums = new int[] { 11, 22, 33, 44, 22, 55 };
int max = nums[0];//max用来记作最大元素值,假设第一个元素是最大的
for (int index = 0; index < nums.length; index++) {
//如果后续某个元素比max大,就存储到max变量上
if (nums[index] > max) {
max = nums[index];
}
}
System.out.println("max=" + max);
}
}
需求3:按照某种格式打印数组元素,数组元素放在方括号[]中,相邻元素使用逗号分隔开。如上述数组打印出来,效果如:[11, 22, 33, 44, 22, 55]
public class ArrayDemo5 {
public static void main(String[] args) {
int[] nums = new int[] { 11, 22, 33, 44, 22, 55 };
String str = "[";//str表示结果字符串,先拼一个[符号
for (int index = 0; index < nums.length; index++) {
//把每一个元素拼接在str后面
str = str + nums[index];
//如果是最后一个元素,则不拼接,而是]
if(index == nums.length-1) {
str = str + "]";
}else {
//如果不是最后一个元素拼接,
str = str + ", ";
}
}
System.out.println(str);
}
}
1.2. 二维数组(了解)
在之前,数组的每一个元素就是一个个的值,这种数组我们称之为一维数组。
二维数组,其实就是数组中的每一个元素就是一个一维数组。
三维数组,数组的每一个元素就是一个二维数组。
其实发现,这种多维数组都可以简单称之为,数组中的数组,在实际开发中使用并不多。
定义和静态初始化一维数组的语法:
数组元素类型[] 数组名 = new 数组元素类型[]{值1,值2,值3,...};
如:int[] nums = new int[]{1,3,5,7};
定义和静态初始化二维数组的语法:
数组元素类型[][] 数组名 = new 数组元素类型[][]{数组1,数组2,数组3,...};
注意,二维数组中的元素类型是一维数组,把数组元素类型[]看成一个整体,表示数据类型。
public class ArrayInArrayDemo1 {
public static void main(String[] args) {
//定义三个一维数组
int[] arr1 = { 1, 2, 3 };
int[] arr2 = { 4, 5 };
int[] arr3 = { 6 };
//把三个一维数组存储到另一个数组中,那么该数组就是二维数组
int[][] arr = new int[][] { arr1, arr2, arr3 };
}
}
定义和动态初始化二维数组的语法:
数组元素类型[][] 数组名 = new 数组元素类型[x][y];
x表示二维数组中有几个一维数组,y表示每一个一维数组中有几个元素。
int[][] arr = new int[3][5];
System.out.println(arr.length);//输出3
因为二维数组表示数组的中的数组,如果要获取数组的每一个元素值,则需要两个循环嵌套。
//二维数组
int[][] arr = new int[][] {
{ 1, 2, 3 },
{ 4, 5 },
{ 6 }
};
使用for循环:
//迭代二维数组
for (int index = 0; index < arr.length; index++) {
//取出每一个一维数组
int[] arr2= arr[index];
//迭代一维数组
for (int j = 0; j < arr2.length; j++) {
int ele = arr2[j];
System.out.println(ele);
}
System.out.println("-----");
}
使用for-each:
for (int[] arr2 : arr) {
for (int ele : arr2) {
System.out.println(ele);
}
System.out.println("-----");
}
}
本系列教程为叩丁狼Java基础班内部教材,若要获得最好的学习效果,需要配合对应教学视频一起学习。需要教学视频,请私信作者即可。
猜你喜欢
- 2024-10-02 掌控你的MySQL语句执行方案(如何让mysql执行脚本?)
- 2024-10-02 我爱Julia之入门-075(字符串05)
- 2024-10-02 AdaBoost算法(手稿+代码)(adaboost算法详解)
- 2024-10-02 有了for循环 为什么还要forEach?(为什么用for循环)
- 2024-10-02 UFS深入浅出 第二章 UFS结构 第三节 UFS分区
- 2024-10-02 Ruby 最常用指令和函数(备忘查询)
- 2024-10-02 MYSQL优化有理有据全分析(面试必备)
- 2024-10-02 最小优先队列 Index min priority queue
- 2024-10-02 mysql explain用法(mysql游标的定义与使用)
- 2024-10-02 mybatis中foreach collection三种用法
- 最近发表
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)