排序算法、Arrays工具类
遍历数组
public static void main(String[] args) {
int[] a = {8, 33, 24, 56, 12, 6};
int sum = 0;
for (int i : a) {
System.out.println(i);
sum += i;
}
System.out.println("sum=" + sum);
}
输入一个数判断是否在数组中存在
public static void main(String[] args) {
int[] a = {8, 33, 24, 56, 12, 6};
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字:");
int num = sc.nextInt();
boolean flag = false;
for (int i : a) {
if (num == i) {
flag = true;
break;
}
}
if (flag) {
System.out.println("你输入的数字在数组中存在!");
} else {
System.out.println("你输入的数字不在数组中!");
}
}
求数组最大值
public static void main(String[] args) {
int[] array = {3, 5, 10, 7, 9, 8};
int max = array[0];
for (int i = 1; i < array.length; i++) {
if (max < array[i]) {
max = array[i];
}
}
System.out.println(max);
}
数组降序排列
1、选择排序
int[] array = {3, 5, 10, 7, 9, 8};
// 选择排序
// i要固定的位置
for (int i = 0; i < array.length - 1; i++) {
// j是要比较的索引
for (int j = i + 1; j < array.length; j++) {
if (array[i] < array[j]) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
}
2、冒泡排序
public static void main(String[] args) {
int[] array = {3, 5, 10, 7, 9, 8};
// 冒泡排序
// 控制轮数
for (int i = 0; i < array.length - 1; i++) {
// 控制比较次数
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
int tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
}
}
}
for (int i : array) {
System.out.println(i);
}
}
向数组中插入一个数据
public static void main(String[] args) {
int[] scores = {67, 87, 33, 56, 99};
// 降序排序
for (int i = 0; i < scores.length - 1; i++) {
for (int j = 0; j < scores.length - 1 - i; j++) {
if (scores[j] < scores[j + 1]) {
int tmp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = tmp;
}
}
}
// 插入一个成绩
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个成绩:");
int score = sc.nextInt();
// 创建新数组
int[] newScores = new int[scores.length + 1];
// 拷贝数据
for (int i = 0; i < scores.length; i++) {
newScores[i] = scores[i];
}
// 插入数据
// {67, 87, 33, 56, 99} => {99, 87, 67, 56, 33, 0}
// score = 60;
for (int i = 0; i < newScores.length; i++) {
if (score >= newScores[i]) {
for (int j = newScores.length - 1; j > i ; j--) {
newScores[j] = newScores[j - 1];
}
newScores[i] = score;
break;
}
}
sc.close();
// 遍历数组
for (int s : newScores) {
System.out.println(s);
}
}
Arrays
1.数组(array)的工具类
2.常用方法
toString()
传入任意类型的数组,返回格式化字符串
sort()
传入一个任意类型数组,没有返回值,对原数组进行升序排序
copyOf()
将原数组拷贝到一个指定长度的新数组
fill()
使用指定数据填充整个数组
binarySearch()
进行二分查找,前提是数组有序,查询到返回索引,没查询到返回负数
public static void main(String[] args) {
int[] array1 = {1, 3, 6, 8, 0};
String str1 = Arrays.toString(array1);
System.out.println(str1); // [1, 3, 6, 8, 0]
Arrays.sort(array1);
System.out.println(Arrays.toString(array1));
int[] array2 = Arrays.copyOf(array1, 10);
System.out.println(Arrays.toString(array2));
Arrays.fill(array2, 666); // 使用666填充整个数组
System.out.println(Arrays.toString(array2));
// 二分查找,折半查找
// 1 3 5 9 11 23 56
// 查找11
// 取中间数9,11 > 9
// 11 23 56
// 取中间23,11 < 23
// 11
int result = Arrays.binarySearch(array1, 6);
System.out.println(result);
// 如果查找不到,返回值是负数
System.out.println(Arrays.binarySearch(array1, 2));
}