目录
一、什么是数组
二、数组的定义和访问
2.1 静态初始化数组
1、数组的访问
2、数组的遍历
3、静态数组案例训练
2.2 动态初始化数组
动态数组案例训练
三、数组在计算机中的原理
3.1 数组的执行原理,java程序的执行原理
3.2 多个变量指向同一数组问题
四、数组专项训练
4.1 数组求最值
4.2 数组反转
4.3 随机排名
package com.runa.define;
import java.lang.reflect.Array;
public class ArrayDemo1 {
public static void main(String[] args) {
int[] ages = new int[]{16, 17, 19};
double[] scores = new double[]{89.9, 99.5, 59.7, 88};
// 简化写法
int[] ages2 = {16, 17, 19};
double[] scores2 = {89.9, 99.5, 59.7, 88};
// 另一种写法
int ages3[] = {16, 17, 19};
double scores3[] = {89.9, 99.5, 59.7, 88};
}
}
package com.runa.define;
public class ArrayDemo2 {
public static void main(String[] args) {
// 1、数组的访问
int[] arr = {23, 34, 45};
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
//2、修改数组
arr[0] = 100;
System.out.println(arr[0]);
// 3、访问数据元素个数(长度)
System.out.println("数组的长度为:" + arr.length);
// 4、获取数组的最大索引(前提元素个数大于1)
System.out.println(arr.length-1);
}
}
package com.runa.define;
public class ArrayDemo3 {
public static void main(String[] args) {
// 1 定义一个数组
int[] arr = {12,15,65,15,17};
//2 循环访问==正向
for(int i=0; i< arr.length; i++){
System.out.println(arr[i]);
}
//3 逆向循环
for (int i = arr.length - 1; i >= 0; i--) {
System.out.println(arr[i]);
}
}
}
package com.runa.define;
public class ArrayDemo4 {
public static void main(String[] args) {
int[] arr = {16, 26, 36, 6, 100};
// 初始化
int money = 0;
for (int i = 0; i < arr.length; i++) {
money += arr[i];
}
System.out.println("总销售额为:" + money);
}
}
package com.runa.define;
public class ArrayDemo5 {
public static void main(String[] args) {
// 动态初始化数组
int[] age = new int[3];
// 查看默认值,都是0
System.out.println(age[0]);
System.out.println(age[1]);
System.out.println(age[2]);
// 赋值
age[0] = 12;
age[1] = 13;
age[2] = 11;
System.out.println(age[0]);
System.out.println(age[1]);
System.out.println(age[2]);
}
}
package com.runa.define;
import java.util.Scanner;
public class ArrayDemo6 {
public static void main(String[] args) {
// 1 定义一个动态数组
double[] scores = new double[6];
Scanner sc = new Scanner(System.in);
// 2 循环输入6个评委分数存储在数组
for (int i = 0; i < scores.length; i++) {
System.out.println("请您输入第 " + (i + 1) + " 个评委的分数:");
double score =sc.nextDouble();
scores[i] = score;
}
// 3 遍历数据求和之后再平均
double sum = 0;
for (int i = 0; i < scores.length; i++) {
sum += scores[i];
}
double avg = sum/scores.length;
System.out.println(" 选手最终得分值为:" + avg);
}
}
package com.runa.memory;
public class ArrayDemo1 {
// 掌握普通变量,数组在计算机当中原理,java在计算机的执行过程
public static void main(String[] args) {
int[] faceScores = {15,9000, 10000, 20000, 9500, -5};
int max = faceScores[0];
for (int i = 1; i < faceScores.length; i++) {
if(faceScores[i] > max){
max = faceScores[i];
}
}
System.out.println("颜值最高的是:" + max);
}
}
package com.runa.memory;
public class ArrayDemo2 {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40,50};
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
System.out.println("-----------------------");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
package com.runa.memory;
import java.util.Random;
import java.util.Scanner;
public class ArrayDemo3 {
public static void main(String[] args) {
int[] codes = new int[5];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < codes.length; i++) {
System.out.println("请输入第 " + (i + 1) + " 个员工的工号");
int code = sc.nextInt();
codes[i] = code;
}
// 打乱数组中的元素顺序
Random r = new Random();
for (int i = 0; i < codes.length; i++) {
// 产生随机索引
int index = r.nextInt(codes.length); // 0-4
// 定义一个临时变量temp
int temp = codes[index];
// 把i位置的值赋值给temp
codes[index] = codes[i];
codes[i] = temp;
}
for (int i = 0; i < codes.length; i++) {
System.out.print("员工的随机排序是:" + codes[i] + " ");
}
}
}