什么是数组?
定义:
- 数组是相同类型数据的有序集合。
- 数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成。
- 其中,没一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问它们。
特点:
- 长度是确定的。数组一旦被创建,它的大小就是不可改变的。
- 其元素必须是相同类型,不允许数显混合类型。
- 元素可以是任何数据类型,包括基本类型和引用类型。
- 数组变量属于应用类型,数组也可以看成是对象,数组中的每个元素相当于该对象的成员变量,数组对象本身是在堆中的。
数组声明创建
声明
-
首先必须声明数组变量,才能在程序中使用数组。声明数组的语法格式:
dataType[ ] arrayRefVar; //首先方法 dataType arrayRefVar[ ]; //实现的目的相同,但是Java更加轻重于第一种方法,一目了然。
-
Java语言使用new操作符来创建数组,语法如下:
dataType[ ] array = new dataType[arraySize]
-
数组的元素是通过索引访问的,数组索引从0开始,即数组下标。
-
获取数组的长度:
arrays.length
。
创建
public class ArrayDeom02 {
public static void main(String[] arg){
//另一种数组表示方法 int[] array = new int[5];
//数组的声明
int[] array;
//分配数组的空间
array = new int[5];
//分配数值
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
array[4] = 5;
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
内存分析
-
Java内存分析:
数值初始化
-
动态初始化(包含默认初始化):
int[] array = new int[2]; array[0] = 1; array[1] = 2; array[2] = 3;
-
静态初始化:创建+赋值
int[] array = {1,2,3,4,5}
-
默认初始化
- 数值是引用类型,它的元素相当于类的实例变量,因此数值一经分配空间,其中的每个单元也被按照实例变量同样的方式被隐式初始化。
数值下标越界
-
超过数组下标范围,比如,你声明数组a时的最大下标是15,出来一个a(16),就是下标越界。
public class ArrayDeom08 { public static void main(String[] args) { int[] n = new int[3]; n[0] = 1; n[1] = 2; n[2] = 3; for (int i = 0; i <= n.length; i++) { //此处应为i<n.length,i=lrngth会造成下标越界 System.out.println(n[i]); } } }
数值的使用
-
for...each循环
eg.遍历数组元素
public class ArrayDeom09 { public static void main(String[] args) { int[] n = {1,2,3,4,5,6}; for (int m : n){ System.out.print(m + "\t"); } } }
-
数值作为方法入参和数组作为返回值
public class ArrayDeom04 { public static void main(String[] arg){ //用户自行输入 Scanner longth = new Scanner(System.in); //记长度输入 System.out.println("输入要比较多少数字:"); int Longth = longth.nextInt(); int[] array = new int[Longth]; System.out.println("输入数字:"); // for (int i = 0; i < Longth; i++) { array[i] = longth.nextInt(); } /* 不记长度输入,等学到相关知识在回来看,网上的教程。 String str= longth.next().toString(); String[] arr = str.split(","); int[] array = new int[arr.length]; for (int i = 0; i < array.length; i++) { array[i] = Integer.parseInt(arr[i]); } */ printArray(array); System.out.println(); int[] result = Array(array); printArray(result); } //反转数组 public static int[] Array(int[] arrays){ int[] a = new int[arrays.length]; for (int i = arrays.length-1,j=0 ; j < arrays.length ; i--,j++) { a[i]=arrays[j]; //a[j]=arrays[i]效果一样 } return a; //数组作为返回值 } //打印数组 public static void printArray(int[] arrays){ for (int i = 0; i < arrays.length ; i++) { System.out.print(arrays[i]+" "); } } }
二维数组
-
多个数组可以看成是数组的数组,比如二维数组就是一个特殊的一维数组。
-
二维数组
int a[][] = new int[4][5]
-
二维数组可以看成几行几列的数组。
二维数组的使用
public class ArrayDeom01 {
public static void main(String[] arg){
//二维数组与for循环的嵌套使用
int[][] c= {{1,2,3},{2,1,3},{4,8,6}};
for (int i=0;i<c.length;i++) {
for (int j = 0; j < c[i].length; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
}
Array类
Java为开发者提供了两个便利的操作数组的类,Array类提供了动态创建和访问数组的方法,Arrays提供了一系列用来操作数组的方法(如排序、查找),另外此类还包含一个允许将数组作为列表来查看的静态工厂。
-
数组的工具类java.util.Arrays
-
Array
类的方法全部都是静态方法,而且构造方法被private
修饰(是不是想到了Math类 :-)),它们的作用就是提供静态的函数库。 -
Array
类不能被实例化。 -
常用功能:
-
给数组赋值:通过fill方法(假如一个数组为 int[] b = {3, 4, 5, 7, 7, 8}, Arrays.fill(b,1,3,8),意思为数组b的下标从1~3之间的数字变为8而不包括下标3)。
public class ArrayDeom12 { public static void main(String[] args) { ArrayDeom12 arrayDeom12 = new ArrayDeom12(); int[] b = {3, 4, 5, 7, 7, 8}; arrayDeom12.assignMent(b); arrayDeom12.outArray(b); } public void outArray(int q[]){ for (int n : q){ System.out.print(n + " "); } } public void assignMent(int w[]){ Scanner scanner = new Scanner(System.in); System.out.println("数组下标开始"); int x= scanner.nextInt(); System.out.println("数组下标结束"); int y= scanner.nextInt(); System.out.println("覆盖数字"); int n= scanner.nextInt(); Arrays.fill(w,x,y,n); } }
-
给数组排序:通过sort方法。
public class ArrayDeom { public static void main(String[] args) { int[] a = {4,5,8,9,7,3,5}; Arrays.sort(a); for (int n : a){ System.out.print(n + " "); } } }
-
打印数组:通过toString方法。
public class ArrayDeom { public static void main(String[] args) { int[] a = {5,6,6,7,89,4,25}; System.out.println(Arrays.toString(a)); } }
-
比较数组大小:通过equals方法比较数组中元素值是否想等。
-
public class ArrayDeom13 { public static void main(String[] args) { int[] a = {34,456,2,6,8,5,53,3,2,5}; int[] b = {34,21,1,4,5,6,7,8,5,3,3}; int[] c = {34,21,1,4,5,6,7,8,5,3,3}; System.out.println(Arrays.equals(a,b));; System.out.println(Arrays.equals(b,c));; } }
-
查找数组元素:通过binarySearch方法能对排序好的数组进行二查找。
public class ArrayDeom03 { public static void main(String[] args) { number(); } static void number(){ int[] n= {6,23,8,42,9,3,98,10,34,76}; Arrays.sort(n); System.out.print("依次排序:"); for (int i : n) { System.out.print(i+" "); //排序 } System.out.println(); System.out.print("输入要查询的数:"); int N = new Scanner(System.in).nextInt(); int f = Arrays.binarySearch(n, N); //二分查找,N表示从n中排完序要找的数 System.out.print('\n'+"在第"+(f+1)+"个数"); } }
-
冒泡排序(原型)
public class ArrayDeom06 {
public static void main(String[] arg){
int[] a = {4,6,2,7,8,4,1,7};
int[] sort = ranKing(a);
System.out.println(Arrays.toString(sort));
}
public static int[] ranKing(int[] arrays){
//冒泡排序
//排序次数
int temp;
for (int i = 0; i < arrays.length-1; i++) {
boolean flag = false;
//比较大小
for (int j = 0; j < arrays.length-1-i; j++) {
if (arrays[j] > arrays[j+1]) {
//交换位置
temp = arrays[j+1];
arrays[j+1] = arrays[j];
arrays[j] = temp;
flag = true;
}
}
//减少排序次数
if (flag != true) {
break;
}
}
return arrays;
}
}
稀疏数组(了解)
public class ArrayDeom07 {
public static void main(String[] arg) {
int[][] board = new int[11][11]; //白子为1,黑子为2,棋盘格局为11*11
board[1][2] = 1;
board[3][4] = 2;
for (int[] ints : board) {
for (int anInt : ints) {
System.out.print(anInt + "\t");
}
System.out.println();
}
System.out.println("=================================================");
//转换稀疏数组保存,理解加记忆
//获取有效值
int sum = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] != 0) {
sum++;
}
}
}
System.out.println("有效个数为"+sum);
int[][] board1 = new int[sum+1][3];
board1[0][0] = 11;
board1[0][1] = 11;
board1[0][2] = sum;
int count = 0;
for (int i = 0; i < board.length ; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] != 0) {
count++;
board1[count][0] = i;
board1[count][1] = j;
board1[count][2] = board[i][j];
}
}
}
System.out.println("输出稀疏数组:");
for (int i = 0; i < board1.length; i++) {
System.out.println(board1[i][0]+"\t"+board1[i][1]+"\t"+board1[i][2]+"\t");
}
System.out.println("=================================================");
System.out.println("还原稀疏数组:");
//还原稀疏数组
//创建一个动态数组
int[][] board2 = new int[board1[0][0]][board1[0][1]]; // new int[board1[0][0]][board1[0][1]]==>new int[11][11]
for (int i = 1; i < board1.length; i++) {
board2[board1[i][0]][board1[i][1]] = board1[i][2]; /*board[1][2] = 1; board[3][4] = 2;*/
}
//打印
for (int[] ints : board2) {
for (int anInt : ints) {
System.out.print(anInt + "\t");
}
System.out.println();
}
}
}
评论区