我亦无他 唯手熟尔
化繁为简,先死后活
一、介绍
1.定义:
引用数据类型,用来存储统一类型的数据
二、数组的使用
1.动态初始化:
数据类型 数组名[] = new 数据类型[大小] == 数据类型[] 数组名 = new 数据类型[大小](推荐写法)
案例:循环输入5个数,保存到double数组
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double[] arr = new double[5];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextDouble();
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+"\t");
}
}
}
2.动态初始化:
先声明数组,再分配数组空间 数据类型[] 数组名; 数组名 = new 数据类型[大小]
int[] a ; a = new int[5] 等价于 int a[]; a =new int[5];
3.静态初始化:
数据类型 数组名[] = {数值} 等价于 数据类型[] 数组名 = {数值} 开发中用的不多
4.注意事项:
- 数组一定要保证数据类型的相同性,但是也可以保证小转大哦,double[] a = {100,1,31,100.1};
- 数组里面可以放任何数据,但是要保证数据的同一性
- 数组没有赋值是有默认值的,数组下标0开始
5.案例:
1.创建char类型数组,放置26个字符A-Z,并输出;
public class Hello {
public static void main(String[] args) {
char[] arr = new char[26];
for (int i = 0; i < arr.length; i++) {
arr[i] = (char) ('A' + i);
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+"\t");
}
}
}
2.求出一个数组的最大值,并求到其下标
public class Hello {
public static void main(String[] args) {
int[] arr = {4, -1, 9, 10, 23};
int max = 0, index = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i]>=max){
max = arr[i];
index = i;
}
}
System.out.println("index:"+index+" max: "+max);
}
}
三、数组赋值的机制
1.定义:
数组的默认是一个引用数据类型,赋值的是一个地址。
public class Hello {
public static void main(String[] args) {
int[] arr1 = {1,2,3};
int[] arr2 = arr1; //arr2的变化会影响arr1
arr2[2] = 10;
for (int i = 0; i < arr1.length; i++) {
System.out.println(arr1[i]); // 1 2 10
}
}
}
2.值传递 和 引用传递 的区别:
3.数组拷贝:
将int[] arr1 = {10,20,30} 拷贝到 arr2数组中。要求数据空间独立
public class Hello {
public static void main(String[] args) {
int[] arr1 = {10, 20, 30};
int[] arr2 = new int[arr1.length];
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
for (int i = 0; i < arr1.length; i++) {
System.out.print("arr1: "+ arr1[i]+'\t');
System.out.print("arr2: "+ arr2[i]+'\t');
}
}
}
4.数组的翻转:
public class Hello {
public static void main(String[] args) {
//思路,新数组的长度最后一位值为后数组的第一位
int[] arr1 = {11, 22, 33, 44, 55, 66};
int[] arr2 = new int[arr1.length];
for (int i = 0; i < arr1.length; i++) {
arr2[arr1.length-1-i] = arr1[i];
}
for (int i = 0; i < arr1.length; i++) {
System.out.println(arr2[i]+"");
}
}
}
5.数组的添加:
案例:用户可以自行决定是否添加
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr1 = {}, arr2 = {};
do {
arr2 = new int[arr1.length + 1];
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
System.out.println("please input int:");
arr2[arr1.length] = sc.nextInt();
arr1 = arr2;
System.out.println("continue?please input yes/no:");
if (sc.next().equals("no")) break;
} while (true);
for (int i = 0; i < arr1.length; i++) {
System.out.print(arr1[i] + "\t");
}
}
}
四、数组排序
1.冒泡排序:
定义:一个一个往上走;
总结:5个数据进行4次排序(外层),每一次排序确定一个元素的位置,前面的数大于后面的数进行交换,每一次少一个排序
public class Hello {
public static void main(String[] args) {
int[] arr = {24, 69, 80, 57, 13, 200};
int temp = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
2.查找
案例1:顺序查找:
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] names = {"pear", "apple", "banana", "tomato", "grape"};
System.out.println("please input fruit name: ");
String name = sc.next();
boolean flag = false;
for (int i = 0; i < names.length; i++) {
if (name.equals(names[i])) {
flag = !flag;
System.out.println("suecess!!!,index: " + i);
}
}
if (!flag) System.out.println("faith,not found");
}
}
案例2:二分查找(高级编程的时候讲)
五、二维数组
1.动态初始化:
语法:类型[][] 数组名 = new 类型[大小][大小]
public class Hello {
public static void main(String[] args) {
int[][] arr = new int[2][3];
arr[1][1] = 8;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
2.静态初始化:
定义:int[][] arr ={{0,0,0},{0,8,0}}
3.列不确定声明:
定义:int[][] arr = new int[3][],arr[i] = new int[];
六、二维数组的遍历
案例1:遍历数组并求和:
public class Hello {
public static void main(String[] args) {
int[][] arr ={{4,6},{1,4,5,7},{-2}};
int sum = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j]+" ");
sum+=arr[i][j];
}
System.out.println();
}
System.out.println("sum: "+sum);
}
}
案例2:杨辉三角:
注意:第三行开始有: arr[i][j] = arr[i-1][j-1] + arr[i-1][j] (自己写的很6啊)
public class Hello { //1
public static void main(String[] args) { //1 1
int[][] arr = new int[10][]; //1 2 1
for (int i = 0; i < arr.length; i++) { //1 3 3 1
arr[i] = new int[i + 1]; //开辟空间很六啊 //1 4 6 4 1
for (int j = 0; j < arr[i].length; j++) { //1 5 10 10 5 1
//第三行开始有: arr[i][j] = arr[i-1][j-1] + arr[i-1][j]
if (i == j || j == 0) {
arr[i][j] = 1;
}else{
arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
}
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
3.细节:
定义:二维数组声明 int[][] y(推荐) 或者 int[] y[] 或者 int y[][]
二维数组是由一维数组构成
注意:int[] x,y[]; ===>x为一维数组,y为二维数组;
七、课后作业
好好理解上面的代码