当前位置: 首页 > news >正文

JavaDay7

数组

数组的定义

  • 数组是相同类型数据的有序集合。
  • 数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成。
  • 其中,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问它们。

数组声明创建

  • 首先必须声明数组变量,才能在程序中使用数组。下面是声明数组的语法:
dataType[] arrayRefVar;//首选的方法
或
dataType arrayRefVar[];//效果相同,但不是首选方法
  • Java语言使用new操作符来创建数组,语法如下:
dataype[] arrayRefVar = new dataType[arraySize];
  • 数组的元素是通过索引访问的,数组索引从0开始。
  • 获取数组长度:
arrays.length
package array;public class ArrayDemo01 {//变量的类型 变量的名字 = 变量的值;//数组类型public static void main(String[] args) {int[] nums;//1.声明一个数组nums = new int[10];//2.创建一个数组int[] nums2 = new int[10];//3.给数组元素赋值nums[0] = 1;nums[1] = 2;nums[2] = 3;nums[3] = 4;nums[4] = 5;nums[5] = 6;nums[6] = 7;nums[7] = 8;nums[8] = 9;nums[9] = 10;//计算所有元素的和int sum = 0;for (int i = 0; i < nums.length; i++) {sum += nums[i];}System.out.println(sum);}
}
输出:
55

内存分析

d5cf6d00a7a7fe995e0380fbe554cd0d

e1b3b920d059374682f2e65b97e7cb9d

三种初始化

  • 静态初始化
  • 动态初始化
  • 数组的默认初始化
    • 数组是引用类型,它的元素相当于类的实例变量,因此数组一经分配空间,其中的每个元素也被按照实例变量同样的方式被隐式初始化。
package array;public class ArrayDemo02 {public static void main(String[] args) {//静态初始化:创建 + 赋值int[] a = {1,2,3,4,5,6,7,8};System.out.println(a[0]);//动态初始化:包含默认初始化int[] b = new int[10];b[0] = 10;System.out.println(b[0]);System.out.println(b[1]);}
}
输出:
1
10
0

数组的四个基本特点

  • 其长度是确定的。数组一旦被创建,它的大小就是不可以改变的。

  • 其元素必须是相同类型,不允许出现混合类型。

  • 数组中的元素可以是任何数据类型,包括基本类型和引用类型。

  • 数组变量属于引用类型,数组也可以看成是对象,数组中的每个元素相当于该对象的成员变量。

    数组本身就是对象,Java中对象是在堆中的,因此数组无论保存原始类型还是其他对象类型,数组对象本身是在堆中的。

数组边界

  • 下标的合法区间:[0,length-1],如果越界就会报错;

    public static void main(String[] args){int[] a = new int[2];System.out.println(a[2]);
    }
    
  • ArrayIndexOutOfBoundsException:数组下标越界异常!

  • 小结:

    • 数组是相同数据类型(数据类型可以为任意类型)的有序集合
    • 数组也是对象。数组元素相当于对象的成员变量
    • 数组长度是确定的,不可变的。如果越界,则报:ArrayIndexOutOfBounds

数组使用

  • 普通For循环
  • For-Each循环
  • 数组作方法入参
  • 数组作返回值
package array;public class ArrayDemo03 {public static void main(String[] args) {int[] arrays = {1,2,3,4,5};//打印全部的数组元素for (int i = 0; i < arrays.length; i++) {System.out.println(arrays[i]);}System.out.println("===================");//计算所有元素的和int sum = 0;for (int i = 0; i < arrays.length; i++) {sum += arrays[i];}System.out.println("sum=" + sum);System.out.println("===================");//查找最大元素int max = arrays[0];for (int i = 1; i < arrays.length; i++) {if(arrays[i] > max){max = arrays[i];}}System.out.println("max = " + max);}
}
输出:
1
2
3
4
5
===================
sum=15
===================
max = 5
package array;public class ArrayDemo04 {public static void main(String[] args) {int[] arrays = {1,2,3,4,5};
//        //增强for循环,没有下标
//        for(int array : arrays){
//            System.out.println(array);
//        }printArray(arrays);int[] reverse = reverse(arrays);printArray(reverse);}//打印数组元素public static void printArray(int[] arrays){for (int i = 0; i < arrays.length; i++) {System.out.print(arrays[i] + " ");}}//反转数组public static int[] reverse(int[] arrays){int[] result = new int[arrays.length];//反转的操作for (int i = 0,j = result.length-1; i < arrays.length; i++,j--) {result[j] = arrays[i];}return result;}
}
输出:
1 2 3 4 5 5 4 3 2 1 

多维数组

  • 多维数组可以看成数组的数组,比如二维数组就是一个特殊的一维数组,其每一个元素都是一个一维数组。

  • 二维数组

    int a[][] = new int[2][5];
    
  • 解析:以上二维数组 a 可以看成一个两行五列的数组

a21ec2e8e510b05b6bfccd8468ce928a

package array;public class ArrayDemo05 {public static void main(String[] args) {/*1,2  array[0]2,3  array[1]3,4  array[2]4,5  array[3]*/int[][] arrays = {{1,2},{2,3},{3,4},{4,5}};System.out.println(arrays[0][0]);System.out.println("=================");for (int i = 0; i < arrays.length; i++) {for (int j = 0; j < arrays[i].length; j++) {System.out.print(arrays[i][j]+ " ");}}}
}
输出:
1
=================
1  2  2  3  3  4  4  5 

Arrays类

  • 数组的工具类java.util.Arrays
  • 由于数组对象本身并没有什么方法可以供我们调用,但API中提供了一个工具类Arrays供我们使用,从而可以对数据对象进行一些基本的操作。
  • 查看JDK帮助文档
  • Arrays类中的方法都是static修饰的静态方法,在使用的时候可以直接使用类名进行调用,而“不用”使用对象来调用(注意:是“不用”而不是“不能”)
  • 具有以下常用功能:
    • 给数组赋值:通过 fill 方法。
    • 对数组排序:通过 sort 方法,按升序。
    • 比较数组:通过 equals 方法比较数组中元素是否相等。
    • 查找数组元素:通过 binarySearch 方法能对排序好的数组进行二分查找法操作。
package array;import java.util.Arrays;public class ArraysDemo06 {public static void main(String[] args) {int[] a = {1, 2, 3, 4, 9090, 31231, 543, 21, 3, 23};//System.out.println(a);//打印数组元素Array.toString//System.out.println(Arrays.toString(a));//printArrays(a); 自己写的方法Arrays.sort(a);//数组进行排序System.out.println(Arrays.toString(a));System.out.println("==================");
//        Arrays.fill(a,0);//数组填充,把数组里面的值都填充为0;
//        System.out.println(Arrays.toString(a));
//        System.out.println("==================");Arrays.fill(a,2,4,0);//对数组[2,3)位置的数据填充为0;System.out.println(Arrays.toString(a));}public static void printArrays(int[] a) {for (int i = 0; i < a.length; i++) {if (i == 0) {System.out.print("[");}if (i == a.length - 1) {System.out.print(a[i] + "]");} else {System.out.print(a[i] + ", ");}}}
}
输出:
[1, 2, 3, 3, 4, 21, 23, 543, 9090, 31231]
==================
[1, 2, 0, 0, 4, 21, 23, 543, 9090, 31231]

冒泡排序

package array;import java.util.Arrays;public class ArrayDemo07 {public static void main(String[] args) {int[] a = {88,9,85,2,65,30,22};int[] sort = sort(a);//sort(a)为调用我们写的方法对a数组排序,给我们新建立的数组sortSystem.out.println(Arrays.toString(sort));}//冒泡排序/*1.比较数组中,两个相邻的元素,如果第一个数比第二个数大,我们就交换他们的位置2.每一次比较,都会产生出一个最大,或者最小的数字;3.下一轮则可以少一次排序!4.依次循环,直到结束!*/public static int[] sort(int[] array){//临时变量int temp = 0;//外层循环,判断哦我们这个要走多少次;boolean flag = false;//通过flag标志位减少没有意义的比较for (int i = 0; i < array.length-1; i++) {//内层循环,比较判断两个数,如果第一个数比第二个数大,则交换位置for(int j = 0;j < array.length-1-i;j++){if(array[j+1]<array[j]){temp = array[j];array[j] = array[j+1];array[j+1] = temp;flag = true;}}if (flag == false){break;}}return array;}}

稀疏数组

bb58c1d2853782d1e1d74d178094a143

package array;public class ArrayDemo08 {public static void main(String[] args) {//1.创建一个二维数组 11 * 11   0:没有棋子    1:黑棋     2:白棋int[][] array1 = new int[11][11];array1[1][2] = 1;array1[2][3] = 2;//输出原始的数组System.out.println("输出原始的数组:");for(int[] ints : array1){for(int anInt : ints){System.out.print(anInt+"\t");}System.out.println();}System.out.println("=================");//转换为稀疏数组保存//获取有效值的个数int sum = 0;for (int i = 0; i < 11; i++) {for (int j = 0; j < 11; j++) {if(array1[i][j] != 0){sum++;}}}System.out.println("有效值的个数:" + sum);//2.创建一个稀疏数组的数组int[][] array2 = new int[sum+1][3];array2[0][0] = 11;array2[0][1] = 11;array2[0][2] = sum;//遍历二维数组,将非零的值,存放稀疏数组中int count = 0;for (int i = 0; i < array1.length; i++) {for (int j = 0; j < array1[i].length; j++) {if(array1[i][j] != 0){count++;array2[count][0] = i;array2[count][1] = j;array2[count][2] = array1[i][j];}}}//输出稀疏数组System.out.println("稀疏数组");for (int i = 0; i < array2.length; i++) {System.out.println(array2[i][0] + "\t" + array2[i][1] + "\t" +array2[i][2]);}System.out.println("=================");System.out.println("还原");//1.读取稀疏数组int[][] array3 = new int[array2[0][0]][array2[0][1]];//2.给其中的元素还原它的值for (int i = 1; i < array2.length; i++) {array3[array2[i][0]][array2[i][1]] = array2[i][2];}//3.打印还原System.out.println("输出还原的数组");for(int []ints : array3){for (int anInt : ints){System.out.print(anInt+"\t");}System.out.println();}}
}
输出
输出原始的数组:
0	0	0	0	0	0	0	0	0	0	0	
0	0	1	0	0	0	0	0	0	0	0	
0	0	0	2	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	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	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	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
=================
有效值的个数:2
稀疏数组
11	11	2
1	2	1
2	3	2
=================
还原
输出还原的数组
0	0	0	0	0	0	0	0	0	0	0	
0	0	1	0	0	0	0	0	0	0	0	
0	0	0	2	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	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	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	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	进程已结束,退出代码0
http://www.hskmm.com/?act=detail&tid=8563

相关文章:

  • 前端场景题笔记
  • P3934 [Ynoi Easy Round 2016] 炸脖龙 I 做题记录
  • 【CompletableFuture 核心操作全解】详细注释版
  • 关于学术不端的一些思考
  • python基础-字典
  • pod 内nslookup请求时常异常
  • 单调队列优化DP
  • 4.5.11版本闪亮登场~快来看看有哪些新功能
  • 教你数分钟内创建并运行一个 DolphinScheduler Workflow!
  • AT_agc065_b [AGC065B] Erase and Insert
  • 《大模型时代——智能体的崛起与应用实践(微课视频版)》
  • 第三节:GoLangChain提示词(Prompts)处理详解
  • rhel8 中vdo 邏輯卷的邏輯擴容
  • Codeforces Round 1051 (Div. 2) 部分题解
  • kingbase金仓数据库的密码有效期和密码复杂度
  • HDF5文件
  • Error encountered when performing Introspect the Portion of idea Introspect using JDBC metadata在哪设置
  • 核桃 CSP-S 模拟
  • 正确输入连字号、连接号、破折号和负号
  • 9 月记录
  • python基础-元组
  • .net core中获得程序集以及注入框架的方法总结
  • python基础篇-list(列表)
  • vscode使用powershell中文乱码
  • 关于如何读懂 P11832 [省选联考 2025] 图排列?
  • Untitled
  • 敏感性分析
  • 完整教程:论园区电气安全管理系统的重要性
  • 基于CSU8RP1186芯片的握力器解决方案
  • 亮相2025年服贸会,天翼云打造高质量算力服务新生态!