目录
一、一维数组
1.语法
数据类型 [ ] 数组名 = new 数据类型[ 个数 ]
String [] name = new String [10];
double [] salarys = new double [20];
int [] num = new int [5];
//左侧 栈内存里声明了名为num的整型变量
//右侧 堆内存里分配了5个整型大小的连续空间内存
//等号 将右侧空间的首地址赋值给了左侧
//数组名 数组名num就是后面空间的首地址
2.初始化(静态初始化——无需键盘录入)
格式 : 数据类型 [] 数组名 = {值1,值2,值3};
int [] year = {2020,2021};
int [] day = {30,29,28};
//注:声明并出示话数组不需要指定数组的大小
①创建数组后不能修改数组的大小
②基本数据类型数组的默认值为0
③char类型数组元素的默认值为\u0000
④布尔类型数组的默认值是false
3.初始化(动态初始化——键盘录入)
//实例1.输入5个学生成绩求和以及平均值
Scanner input = new Scanner(System.in);
final int N = 5; //N常量不可改变
double [] scores = new double [N];
double sum=0,avg; //avg平均值
System.out.print("请依次输入学生成绩:");
for(int i = 0;i<scores.length;i++) {
scores[i] = input.nextDouble();
sum += scores[i];
}
avg = sum / scores.length;
System.out.println("总成绩:"+sum+" 平均成绩:"+avg);
//实例2.打印斐波那契数列 0,1,1,2,3,5,8,13,21,34,55,89,144
Scanner input = new Scanner(System.in);
final int N = 15;
int [] Fibonacci = new int [N];
Fibonacci[0] = 0;
Fibonacci[1] = 1;
for(int i=2;i<Fibonacci.length;i++) {
//规律:从第三项开始,该项是前两项的和
Fibonacci[i] = Fibonacci[i-2] + Fibonacci[i-1];
}
for (int i = 0; i < Fibonacci.length; i++) {
System.out.print(Fibonacci[i]+" ");
}
4.冒泡排序
//1.一工会比较数组元素个数-1轮
//2.每一轮,比较的次数比上一轮少1
//3.如果前面一个数字大于/小于后面一个数字,那么交换
int [] BubbleSort = {55,66,99,77,33};
int temp;
for(int i = 0; i < BubbleSort.length - 1;i++) {
for(int j = 0 ;j < BubbleSort.length - i - 1; j++) {
if(BubbleSort[j] > BubbleSort[j + 1]) {
temp = BubbleSort[j];
BubbleSort[j] = BubbleSort[j + 1];
BubbleSort[j + 1] = temp;
}
}
}
System.out.print("排序结果为:");
for (int i = 0; i < BubbleSort.length; i++) {
System.out.print(BubbleSort[i]+" ");
}
5.选择排序
int [] SelectSort = {55,33,22,99,66,44};
for(int i = 0;i < SelectSort.length;i++){
int min = SelectSort[i];
int minIndex = i;
for (int j = i + 1; j < SelectSort.length; j++) {
if(min > SelectSort[j]) {
min = SelectSort[j];
minIndex = j;
}
}
int temp = SelectSort[i];
SelectSort[i] = SelectSort[minIndex];
SelectSort[minIndex] = temp;
}
System.out.print("排序结果为:");
for (int i = 0; i < SelectSort.length; i++) {
System.out.print(SelectSort[i]+" ");
}
6.系统自带排序
import java.util.Arrays; //注意导入包
public class OwnSortDemo {
public static void main(String[] args) {
char [ ] words = {'q','w','e','q','r','t','y','u','i'};
Arrays.sort(words);
System.out.print("默认排序结果为:");
for(int i = 0;i < words.length;i++) {
System.out.print(words[i]+" ");
}
System.out.println();
System.out.print("逆序排序结果为:");
for(int i = 0;i < words.length / 2; i++) { //只进行一半交换
char temp = words[i];
words[i] = words[words.length - i - 1];
words[words.length - i - 1] = temp;
}
for(int i = 0;i < words.length;i++) {
System.out.print(words[i]+" ");
}
}
}
7.数组元素的插入和删除
插入:①找到插入位置的下标
②从最后的元素开始,前面的元素向后一个元素覆盖
③在第i个赋值
④数组输出长度时+1
删除:①找到删除元素下表
②从第i个开始,后面覆盖前面
③数组长度输出时-1
二、二维数组
语法:
数组类型 [ ][ ] 数组名 = new 数组类型 [行值][列值];
String [] Name = {"张三","李四","王二麻子"};
String [] Subject = {"语文","数学","英语"};
final int Row = Name.length;
final int Col = Subject.length;
int [][] Score = new int [Row][Col];
Scanner input = new Scanner(System.in);
for(int i = 0 ; i < Row ; i++ ) {
for(int j = 0 ; j < Col ; j++ ) {
System.out.print(Name[i]+"的"+Subject[j]+"成绩是:");
Score[i][j] = input.nextInt();
}
}