2021-1-10
1. IDEA project structure
2. IDEA commonly used shortcut keys
3. Array Introduction (Array)
4. The definition format of the array
5. Array definition format
package com.ei.array;
/**
* @author wc
* @Date: 2021/01/11/10:12
*/
public class Demo01Array {
/*
数组定义格式:
1:数组类型[] 数组名;
2:数组类型 数组名[];
*/
public static void main(String[] args) {
//数组类型[] 数组名;
//定义一个int类型数组,数组名为arr
//int [] arr=创建数组容器;
int [] arr=;
System.out.println(arr);
}
}
6. Dynamic initialization of the array
package com.ei.array;
/**
* @author wc
* @Date: 2021/01/11/10:21
*/
public class Demo02Array {
/*
数组的动态初始化:
在数初始化的时候,需要手动指定数组的长度,系统会为数组容器分配初始值;
动态初始化格式:
数据类型[] 数组名=new 数据类型[数组的长度]
*/
public static void main(String[] args) {
//数据类型[] 数组名=new 数据类型[数组的长度]
//通过new关键字 创建了一个int类型的数组容器,该容器可以存储4个int类型数据,该容器被arr数组变量所记录
int[] arr=new int[4];
//[I@1b6d3586 内存地址
System.out.println(arr);
}
}
7. Array element access
package com.ei.array;
/**
* @author wc
* @Date: 2021/01/11/10:36
*/
public class Demo03ArrayIndex {
/*
数组动态初始化:
初始化的时候,手动指定数组长度,系统会为数组容器分配初始值
数组的元素访问格式:
数组名[索引]
索引:数组中数据的编号方式,编号从0开始;
作用:访问数组中的空间位置
*/
public static void main(String[] args) {
int [] arr=new int[3];
//默认初始值为0
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
System.out.println("===========================");
arr[0]=1;
arr[1]=3;
arr[2]=2;
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
}
}
8. Static initialization of the array
package com.ei.array1;
/**
* @author wc
* @Date: 2021/01/11/11:01
*/
public class Demo01 {
/*
数组静态初始化:初始化时指定每个数组元素的初始值,由系统决定数组长度
完整格式:
数据类型[] 数组名=new 数据类型[]{数据1,数据2.....};
简化格式:
数据类型[] 数组名={数据1,数据2.....};
*/
public static void main(String[] args) {
//数据类型[] 数组名=new 数据类型[]{数据1,数据2.....}
int[] arr=new int[]{
1,2,3};
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
System.out.println("================");
//数据类型[] 数组名={数据1,数据2.....}
int[] arr1={
1,3,4};
System.out.println(arr1[0]);
System.out.println(arr1[1]);
System.out.println(arr1[2]);
}
}
9. Array traversal (four examples)
package com.ei.test;
/**
* @author wc
* @Date: 2021/01/11/11:16
*/
public class TestArray {
public static void main(String[] args) {
//遍历:通过循环获取数组中的元素(数据)
int[] arr={
11,343,33,44,55,55};
for (int i = 0; i < 6; i++) {
System.out.println(arr[i]);
}
System.out.println("===================");
//代码优化:通过数组工具 数组名.length 自动获取数组的长度
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
Example 1.
Example 2
package com.ei.test;
import java.util.Scanner;
/**
* @author wc
* @Date: 2021/01/13/9:27
*/
public class SumArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0;
int[] arr = new int[5];
for (int i = 0; i < arr.length; i++) {
System.out.println("请输入第" + (i + 1) + "个数");
arr[i] = sc.nextInt();
}
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
System.out.println("输出和为"+sum);
}
}
Example three
Example four
package com.ei.test;
import java.util.Scanner;
/**
* @author wc
* @Date: 2021/01/13/10:27
*/
public class ScoreArray {
public static void main(String[] args) {
int[] arr = new int[6];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
int score = sc.nextInt();
if (score >= 0 && score <= 100) {
System.out.println("请输入第" + (i + 1) + "个数");
arr[i] = sc.nextInt();
} else {
System.out.println("输入有误");
i--;
}
}
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (max < arr[i]) {
max = arr[i];
System.out.println(max);
}
}
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (min > arr[i]) {
min = arr[i];
System.out.println(min);
}
}
}
}