2.数组引入概述:一组连续的内存空间,用于存储相同类型的元素
特点:1.类型相同;2.长度固定
需求:存储 全班同学的姓名
弊端:如果用String,定义变量的数量太多,冗余代码过多
String name1 = 张宗周; String name2 = 杜文纤;
需求:给每位同学年龄都加1
弊端:不方便操作及统一管理
agel += l; age2 += 1;3.数组的创建
- 声明数组,再定义空间
int[] a; a = new int[3];
声明数组同时,进行定义空间(常用)
适用于控制台录入多个元素值
int[] b = new int[3]; b[0] = 1; b[1] = 2; b[2] = 3;
定义空间的同时 ,进行赋值
注意:有多少个值,代表数组长度是多少
int[] c = new int[] {3,5,6};
简化版的直接赋值方式(常用)
适用于简单测试
int[] d = {1,3,5};
4.数组下标
数组通过下标进行操作,范围:0~长度-1
定义3个整数类型的数组空间
int[] a = new int[3];
// 通过下标进行操作;范围:0 ~ 长度-1
a[0] = 1;//通过下标给元素赋值
a[1] = 2;
a[2] = 3;
//a[3] = 3;数组下标越界
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
//System.out.println(a[3]);数组下标越界
5.数组默认值问题
如果定义数组,不赋值,默认值是多少
public class Test2 {
public static void main(String[] args) {
// 定义整型数组
int[] a = new int[3];
System.out.println(a[0]);//0
System.out.println(a[1]);
System.out.println(a[2]);
// 定义字符串类型数组
String[] b = new String[3];
System.out.println(b[0]);//null 关键字,引用类型给定的空值
System.out.println(b[1]);
System.out.println(b[2]);
//定义小数类型数组
double[] c = new double[3];
System.out.println(c[0]);//0.0
System.out.println(c[1]);
System.out.println(c[2]);
//定义字符类型数组
char[] d = new char[3];
System.out.println(d[0]);//ascii为0的字符 -空格
System.out.println(d[1]);
System.out.println(d[2]);
//定义布尔类型的 数组
boolean[] e= new boolean[3];
System.out.println(e[0]);//false
System.out.println(e[1]);
System.out.println(e[2]);
}
}
6.数组的循环遍历
数组x[]的长度为:x.length遍历的概念:依次取出数组中的每一个元素
方式一:普通的for循环
public class Test3 {
public static void main(String[] args) {
int[] a = new int[3];
a[0] = 1;
a[1] = 2;
a[2] = 3;
//数组的循环遍历
System.out.println("数组长度:"+a.length);
for(int i=0;i
1.5以后Java新增foreach循环和可变参数
方式2:使用增强for循环(foreach循环)
int[] a = new int[3];
a[0] = 1;
a[1] = 2;
a[2] = 3;
for(int x ; a) {//把a中的值一个一个取出来赋给x
System.out.println(x);
}
方式3:调用方法
public class Test13 {
public static void main(String[] args) {
int[] a = new int[3];
a[0] = 1;
a[1] = 2;
a[2] = 3;
// 用方法遍历
print1(a);
}
public static void print1(int[] x) {
for (int i = 0; i < x.length; i++) {
System.out.print(x[i]);
}
}
}
方式4:用可变参数(jkd1.5之后)遍历
注意可变参数只能是参数列表最后一个
public class Test13 {
public static void main(String[] args) {
int[] a = new int[3];
a[0] = 1;
a[1] = 2;
a[2] = 3;
// 可变参数的本质是数组
print2(a);
}
public static void print2(int... x) {
for (int i = 0; i < x.length; i++) {
System.out.print(x[i]);
}
}
}
7.数组异常测试
空指针
//测试数组的异常
print3(null);
//java.lang.NullPointerException
//当一个变量为null(没有被赋值时),我们调用了该变量的属性和方法
public static void print3(int[] x){
System.out.println(x.length);
}
}
下标越界
int[] a = new int[3];
a[0] = 1;
a[1] = 2;
a[2] = 3;
print2(a);
}
public static void print2(int... x) {
for (int i = 0; i <= x.length; i++) {//此处<=越界了
System.out.print(x[i]);
}
8.栈内存和堆内存
数组是引用类型,放在堆里面
引用类型在栈里,指向堆,即实际存放的位置在堆中
栈内存:
用于存储局部,临时变量(基本数据类型)和引用类型大小固定!
堆内存:
大小不固定,用来存储对象
9.数组案例练习
打印正三角形
public class Test16 {
public static void main(String[] args) {
char[] c = new char[] { 'A', 'B', 'C', 'D', 'F', 'G' };
for (int i = 1; i <= c.length; i++) {
for (int j = i; j < c.length; j++) {
System.out.print("_");
}
for (int j = 1; j <= i * 2 - 1; j++) {
System.out.print(c[i - 1]);
}
System.out.println();
}
}
}
给定一个整数数组,统计数组元素的平均值
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
System.out.println("请给定一个数组长度:");
Scanner input = new Scanner(System.in);
int size = input.nextInt();
int[] a = new int[size];
double sum = 0;
for (int i = 0; i < size; i++) {
System.out.println("请录入第" + (i + 1) + "个元素");
a[i] = input.nextInt();
sum += a[i];
}
System.out.println("平均值为:" + sum / size);
}
}
给定一个整数数组,从控制台录入一个数如,果在数组中存在,则输出下标,不存在,则输出-1
分析:直接赋值数组元素,控制台接受一个数,循环遍历后,与数组元素匹配,如果匹配上,则记录下标:否则输出-1
public class Test2 {
public static void main(String[] args) {
int[] a = { 3, 2, 8, 9 };
System.out.println(Arrays.toString(a));// 打印数组方式
Scanner input = new Scanner(System.in);
System.out.println("请输入一个数:");
int num = input.nextInt();
int index = -1;
// 循环遍历
for (int i = 0; i < a.length; i++) {
if (num == a[i]) {
index = i;
break;// 退出循环
}
}
System.out.println("下标为:" + index);
}
}
10.数组扩容
创建一个比原数组更大的空间,将原数组拷贝到目标数组中
自定义扩容方式
public class Test1 {
public static void main(String[] args) {
int[] a = { 1, 3, 5 };
int[] b = new int[a.length + 1];
for (int i = 0; i < a.length; i++) {
b[i] = a[i];
}
System.out.println(Arrays.toString(b));
}
}
系统提供的扩容方法1:System.arraycopy
参数1:原数组参数2:原数组的哪个位置开始拷贝,一般写0参数3:目标数组参数4:拷贝到目标数组的哪个位置,一般写0参数5:要拷贝的长度,一般写原数组长度
System.arraycopy
import java.util.Arrays;
public class Test2 {
public static void main(String[] args) {
int[]a= {1,3,5};
int[]b=new int[a.length+1];
System.arraycopy(a, 0, b, 0, a.length);
System.out.println(Arrays.toString(b));
}
}
系统提供的扩容方法2: copyOf(常用)
参数1:原数组参数2:扩容的长度返回值: 扩容后的数组
copyOf(常用)
public class Test3 {
public static void main(String[] args) {
int[] a = { 1, 2, 5 };
int[] b = Arrays.copyOf(a, a.length + 1);
System.out.println(Arrays.toString(b));
}
}
11.二维数组
Java中没有真正的多维数组,多维数组的表示方式是数组中的元素还是数组
12.二维数组创建
方式一:声明并分配空间(常用)
int[][] a = new int[2][3];// 创建2行3列的二维空间数组
// 可以通过行下标和列下标进行赋值与取值:
// 行下标范围:0 ~ 长度-1
// 列下标范围:0 ~ 长度-1
a[0][0] = 6;
a[0][1] = 3;
a[0][2] = 5;
// a[0][3]=8;//列下标越界
a[1][0] = 9;
// a[2][0]=4;//行下标越界
// 没有赋值的元素默认为0
方式二:分配行空间并赋值(复杂)
int[][] b = new int[2][];
b[0] = new int[] { 1, 3, 5 };
b[1] = new int[] { 2, 4, 6 };
方式三:创建空间的同时直接赋值(常用)
int[][] c = { { 1, 2, 3 }, { 4, 5, 6 } };
打印二维数组
int[][] a = new int[2][3];
a[0][0] = 6;
a[0][1] = 3;
a[0][2] = 5;
a[1][0] = 9;
System.out.println("行长度:" + a.length);
System.out.println("列长度:" + a[0].length);
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
案例:一起来参加程序员大赛吧,有3个班级各3名学员参赛,记录每个学员的成绩,并计算每个班的平均分。
public class Test17 {
public static void main(String[] args) {
int[][] scores = { { 90, 89, 78 }, { 78, 89, 99 }, { 88, 77, 99 } };
for (int i = 0; i < scores.length; i++) {
int sum = 0;
for (int j = 0; j < scores[i].length; j++) {
System.out.print(scores[i][j] + " ");
sum += scores[i][j];
}
System.out.println("第" + (i + 1) + "班的平均分为" + sum / scores[i].length);
}
}
}
13.不规则二维数组
分析:先创建空间的方式不能设置不规则的二维数组注意:不规则的二维数组一般不会去设置
// 方式一
int[][] a = new int[2][];
a[0] = new int[] { 1, 3, 5 };
a[1] = new int[] { 2, 4, 6, 7, 8 };
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~");
// 方式二
int[][] c = { { 1, 2, 3 }, { 4, 5, 6, 7, 8, 9 } };
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();
}
杨辉三角
import java.util.Iterator;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入杨辉三角的行(列)数:");
int n = input.nextInt();
int[][] a = new int[n][n];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) {
a[i][j] = 1;
} else {
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
14.方法传参为引用类型(重点难点)
数组作为方法参数的应用:
使用方法封装来打印数组
public class Test1 {
public static void main(String[] args) {
int[] a = { 1, 5, 6 };
printArray(a);
}
private static void printArray(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
15.值传递,地址传递
值传递
结论:值传递时,形参值的改变不影响实参
public class Test2 {
public static void main(String[] args) {
int a = 3;
System.out.println(a);
System.out.println(change(a));
}
private static int change(int a) {
a++;
return a;
}
}
地址传递
方法参数为引用类型
结论:地址的传递时,形参的改变会影响实参
public class Test3 {
public static void main(String[] args) {
int[] a = { 1, 2, 3 };
change(a);
System.out.println(Arrays.toString(a));
}
private static void change(int[] a) {
a[2] = 4;
}
}
16.数组作为返回值应用
传入一个数组,在方法实现中进行扩容后,返回出来;并打印返回的数组
import java.util.Arrays;
//相当于直接定义一个copyof方法
public class Test1 {
public static void main(String[] args) {
int[] a = { 1, 2, 3 };
a = copyof(a, a.length + 1);
System.out.println(Arrays.toString(a));
}
private static int[] copyof(int[] src, int length) {
int[] dest = new int[length];
// 循环遍历,将原数组的值挨个拷贝到目标数组
for (int i = 0; i < src.length; i++) {
dest[i] = src[i];
}
return dest;
}
}
17.可变长参数
可变参数: int… a: 可以直接接收多个同种类型的参数本质:数组,如果形参为可变参数,那么实参自动会生成new数组的方式细节:可变参数只能放在形参的最后,且参数中只能有一个可变参数
public class Test1 {
public static void main(String[] args) {
//int[] a = {1,2,3};
//printArray(a); //普通数组传参
//printArray(new int[]{1,2,3}); //直接new数组传参
printArray(1,2,3); //直接传多个变量
}
private static void printArray(int b,int... a) {
for(int i=0;i
思考:
数组作为参数,和基本类型变量作为参数有区别吗
数组是地址传递,会直接影响实参!
地址的传递时,形参的改变会影响实参
值传递时,形参值的改变不影响实参
作业
创建整数数组,并进行赋值,长度为5(备注好数组的组成、标识符、长度、下标,数据类型)
import java.util.Arrays;
import java.util.Scanner;
public class d6_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] a= new int[5];
for(int i=0;i 使用3种方式创建整数数组,长度为5。
public class d6_2 {
public static void main(String[] args) {
// 方法一
int[] a = new int[5];
// 方法二
int[] b;
b = new int[5];
// 方法三
int[] c = new int[] { 1, 2, 3, 4, 5 };
// 方法四
int[] d = { 1, 2, 3, 4, 5 };
}
}
1个班5人java成绩,创建浮点数数组,使用循环输入、再找到成绩最高的分数,并输出
提示:先new数组空间,循环赋值给数组元素,定义一个max变量赋值第一个元素,依次与数组元素比较,得出最大的
import java.util.Scanner;
public class d6_3 {
public static void main(String[] args) {
double[] a = new double[5];
Scanner input = new Scanner(System.in);
double max = 0;
for (int i = 0; i < a.length; i++) {
System.out.println("输入第" + (i + 1) + "个分数");
double b = input.nextDouble();
a[i] = b;
}
for (int i = 0; i < a.length - 1; i++) {
if (a[i] < a[i + 1]) {
max = a[i + 1];
} else {
max = a[i];
}
}
System.out.println(max);
}
}
创建数组长度为5,然后长度扩容为10
import java.util.Arrays;
public class d6_4 {
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4, 5 };
int[] b = { 1, 2, 3, 4, 5 };
int[] c = { 1, 2, 3, 4, 5 };
// 方法1
int[] a1 = new int[a.length + 5];
for (int i = 0; i < a.length; i++) {
a1[i] = a[i];
}
System.out.println(Arrays.toString(a1));
// 方法2
int[] b1 = new int[a.length + 5];
System.arraycopy(b, 0, b1, 0, a.length);
System.out.println(Arrays.toString(b1));
// 方法3
int[] c1 = Arrays.copyOf(c, c.length + 5);
System.out.println(Arrays.toString(c1));
}
}
1个班5人java成绩,创建方法,参数为数组,计算平均分,然后所有学员50分以下+10分,50分以上的加5分,并将结果数组返回,接受后输出
提示:1.创建5个空间的数组,循环接收录入的值,求出平均分
2.再循环遍历,根据判断加分;最后再打印
package com.qf.Day6HomeWork;
import java.util.Arrays;
import java.util.Scanner;
public class d6_5 {
public static void main(String[] args) {
double[] a = new double[5];
Scanner input = new Scanner(System.in);
double sum1 = 0;
double sum2 = 0;
for (int i = 0; i < a.length; i++) {
System.out.println("输入第" + (i + 1) + "个分数");
double b = input.nextDouble();
a[i] = b;
sum1 += b;
}
for (int i = 0; i < a.length; i++) {
if (a[i] < 50) {
a[i] += 10;
} else if (a[i] > 50) {
a[i] += 5;
}
}
for (int j = 0; j < a.length; j++) {
sum2 += a[j];
}
System.out.println(Arrays.toString(a));
System.out.println("加分前平均分:" + sum1 / a.length);
System.out.println("加分后平均分:" + sum2 / a.length);
input.close();
}
}



