一维数组
一维数组的使用:
①一维数组的声明和初始化
②如何让调用数组的指定位置的元素
③如何获取数组的长度
④如何遍历数组
⑤数组元素的默认初始化值
⑥数组的内存解析
①一维数组的声明和初始化
◇ 一维数组的声明方式:
Java语言中声明数组时不能指定其长度(数组中元素数),例如:int a[5];//非法
例如:int a[];
int[] a1;
double b[];
String[] c;//引用类型变量
◇一维数组的初始化:数组一旦初始化完成,其长度就确定了,是不可变的
>动态初始化:数组声明且数组元素分配空间与赋值操作分开进行
String[] names = new String[3];
>静态初始化:数组声明且数组元素分配空间与赋值操作同时进行
int [] ids = new int[]{1001,1002,1003,1004};
错误写法:
int[] arr2 = new int[];
int[5] arr3 = new int[5];
int[] arr4 =new int[3]{1,2,3};
②如何让调用数组的指定位置的元素
◇ 定义并运用new为之分配空间后,才可以引用数组中的每个元素;
◇ 数组的引用方式:数组名[数组元素下标]
>数组元素下标可以是整型常量或整形表达式,如:a[3],b[i],c[6*i];
>数组元素下标从0开始,长度为n的数组合法下标取值范围:0→ n-1;
如:int a[] = new int[3];可引用的数组元素为a[0]、a[1]、a[2]
names[0] = “王铭”;
names[1] = “王赫”;
names[2] = “张学良”;
③如何获取数组的长度
◇每个数组都有一个属性length指明它的长度,例如:a.length 指明数组a的长度(元素个数)
System.out.println(names.length);//5
④如何遍历数组
for(int i = 0;i < names.length;i++){
System.out.println(names[i]);
}
⑤数组元素的默认初始化值
◇数组是引用数据类型,它的元素相当于类的成员变量,因此数组一经分配空间,
其中的每个元素也被按照成员变量同样的方式被隐式初始化
◇ 数组元素各个类型的默认初始化值
short[] arr1 = new short[4];
for(i = 0,i < arr1.length,i++){
System.out.println(arr1[i]);//0
}
数组元素类型 | 元素默认初始化值 |
---|---|
byte | 0 |
short | 0 |
int | 0 |
long | 0 |
float | 0.0 |
double | 0.0 |
boolean | false |
char | 0或写为‘u0000’(表现为空) |
引用数据类型 | null |
⑥数组的内存解析
◇内存的简化结构
栈:先进后出,后进先出
◇内存解析
1.升经坊单间短期出租4个月,550元/月(水电煤公摊,网费35元/月),空调、卫生间、厨房齐全。屋内均是IT行业人士,喜欢安静,
所以要求来租者最好是同行或者刚毕业的年轻人,爱干净、安静。手机号如下:
public class Experiment { public static void main(String[] args) { // TODO Auto-generated method stub int[] arr1 = 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 += arr1[index[i]]; } System.out.println(tel);//tel:18013820100 } }
2.从键盘读入学生成绩,找出最高分,并输出学生成绩等级
成绩>=最高分-10 等级为‘A’
成绩>=最高分-20 等级为‘B’
成绩>=最高分-30 等级为‘C’
其余 等级为‘D’
package com.guigu.java; /* 请输入学生人数:5 请输入5个成绩 56 74 89 41 89 student0score is56,grade is D student1score is74,grade is B student2score is89,grade is A student3score is41,grade is D student4score is89,grade is A */ import java.util.Scanner; public class Experiment { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); int maxscore = 0; System.out.print("请输入学生人数:"); int people = scanner.nextInt(); int[] score = new int[people]; System.out.println("请输入" + people + "个成绩"); for (int i = 0; i < score.length; i++) { score[i] = scanner.nextInt(); if (maxscore < score[i]) { maxscore = score[i]; } } char level; for (int i = 0; i < score.length; i++) { if (score[i] >= maxscore - 10) { level = 'A'; }else if (score[i] >= maxscore - 20) { level = 'B'; } else if (score[i] >= maxscore - 30) { level = 'C'; }else { level = 'D'; } System.out.println("student" + i + "score is " + score[i] + ",grade is " + level); } } }
二维数组
• Java语言里提供了支持多维数组的语法
• 如果说可以把一维数组当成几何中的线性图形,那么二维数组相当于是一个表格,像Excel中的表格一样
姓名 | 联系电话 |
---|---|
张三 | 16738896782 |
李四 | 18763783767 |
• 对于二维数组的理解,我们可以看成是一维数组array1又作为另一个一维数组array2的元素而存在。其实,
从数组底层的运行机制来看,其实没有多维数组
• 二维数组的使用:
①二维数组的声明和初始化
②如何让调用数组的指定位置的元素
③如何获取数组的长度
④如何遍历数组
⑤数组元素的默认初始化值
⑥数组的内存解析
①二维数组的声明和初始化
▲动态初始化1:int[][] arr = new int [3][2];
定义了名称为arr的二维数组
二维数组中有3个一维数组
每一个一维数组的名称分别为arr[0],arr[1],arr[2]
给第一个一维数组1脚标位赋值为78写法是:arr[0][1] = 78;
▲动态初始化2:int[][] arr1 =new int[3][];
二维数组中有3个一维数组
每个一维数组都是默认初始化值null(注意:区别于格式1)
可以对这三个一维数组分别初始化
arr[0] =new int[3]; arr[1] =new int[1]; arr[2] = new int[2];
注意:int[][] arr = new int[][3];//非法
▲静态初始化:int[][] arr2 =new int[][]{{3,8,2},{2,7},{9,0,1,6}};
定义一个名称为arr2的二维数组,二维数组中有三个一维数组
每一个一维数组中具体元素也都已经初始化
第一个一维数组 arr2[0] = {3,8,2};
第二个一维数组 arr2[1] = {2,7};
第三个一维数组 arr2[2] = {9,0,1,6};
第三个一维数组长度表示方式:arr2[2].length;
注意特殊写法情况:int[] x,y[]; x是一维数组,y是二维数组
Java中多维数组不必都是规则矩阵形式
②如何让调用数组的指定位置的元素
System.out.println(arr2[0][1]);//8
System.out.println(arr[1][1]);//null
System.out.println(arr1[0][1]);//非法,空异常
③如何获取数组的长度
System.out.println(arr2.length);//长度为3
System.out.println(arr2[1].length);//2
System.out.println(arr2[2].length);/4
④如何遍历数组
for(int i = 0;i < arr2.length;i++){
for(int j = 0;j < arr2[i].length;j++){
System.out.println(arr2[i][j] + “ ”);
}
System.out.println();
}
⑤数组元素的默认初始化值
▲针对动态初始化1:比如:int[][] arr3 = new int[4][3];
外层元素的初始化值为:地址值
内层元素的初始化值为:与一维数组的初始化值情况相同
▲针对动态初始化2:比如:int[][] arr5= new int[4][];
外层元素的初始化值为:null
内层元素的初始化值为:不能调用,否则报错
例如:int[][] arr3 = new int[4][3];
System.out.println(arr3[0]);//默认初始化值为:[I@15db9742 ([:说明arr[0]是一维数组)
System.out.println(arr3);//默认初始化值为:[[I@6d06d69c([[:说明arr3是二维数组)
System.out.println(arr3[0][0]);//默认初始化值为:0
⑥数组的内存解析
1.获取arr1数组中所有元素的和
提示:使用for的嵌套循环即可
public class Experiment { public static void main(String[] args) { // TODO Auto-generated method stub int sum = 0; int[][] arr1 = new int [][] {{3,5,8},{12,9},{7,0,6,4}}; for (int i = 0; i < arr1.length; i++) { for (int j = 0; j < arr1[i].length; j++) { sum += arr1[i][j]; } } System.out.println(sum); } }
2.声明:int[] x,y[];在给x,y变量赋值以后,以下选项允许通过编译的是:
a) x[0] = y; ×
b) y[0] = x; √
c) y[0][0] = x; ×
d) x[0][0] = y; ×
e) y[0][0] = x[0]; √
f) x=y; ×
提示:一维数组:int[]x或者int x[]
二维数组:int[][]y 或者int[] y[] 或者int y[][]
3.使用二维数组打印一个10行杨辉三角
提示:第一行有1个元素,第n行有n个元素
每一行的第一个元素和最后一个元素都是1
从第三行开始,对于非第一个元素和最后一个元素,即yanghui[i][j] =yanghui[i-1][j-1] + yanghui[i-1][j];
/* 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 */ public class Experiment { public static void main(String[] args) { // TODO Auto-generated method stub //声明并初始化二维数组 int[][] yanghui = new int[10][]; //给数组元素赋值 for (int i = 0; i < yanghui.length; i++) { //声明并初始化yanghui[i] yanghui[i] = new int[i +1]; //给首末元素赋值 yanghui[i][0] = yanghui[i][i] = 1; //给非首末元素赋值 for (int j = 1; j < yanghui[i].length - 1; j++) { yanghui[i][j] = yanghui[i - 1][j - 1] + yanghui[i - 1][j]; } } //遍历二维数组 for (int i = 0; i < yanghui.length; i++) { for (int j = 0; j < yanghui[i].length; j++) { System.out.print(yanghui[i][j] + " "); } System.out.println(); } } }
数组中常见的算法
数组中涉及的常见算法:
1.数组元素的赋值(杨辉三角、回形数等)
2.求数值型数组中元素的最大值、最小值、平均数、总和等
3.数组的复制、反转、查找(线性查找、二分法查找)
4.数组元素的排序算法
1.数组元素的赋值(杨辉三角、回形数等)
使用二维数组打印一个10行杨辉三角
提示:第一行有1个元素,第n行有n个元素
每一行的第一个元素和最后一个元素都是1
从第三行开始,对于非第一个元素和最后一个元素,即yanghui[i][j] =yanghui[i-1][j-1] + yanghui[i-1][j];
/* 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 */ public class Experiment { public static void main(String[] args) { // TODO Auto-generated method stub //声明并初始化二维数组 int[][] yanghui = new int[10][]; //给数组元素赋值 for (int i = 0; i < yanghui.length; i++) { //声明并初始化yanghui[i] yanghui[i] = new int[i +1]; //给首末元素赋值 yanghui[i][0] = yanghui[i][i] = 1; //给非首末元素赋值 for (int j = 1; j < yanghui[i].length - 1; j++) { yanghui[i][j] = yanghui[i - 1][j - 1] + yanghui[i - 1][j]; } } //遍历二维数组 for (int i = 0; i < yanghui.length; i++) { for (int j = 0; j < yanghui[i].length; j++) { System.out.print(yanghui[i][j] + " "); } System.out.println(); } } }
2.求数值型数组中元素的最大值、最小值、平均数、总和等
定义一个int型的一维数组,包含10个元素,分别赋一些随机整数,然后求出所有元素的最大值,最小值,和值,平均值,并输出出来
要求:所有随机数都是两位数
public class Experiment { public static void main(String[] args) { // TODO Auto-generated method stub //声明并初始化 int[]arr = new int[10]; //获取10个随机数 for (int i = 0; i < arr.length; i++) { arr[i] = (int)(Math.random()*(99-10+1)) + 10; } //声明最大值、最小值、和值变量 int minnum = arr[0]; int maxnum = arr[0]; int sum = 0; for (int i = 1; i < arr.length; i++) { if (maxnum < arr[i]) { maxnum = arr[i]; } if (minnum > arr[i]) { minnum = arr[i]; } sum +=arr[i]; System.out.println(arr[i]); } int avgValue = sum / arr.length; System.out.println("最大值为:" + maxnum); System.out.println("最小值为" + minnum); System.out.println("总和为" + sum); System.out.println("平均数为" + avgValue); } }
3.数组的复制、反转、查找(线性查找、二分法查找)
①数组的复制
使用简单数组:
在main()方法中声明array1和array2两个变量,他们是int[]类型的数组
使用{},把array初始化为8个素数:2,3,5,7,11,13,17,19
显示array的内容
赋值array2变量等于array1,修改array2中的偶索引元素,使其等于索引值(如array[0] = 0,array[2] = 2)
思考:1.array1和array2是什么关系?
array1和array2地址值相同,都指向了堆空间的唯一的数组实体,array2没有重新开辟一个数组,也就是array2的修改实在array1上修改的,array2就相当于一个桌面的快捷方式不是独立的
2.修改题目,实现array2对array1数组的复制
//1.array1和array2是什么关系? public class Experiment { public static void main(String[] args) { // TODO Auto-generated method stub int[] array1,array2; array1 = new int[]{2,3,5,7,11,13,17,19}; for (int i = 0; i < array1.length; i++) { System.out.print(array1[i]); } System.out.println(); //赋值不等于复制 array2 = array1; for (int i= 0; i< array2.length;i++) { if (i % 2 == 0) { array2[i] = i; } System.out.print(array1[i]); } } }
//2.修改题目,实现array2对array1数组的复制 public class Experiment { public static void main(String[] args) { // TODO Auto-generated method stub int[] array1,array2; array1 = new int[]{2,3,5,7,11,13,17,19}; for (int i = 0; i < array1.length; i++) { System.out.print(array1[i]); } System.out.println(); //数组的复制 array2 = new int[array1.length]; for (int i = 0; i < array2.length; i++) { array2[i] = array1[i]; } for (int i= 0; i< array2.length;i++) { if (i % 2 == 0) { array2[i] = i; } System.out.print(array2[i]); //System.out.print(array1[i]); } } }
②数组的反转:
public class Experiment { public static void main(String[] args) { // TODO Auto-generated method stub String[] arr = new String[] {"JJ","DD","MM","BB","GG","AA","CC"}; //方法一: // for (int i = 0; i < arr.length / 2; i++) { // String temp = arr[i]; // arr[i] = arr[arr.length - i - 1]; // arr[arr.length - i - 1] = temp; // } //方法二: for (int i = 0,j = arr.length - 1; i < j; i++,j--) { String temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]);//CC,AA,GG,BB,MM,DD,JJ } } }
③数组的查找--线性查找://equal(arr[i]):看arr[i]中有与dest内容相同的吗
public class Experiment public static void main(String[] args) { // TODO Auto-generated method stub String[] arr = new String[] {"JJ","DD","MM","BB","GG","AA","CC"}; for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } String dest = "BB"; boolean isflag = true; for (int i = 0; i < arr.length; i++) { //equal(arr[i]):看arr[i]中有与dest内容相同的吗 if (dest.equals(arr[i])) { System.out.println("找到了,位置在" + i); isflag = false; break; } } if (isflag) { System.out.println("很遗憾,没有找到!"); } } }
③数组的查找--二分法查找
前提:所要查找的数组必须有序
public static void main(String[] args) { // TODO Auto-generated method stub int[] arr2 = new int[] {-98,-34,2,34,54,66,79,105,210,333}; int dest = -5; int head = 0; int end = arr2.length - 1; boolean isflag = true; while (head <= end) { int middle = (head + end) / 2; if (dest == arr2[middle]) { System.out.println("找到了,位置在" + middle); isflag = false; break; }else if (arr2[middle] < dest) { head = middle + 1; }else { end = middle - 1; } } if (isflag) { System.out.println("很遗憾,没有找到!"); } } }
数组中常见的异常
1.数组脚标越界异常(ArrayIndex OutOfBoundsException)
int[] arr =new int[2];
System.out.println(arr[2]);
System.out.println(arr[-3]);
2.空指针异常(NullPointerException)
情况一:
int[] arr1 = new int[]{1,2,3,4};
arr1 = null;//重新赋值
System.out.println(arr[0]);//空指针异常,因为:arr1又重新赋值了所以arr1[0]是空的,所以显示空指针异常
情况二:
int[][] arr2 = new int[4][];
System.out.println(arr2[0][0]);//空指针异常,因为:arr2[0]中还未初始化
情况三:
String[] arr3 = new String[] {"Aa","Bb","Cc"};
arr3[0] = null;
System.out.println(arr3[0].toString());//空指针异常,