前言:此为个人笔记梳理,如若侵权,请联系作者删除
1. 数组定义及使用
数组是一组相关变量的集合
其优点对比:
1.不使用数组定义100个整型变量:int i1;int i2;……;int i3;
2.使用数组定义:int i[100];
- 一维数组
使用 Java 数组,经过两个步骤:1.声明数组 2.分配内存给该数组
声明形式一:
声明一维数组:数据类型 数组名[] = null;
分配内存给数组:数组名 = new 数据类型[长度];
声明形式二:
声明一维数组:数据类型[] 数组名 = null;
变量的命名规则:第一个单词的首字母小写,之后每个单词的首字母都大写,例:studentName
public class ArrayDemo01 {
public static void main(String args[]) {
int score[] = null; //声明数组
score = new int[3]; //为数组开辟空间,大小为3
System.out.println("score[0] = " + score[0]);
System.out.println("score[1] = " + score[1]);
System.out.println("score[2] = " + score[2]);
}
}
打印结果:
score[0] = 0
score[1] = 0
score[2] = 0
注:int 为基本数据类型,其默认值都是0
一个堆内存空间可以同时被多个栈内存空间指向
声明数组的同时分配内存空间
数据类型 数组名[] = new 数据类型[个数];
示例:int score[] = new int[10];
注:数组元素通过”数组名称 [下标,索引]”的形式保存
数组的访问也通过循环的方式进行操作,循环操作的时候只需要改变其索引(下标)即可
示例:
for(int x=0;x<3;x++) {
System.out.println("score[" + x + "] = " + score[x]);
}
- 访问注意
访问超过所能访问的范围:报错
java.lang.ArrayIndexOutOfBoundsException:3
数组的索引超出绑定,就是表示数组越界
public class ArrayDemo02 {
public static void main(String args[]) {
int score[] = null; //声明数组
score = new int[3]; //为数组开辟空间,大小为3
for(int x=0;x<3;x++) {
score[x] = x*2+1;
}
for(int x=0;x<3;x++) {
System.out.println("score[" + x + "] = " + score[x]); //循环打印结果
}
}
}
打印结果:
score[0] = 1
score[1] = 3
score[2] = 5
取得数组长度:
System.out.println("数组长度为:" + score.length);
- 数组的静态初始化
数据类型 数组名[] = {初始值0,初始值1,……,初始值n};
public class ArrayDemo04 {
public static void main(String args[]) {
int score[] = {91,92,93,94,95,96}; //使用静态初始化声明
for(int x=0;x<score.length;x++) {
System.out.println("score[" + x + "] = " + score[x]); //循环打印结果
}
}
}
打印结果:
score[0] = 91
score[1] = 92
score[2] = 93
score[3] = 94
score[4] = 95
score[5] = 96
范例一:求出最大值和最小值
public class ArrayDemo05 {
public static void main(String args[]) {
int score[] = {67,89,87,69,90,100,75,90}; //使用静态初始化声明
int max = 0;
int min = 0;
max = min = score[0]; //先赋值
for(int x=0;x<score.length;x++) {
if(max<score[x]) {
max = score[x]; //取两者最大值
}
if(min>score[x]) {
min = score[x]; //取两者最小值
}
}
System.out.println("最大值为:" + max);
System.out.println("最小值为:" + min);
}
}
打印结果:
最大值为:100
最小值为:67
范例二:排序,从小到大(冒泡排序)
public class ArrayDemo06 {
public static void main(String args[]) {
int score[] = {67,89,87,69,90,100,75,90}; //使用静态初始化声明
int temp;
for(int i=1;i<score.length;i++) {
for(int j=0;j<score.length;j++) {
if(score[j]>score[i]) {
temp = score[i];
score[i] = score[j];
score[j] = temp;
}
}
}
for(int k=0;k<score.length;k++) {
System.out.print(score[k] + "\t");
}
}
}
打印结果:
67 69 75 87 89 90 90 100
冒泡排序完整详细过程:
public class ArrayDemo07 {
public static void main(String args[]) {
int score[] = {67,89,87,69,90,100,75,90}; //使用静态初始化声明
int temp;
for(int i=1;i<score.length;i++) {
System.out.print("第" + i + "次过程:");
for(int j=0;j<score.length;j++) {
if(score[j]>score[i]) {
temp = score[i];
score[i] = score[j];
score[j] = temp;
}
}
for(int k=0;k<score.length;k++) {
System.out.print(score[k] + "\0\0");
}
System.out.println("");
}
System.out.print("最后打印结果:");
for(int k=0;k<score.length;k++) {
System.out.print(score[k] + "\0\0");
}
}
}
打印结果:
第1次过程:67 100 87 69 89 90 75 90
第2次过程:67 87 100 69 89 90 75 90
第3次过程:67 69 87 100 89 90 75 90
第4次过程:67 69 87 89 100 90 75 90
第5次过程:67 69 87 89 90 100 75 90
第6次过程:67 69 75 87 89 90 100 90
第7次过程:67 69 75 87 89 90 90 100
最后打印结果:67 69 75 87 89 90 90 100
- 二维数组
动态初始化:
1.数据类型[][] 数组名 = new 数据类型[行数][列数];
2.数据类型 数组名[][] = new 数据类型[行数][列数]; //不推荐使用
public class ArrayDemo08 {
public static void main(String args[]) {
int score[][] = new int [4][3]; //动态初始化
score[0][1] = 30;
score[1][0] = 31;
score[2][2] = 32;
score[3][1] = 33;
score[1][1] = 30;
for(int i=0;i<score.length;i++) { //行数
for(int j=0;j<score[i].length;j++) { //列数
System.out.print(score[i][j] + "\t");
}
System.out.println("");
}
}
}
打印结果:
0 30 0
31 30 0
0 0 32
0 33 0
静态初始化
格式:
数据类型 数组名[][] = {
{第0行初值},{第1行初值},……,{第n行初值}
};
public class ArrayDemo09 {
public static void main(String args[]) {
int score[][] = {
{67,61},{78,89,83},{99,100,98,66,95}
}; //静态初始化
for(int i=0;i<score.length;i++) { //行数
for(int j=0;j<score[i].length;j++) { //列数
System.out.print(score[i][j] + "\t");
}
System.out.println("");
}
}
}
打印结果:
67 61
78 89 83
99 100 98 66 95
- 多维数组(不建议使用过多,耗费计算机的资源)
public class ArrayDemo10 {
public static void main(String args[]) {
int score[][][] = {
{
{5,1},{6,7}
},
{
{9,4},{8,3}
}
};
for(int i=0;i<score.length;i++) {
for(int j=0;j<score[i].length;j++) {
for(int k=0;k<score[i][j].length;k++) {
System.out.println("score[" + i + "]" + "[" + j + "]" + "[" + k + "] = " + score[i][j][k]);
}
//System.out.println("");
}
//System.out.println("");
}
}
}
打印结果:
score[0][0][0] = 5
score[0][0][1] = 1
score[0][1][0] = 6
score[0][1][1] = 7
score[1][0][0] = 9
score[1][0][1] = 4
score[1][1][0] = 8
score[1][1][1] = 3
2. 方法的声明及使用
方法是一段可重复调用的代码段
定义格式:
public static 返回值类型 方法名称(类型 参数1,类型 参数2,...) {
//方法的主体
程序语句;
[return 表达式];
}
- 无返回值的方法
public class MethodDemo01 {
public static void main(String args[]) {
printInfo();
printInfo();
printInfo();
System.out.println("Hello World!");
}
public static void printInfo() {
char[] c = {'H','e','l','l','o',',','L','X','H'}; //定义字符数组
for(int x=0;x<c.length;x++) {
System.out.print(c[x]);
}
System.out.println(""); //换行
}
}
打印结果:
Hello,LXH
Hello,LXH
Hello,LXH
Hello World!
注:方法==函数
方法命名规范要求:第一个单词的首字母小写,其余单词的首字母均大写
- 有返回值的方法
public class MethodDemo02 {
public static void main(String args[]) {
int one = addOne(10,20);
float two = addTwo(10.3f,13.3f);
System.out.println("addOne的计算结果:" + one);
System.out.println("addTwo的计算结果:" + two);
}
public static int addOne(int x,int y) {
int temp = 0; //局部变量
temp = x + y;
return temp;
}
public static float addTwo(float x,float y) {
float temp = 0;
temp = x + y;
return temp;
}
}
打印结果:
addOne的计算结果:30
addTwo的计算结果:23.6
- 方法的重载
定义:方法名称相同,但参数的类型和参数的个数不同,通过传递参数的个数及类型不同以完成不同功能方法的调用
public class MethodDemo03 {
public static void main(String args[]) {
int one = add(10,20);
float two = add(10.3f,13.3f);
int three = add(10,20,30);
System.out.println("one的计算结果:" + one);
System.out.println("two的计算结果:" + two);
System.out.println("three的计算结果:" + three);
}
public static int add(int x,int y) {
int temp = 0; //局部变量
temp = x + y;
return temp;
}
public static float add(float x,float y) {
float temp = 0;
temp = x + y;
return temp;
}
public static int add(int x,int y,int z) {
int temp = 0; //局部变量
temp = x + y + z;
return temp;
}
}
打印结果:
one的计算结果:30
two的计算结果:23.6
three的计算结果:60
提示:System.out.println(); 方法也属于重载方法;八种基本数据类型,传入其中,利用重载,打印其 toString() 方法
- 重载的注意事项
方法的重载一定只是在参数上的类型或个数不同
public class MethodDemo04 {
public static int add(int x,int y) {
int temp = 0; //局部变量
temp = x + y;
return temp;
}
public static float add(int x,int y) {
float temp = 0;
temp = x + y;
return temp;
}
}
打印结果:
MethodDemo04.java:7: 错误: 已在类 MethodDemo04中定义了方法 add(int,int)
public static float add(int x,int y) {
^
1 个错误
- 使用 return 结束一个方法
public class MethodDemo05 {
public static void main(String args[]) {
System.out.println("1.调用fun()方法之前");
fun(10);
System.out.println("2.调用fun()方法之后");
}
public static void fun(int x) {
System.out.println("3.进入fun()方法");
if(x==10) {
return; //结束方法,返回被调用处
}
System.out.println("4.正常执行完fun()方法");
}
}
打印结果:
1.调用fun()方法之前
3.进入fun()方法
2.调用fun()方法之后
注:return 除了可以返回内容之外,还可以结束方法
- 方法的递归调用
定义:方法自己调用自己
public class MethodDemo06 {
public static void main(String args[]) {
System.out.println("计算结果:" + sum(100));
}
public static int sum(int num) {
if(num==1) {
return 1;
}else {
return num + sum(num-1);
}
}
}
打印结果:
计算结果:5050
代码的执行过程:
100+sum(99)
100+99+sum(98)
100+99+98+sum(97)
.
.
.
100+99+98+……+1
- 数组的引用传递
接受和返回数组
public class ArrayReDemo01 {
public static void main(String args[]) {
int[] temp = {1,3,5};
fun(temp);
for(int i=0;i<temp.length;i++) {
System.out.print(temp[i] + "、");
}
}
public static void fun(int[] x) {
x[0] = 6; //引用修改并传递
}
}
打印结果:
6、3、5、
public class ArrayReDemo02 {
public static void main(String args[]) {
int[] temp = fun();
print(temp);
}
public static int[] fun() {
int ss[] = {1,3,5,7,9};
return ss;
}
public static void print(int[] x) {
for(int i=0;i<x.length;i++) {
System.out.print(x[i] + "、");
}
}
}
打印结果:
1、3、5、7、9、
- 范例讲解
定义排序方法
public class ArrayReDemo03 {
public static void main(String args[]) {
int[] score = {67,89,87,69,90,100,75,90};
int[] age = {31,30,18,17,8,9,1,39};
sort(score);
print(score);
System.out.println("====我是分割线====");
sort(age);
print(age);
}
public static int[] sort(int[] temp) {
for(int i=1;i<temp.length;i++) {
for(int j=0;j<temp.length;j++) {
if(temp[j]>temp[i]) {
int x = temp[j];
temp[j] = temp[i];
temp[i] = x;
}
}
}
return temp;
}
public static void print(int[] temp) {
for(int i=0;i<temp.length;i++) {
System.out.print(temp[i] + "\t");
}
System.out.println("");
}
}
打印结果:
67 69 75 87 89 90 90 100
====我是分割线====
1 8 9 17 18 30 31 39
一个操作中,既要求可以排序整型,也可以排序浮点型等各种数据(调用 Java 自带的类库)
public class ArrayReDemo03 {
public static void main(String args[]) {
int[] score = {67,89,87,69,90,100,75,90};
int[] age = {31,30,18,17,8,9,1,39};
java.util.Arrays.sort(score);
print(score);
System.out.println("====我是华丽的分割线====");
java.util.Arrays.sort(age);
print(age);
}
/*public static int[] sort(int[] temp) {
for(int i=1;i<temp.length;i++) {
for(int j=0;j<temp.length;j++) {
if(temp[j]>temp[i]) {
int x = temp[j];
temp[j] = temp[i];
temp[i] = x;
}
}
}
return temp;
}*/
public static void print(int[] temp) {
for(int i=0;i<temp.length;i++) {
System.out.print(temp[i] + "\t");
}
System.out.println("");
}
}
打印结果:
67 69 75 87 89 90 90 100
====我是华丽的分割线====
1 8 9 17 18 30 31 39
- 数组拷贝
过程思路:源数组、源数组的开始点、目标数组、目标数组的开始点、拷贝的长度
public class ArrayReDemo05 {
public static void main(String args[]) {
int[] i1 = {1,2,3,4,5,6,7,8,9}; //源数组
int[] i2 = {11,22,33,44,55,66,77,88,99}; //目标数组
System.out.println("没拷贝过的:");
print(i2);
copy(i1,3,i2,1,3);
System.out.println("已经拷贝过的:");
print(i2);
}
public static int[] copy(int[] s,int s1,int[] o,int s2,int len) {
for(int i=0;i<len;i++) {
o[s2+i] = s[s1+i];
}
return o;
}
public static void print(int[] temp) {
for(int i=0;i<temp.length;i++) {
System.out.print(temp[i] + "\t");
}
System.out.println("");
}
}
打印结果:
没拷贝过的:
11 22 33 44 55 66 77 88 99
已经拷贝过的:
11 4 5 6 55 66 77 88 99
替换:(使用 Java 自带的类库)
public class ArrayReDemo06 {
public static void main(String args[]) {
int[] i1 = {1,2,3,4,5,6,7,8,9}; //源数组
int[] i2 = {11,22,33,44,55,66,77,88,99}; //目标数组
System.out.println("没拷贝过的:");
print(i2);
System.arraycopy(i1,3,i2,1,3); //调用Java中对数组支持的拷贝方法
System.out.println("已经拷贝过的:");
print(i2);
}
/*public static int[] copy(int[] s,int s1,int[] o,int s2,int len) {
for(int i=0;i<len;i++) {
o[s2+i] = s[s1+i];
}
return o;
}*/
public static void print(int[] temp) {
for(int i=0;i<temp.length;i++) {
System.out.print(temp[i] + "\t");
}
System.out.println("");
}
}
打印结果:
没拷贝过的:
11 22 33 44 55 66 77 88 99
已经拷贝过的:
11 4 5 6 55 66 77 88 99
- 总结
- 在方法中,对数组所做的修改都会被保留下来
- 数组的引用传递,传递的就是堆内存的使用权,可以将一个数组传递到方法之中,传递的时候不需要写上“[ ]”,直接写名字即可
3. Java 新特性对数组的支持
- 可变参数
之前的参数规定几个,就要传几个。但 JDK 1.5 之后,为了让方法更加具有灵活性,使用了可变参数的概念,调用方法时可任意多的传递参数
格式如下:
返回值类型 方法名称(类型...参数名称) {}
注:所有的可变参数接受之后,都是以数组的形式保存下来,所以直接按数组的方式接收即可
public class NewDemo01 {
public static void main(String args[]) {
System.out.println("不传递参数(fun()):");
fun();
System.out.println("传递一个参数(fun()):");
fun(1);
System.out.println("传递五个参数(fun(1,2,3,4,5)):");
fun(1,2,3,4,5);
}
public static void fun(int...arg) { //可变参数
for(int i=0;i<arg.length;i++) {
System.out.print(arg[i] + "、");
}
System.out.println("");
}
}
打印结果:
不传递参数(fun()):
传递一个参数(fun()):
1、
传递五个参数(fun(1,2,3,4,5)):
1、2、3、4、5、
- foreach 输出
JDK 1.5 之后推出,代替 for 循环输出,为了方便数组的输出
格式:
for(数据类型 变量名称 : 数组名称) {
…… ……
}
public class NewDemo02 {
public static void main(String args[]) {
System.out.println("不传递参数(fun()):");
fun();
System.out.println("传递一个参数(fun()):");
fun(1);
System.out.println("传递五个参数(fun(1,2,3,4,5)):");
fun(1,2,3,4,5);
}
public static void fun(int...arg) { //可变参数
for(int x : arg) { //使用foreach输出
System.out.print(x + "、");
}
System.out.println("");
}
}
打印结果:
不传递参数(fun()):
传递一个参数(fun()):
1、
传递五个参数(fun(1,2,3,4,5)):
1、2、3、4、5、