Java数组的运算

数组的一些运算

  • 获取数组的最大值最小值及总和
    • 数组的顺序查找
    • 二分法查找
    • 冒泡排序
    • 直接选择排序
    • 数组的反转

获取数组的最大值最小值及总和

public class GetArrayMaxMinIndex {
	public static void main(String[] args) {
		int max = 0;
		int maxIndex = 0;
		int min = 0;
		int minIndex = 0;
		double sum = 0;
		double avg = 0;
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入数组的长度:");
		int length = sc.nextInt();
		int[] arr = new int [length];
		for(int i = 0;i < arr.length;i++){
			System.out.print("请输入数组的第" + (i+1) + "个元素:");
			arr[i] = sc.nextInt();
		}
		//遍历数组
		for(int i = 0;i < arr.length;i++){
			System.out.print(arr[i] + "\t");
		}
		System.out.println();
		max = arr[0];
		min = arr[0];
		for(int i = 0;i < arr.length;i++){
			if(arr[i] > max){
				max = arr[i];
				maxIndex = i;
			}
			if(arr[i] < min){
				min = arr[i];
				minIndex = i;
			}
			sum += arr[i];
		}
		avg = sum / arr.length;
		System.out.println("最大值:" + max + " 下标为:" + maxIndex);
		System.out.println("最小值:" + min + " 下标为:" + minIndex);
		System.out.println("数组的总和为:" + sum);
		System.out.println("数组的平均值为:" + avg);
		
	}
}

数组的顺序查找

import java.util.Scanner;

//顺序排序
public class InOrderSearchArray {
	public static void main(String[] args) {
		int index = -1;
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入数组的长度:");
		int length = sc.nextInt();
		int[] arr = new int [length];
		for(int i = 0;i < arr.length;i++){
			System.out.print("请输入数组的第" + (i+1) + "个元素:");
			arr[i] = sc.nextInt();
		}
		//遍历数组
		for(int i = 0;i < arr.length;i++){
			System.out.print(arr[i] + "\t");
		}
		System.out.println();
		System.out.println("请输入你要查找的数字:");
		int num = sc.nextInt();
		//查找数据
		for(int i = 0;i < arr.length;i++){
			if(num == arr[i]){
				index = i;
			}
		}
		if(index != -1){
			System.out.println(num + "下标为:" + index);
		}else{
			System.out.println("没有找到");
		}

	}
}

二分法查找

注意:数组一定是有序的

import java.util.Scanner;

//二分查找
public class BinarySearchArray {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入数组的长度:");
		int length = sc.nextInt();
		int[] arr = new int [length];
		for(int i = 0;i < arr.length;i++){
			System.out.print("请输入有序数组的第" + (i+1) + "个元素:");
			arr[i] = sc.nextInt();
		}
		int index = -1;
		int left = 0;
		int right = arr.length-1;
		int mid = (right + left) / 2;
		//遍历数组
		for(int i = 0;i < arr.length;i++){
			System.out.print(arr[i] + "\t");
		}
		System.out.println();
		System.out.println("请输入你要查找的数字:");
		int num = sc.nextInt();
		//查找数据
		while(true){
			if(right < left ){
				break;
			}
			if(num == arr[mid]){ 
				index = mid;
				break;
			}else if(num < arr[mid]){
				right = mid - 1;
				mid = (right + left) / 2;
			}else{
				left = mid + 1;
				mid = (right + left) / 2;
			}
		}
		if(index != -1){
			System.out.println("找到了" + num + "下标为:" + mid);
		}else{
			System.out.println("没有找到");
		}
		
	}
}

冒泡排序

import java.util.Scanner;

//冒泡排序
public class BubbleSortArray {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入数组的长度:");
		int length = sc.nextInt();
		int[] arr = new int [length];
		for(int i = 0;i < arr.length;i++){
			System.out.print("请输入数组的第" + (i+1) + "个元素:");
			arr[i] = sc.nextInt();
		}
		//遍历数组
		for(int i = 0;i < arr.length;i++){
			System.out.print(arr[i] + "\t");
		}
		System.out.println();
		System.out.println("冒泡排序后:");
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr.length-i-1; j++) {
				if(arr[j] > arr[j+1]){
					int temp = arr[j+1];
					arr[j+1] = arr[j];
					arr[j] = temp;
				}
			}
		}
		for(int i = 0;i < arr.length;i++){
			System.out.print(arr[i] + "\t");
		}
	}
}

直接选择排序

import java.util.Scanner;

//直接选择排序
public class SelectSortArray {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入数组的长度:");
		int length = sc.nextInt();
		int[] arr = new int [length];
		for(int i = 0;i < arr.length;i++){
			System.out.print("请输入数组的第" + (i+1) + "个元素:");
			arr[i] = sc.nextInt();
		}
		//遍历数组
		for(int i = 0;i < arr.length;i++){
			System.out.print(arr[i] + "\t");
		}
		System.out.println();
		System.out.println("直接排序后:");
		for (int i = 0; i < arr.length; i++) {
			int min = arr[i];
			int minIndex = i;
			for (int j = i; j < arr.length; j++) {
				if(arr[j] < min){
					min = arr[j];
					minIndex = j;
				}
			}
			if(minIndex != i){
				int temp = arr[i];
				arr[i] = arr[minIndex];
				arr[minIndex] = temp;
			}
		}
		for(int i = 0;i < arr.length;i++){
			System.out.print(arr[i] + "\t");
		}
	}
}

数组的反转

赋值法

//数组的反转:复制发
public class ArrayReverseCopy {
	public static void main(String[] args) {
		int[] arr = {12,33,44,55,62,75,22};
		int[] arrCopy = new int[arr.length];
		for(int i = 0;i < arr.length;i++){
			arrCopy[i] = arr[arr.length-i-1];
		}
		System.out.println("arr的数组:");
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + "\t");
		}
		System.out.println();
		System.out.println("arrCopy的数组:");
		
		for (int i = 0; i < arrCopy.length; i++) {
			System.out.print(arrCopy[i] + "\t");
		}
		
		
	}
}

交换法

public class ArrayReverseJiaoHuan {
	public static void main(String[] args) {
		int[] arr = {12,33,44,55,62,75,22};
		
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + "\t");
		}
		
		System.out.println();
		System.out.println("------------------------------------------");
		
		for (int i = 0; i < arr.length/2; i++) {
			int temp = arr[arr.length-1-i];
			arr[arr.length-1-i] = arr[i];
			arr[i] = temp;
		}
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + "\t");
		}
	}
}

你可能感兴趣的:(Java学习)