数组练习
public static void main(String[] args) {
exercise_01();
exercise_02();
exercise_03();
exercise_04();
exercise_05();
exercise_06();
exercise_07();
exercise_08();
exercise_09();
exercise_10();
exercise_11();
exercise_12();
}
public static void exercise_01(){
int[] arr = {124,532,52,64,5,547,43,43,747};
System.out.println();
System.out.println("----------------------------1---------------------------");
int sum = 0;
int max = arr[0];
int min = arr[0];
for (int i : arr) {
sum += i;
if (max < i) {
max = i;
}
if (min > i) {
min = i;
}
}
System.out.println("数组和是:"+sum);
System.out.println("数组中的最大值:"+max);
System.out.println("数组中的最小值:"+min);
}
public static void exercise_02(){
int[] arr = {34,63,25,76,22,54,65,22};
int key = 9;
System.out.println();
System.out.println("----------------------------2---------------------------");
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
System.out.println(key+"第一次下标是:"+i);
break;
}
if (i == arr.length-1) {
System.out.println("数组中没有该数值!");
}
}
}
public static void exercise_03(){
int[] arr = {212,352,535,658,321,972,221,58,685,452};
int max = arr[0];
int index = 0;
System.out.println();
System.out.println("----------------------------3---------------------------");
for (int i = 0; i < arr.length; i++) {
if (max < arr[i]) {
max = arr[i];
index = i;
}
}
System.out.println("数组最大值:"+max+" 下标是:"+index);
}
public static void exercise_04(){
int[] arr = {7,4,8,9,10,7,6,9,10,8};
int max = arr[0];
int min = arr[0];
int sum = 0;
System.out.println();
System.out.println("----------------------------4---------------------------");
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
if (max < arr[i]) {
max = arr[i];
}
if (min > arr[i]) {
min = arr[i];
}
}
System.out.println("平均分数:");
System.out.println((sum-max-min)/10);
}
public static void exercise_05(){
int[] arr = {96,9,1,9,6,66};
int min = arr[0];
System.out.println();
System.out.println("----------------------------5---------------------------");
for (int i : arr) {
if (min > i) {
min = i;
}
}
System.out.println("数组中最小值:"+min);
}
public static void exercise_06(){
int[] arr = {1,9,96,9,6,66};
int product = 1;
System.out.println();
System.out.println("----------------------------6---------------------------");
for (int i : arr) {
product *= i;
}
System.out.println("数组的乘积:"+product);
}
public static void exercise_07(){
int[] arr = {1,2,3,4,5,6,7,8,9,11,12};
int sum = 0;
System.out.println();
System.out.println("----------------------------7---------------------------");
for (int i : arr) {
if (i % 2 != 0) {
sum += i;
}
}
System.out.println(sum);
}
public static void exercise_08(){
char[] c = {'a','f','j','j','a','e','g'};
int count = 0;
char key = 'a';
System.out.println();
System.out.println("----------------------------8---------------------------");
for (char d : c) {
if (key == d) {
count++;
}
}
System.out.println(key+" 在数组中出现了:"+count);
}
static public void exercise_09(){
int[] arr = {1,9,96,9,6,66};
int[] newArr = new int[4];
System.out.println();
System.out.println("----------------------------9---------------------------");
for (int i = 0; i < newArr.length; i++) {
newArr[i] = arr[i+1];
System.out.print(newArr[i]+" ");
}
}
public static void exercise_10(){
int[] arr = {23,43,65,77,13,32,5,7,45};
System.out.println();
System.out.println("----------------------------10---------------------------");
for (int i = 0; i < arr.length/2; i++) {
int tmp = arr[i];
arr[i] = arr[arr.length-1-i];
arr[arr.length-1-i] = tmp;
}
for (int i : arr) {
System.out.println(i);
}
System.out.println();
}
public static void exercise_11(){
int[] oldArr = {1,3,4,5,6,6,0,5,6,7,0,5,34,46,0,4};
int zero = 0;
System.out.println();
System.out.println("----------------------------11---------------------------");
for (int i = 0; i < oldArr.length; i++) {
if (oldArr[i] == 0) {
zero++;
}
}
int[] newArr = new int[oldArr.length-zero];
int j = 0;
for (int i = 0; i < oldArr.length; i++) {
if (oldArr[i] != 0) {
newArr[j] = oldArr[i];
j++;
}
}
System.out.println("新数组:");
for (int i : newArr) {
System.out.print(i+" ");
}
}
public static void exercise_12(){
System.out.println();
System.out.println("----------------------------12---------------------------");
int[] arr1 = {1,7,9,11,13,15,17,19};
int[] arr2 = {2,4,6,8,10};
int[] arr = new int[arr1.length+arr2.length];
int tmp = 0;
for (int i = 0; i < arr1.length; i++) {
arr[i] = arr1[i];
}
for (int i = 0; i < arr2.length; i++) {
arr[arr1.length+i] = arr2[i];
}
System.out.println("数组合并:");
for (int i : arr) {
System.out.print(i+" ");
}
System.out.println();
System.out.println("排序:");
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]) {
tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
for (int i : arr) {
System.out.print(i+" ");
}
}