二维数组案例
案例1:定义一个函数,获取某个数组中的最小值
package com.qf.work;
public class Work {
public static void main(String[] args) {
int[] arr = {4,6,5,1,8,9,7,3};
int min = getMin(arr);
System.out.println("最小值为:" + min);
}
public static int getMin(int[] arr) {
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
int element = arr[i];
if(min > element){
min = element;
}
}
return min;
}
}
案例二:定义一个数组,数组成员10个,找出数组中最大数连同下标一起输出
package com.qf.work;
public class Work {
public static void main(String[] args) {
int[] arr = {4,6,5,10,1,8,9,7,3,2};
int max = arr[0];
int maxIndex = 0;
for (int i = 1; i < arr.length; i++) {
int element = arr[i];
if(max < element){
max = element;
maxIndex = i;
}
}
System.out.println("最大值为:" + max);
System.out.println("最大值的下标为:" + maxIndex);
}
}
案例三:B哥去参加青年歌手大奖赛,有10个评委打分,(去掉一个最高一个最低)求平均分?
package com.qf.work;
import java.util.Arrays;
public class Work {
public static void main(String[] args) {
int[] arr = {4,6,5,10,1,8,9,7,3,2};
Arrays.sort(arr);
int sum = 0;
for (int i = 1; i < arr.length-1; i++) {
sum += arr[i];
}
double avg = sum/8.0;
System.out.println("平均分为:" + avg);
}
}
案例四:定义数组,存放5个学生的成绩【成绩值自己设定】,将成绩从大到小排序,获得成绩之和,平均成绩,最小成绩,最大成绩。
package com.qf.work;
public class Work {
public static void main(String[] args)
int[] arr = {8,9,7,3,2};
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]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
int max = arr[0];
int min = arr[arr.length-1];
int sum = 0;
for (int element : arr) {
sum += element;
}
double avg = sum/5.0;
System.out.println("成绩之和:" + sum);
System.out.println("平均分为:" + avg);
System.out.println("最大值为:" + max);
System.out.println("最小值为:" + min);
}
}
案例五:提取一个方法,将指定数组中的数组元素进行反转 ,例如:{10,23,2,45,6}—>{6,45,2,23,10}
//方法一
package com.qf.work;
import java.util.Arrays;
public class Work {
public static void main(String[] args) {
int[] arr = {10,23,2,45,6};
reverse(arr);
System.out.println(Arrays.toString(arr));
}
public static void reverse(int[] arr){
int start = 0;
int end = arr.length-1;
while(start < end){
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
}
//方法二
package com.qf.work;
import java.util.Arrays;
public class Work {
public static void main(String[] args) {
int[] arr = {10,23,2,45,6};
int[] reverse = reverse(arr);
System.out.println(Arrays.toString(reverse));
}
public static int[] reverse(int[] arr){
int[] newArr = new int[arr.length];
int index = newArr.length-1;
for (int element : arr) {
newArr[index--] = element;
}
return newArr;
}
}
案例六:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组
package com.qf.work;
import java.util.Arrays;
public class Work {
public static void main(String[] args) {
int[] arr = {3,5,65,1,32};
//找到最大值及下标
int max = arr[0];
int maxIndex = 0;
for (int i = 1; i < arr.length; i++) {
if(max < arr[i]){
max = arr[i];
maxIndex = i;
}
}
//最大的与第一个元素交换
arr[maxIndex] = arr[0];
arr[0] = max;
//找到最大值及下标
int min = arr[0];
int minIndex = 0;
for (int i = 1; i < arr.length; i++) {
if(min > arr[i]){
min = arr[i];
minIndex = i;
}
}
//最小的与最后一个元素交换
arr[minIndex] = arr[arr.length-1];
arr[arr.length-1] = min;
System.out.println(Arrays.toString(arr));
}
}