- /*
- 数组的最值
- */
- class Demo_Array1 {
- public static void main(String[] args) {
- int[] arr = {12,45,36,78,52,14,12};
- int max = getArrayMax(arr);
- System.out.println("数组最大值为"+max);
- int min = getArrayMin(arr);
- System.out.println("数组最小值为"+min);
- }
- public static int getArrayMax(int[] arr ) {
- int max = arr[0];
- for (int x = 1; x < arr.length; x++) {
- if (max < arr[x]) {
- max = arr[x];
- }
- }
- return max;
- }
- public static int getArrayMin(int[] arr ) {
- int min = arr[0];
- for (int x = 1; x < arr.length; x++) {
- if (min > arr[x]) {
- min = arr[x];
- }
- }
- return min;
- }
- }
- /*
- 逆序
- */
- class Demo_Array2 {
- public static void main(String[] args) {
- int[] arr = {12,45,36,78,52,14,13};
- System.out.println("逆序前:");
- print(arr);
- reverseArray(arr);
- System.out.println("逆序后:");
- print(arr);
- }
- public static void reverseArray(int[] arr) {
- for (int i = 0;i < arr.length/2 ;i++ ) {
- int temp = arr[i];
- arr[i] = arr[arr.length-1-i];
- arr[arr.length-1-i]=temp;
- }
- }
- public static void print(int[] arr ) {
- System.out.print("[");
- for(int i =0;i < arr.length;i++){
- if(i!=arr.length-1) {
- System.out.print(arr[i]+",");
- }else {
- System.out.println(arr[i]+"]");
- }
- }
- }
- }
- /*
- 查表法
- */
- import java.util.Scanner;
- class Demo_Array3{
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入1~7的整数");
- int x = sc.nextInt();
- if (x < 1 || x > 7) {
- System.out.println("我叶良辰不是你惹得起的人,你敢糊弄我?");
- }else {
- char week = getWeek(x);
- System.out.println(x+"是星期"+week);
- }
- }
- public static char getWeek(int x) {
- char[] arr = {' ','一','二','三','四','五','六','日'};
- return arr[x];
- }
- }
- /*
- 数组元素查找
- */
- import java.util.Scanner;
- class Demo_Array4 {
- public static void main(String[] args) {
- int[] arr = {12,45,36,78,52,14,13};
- print(arr);
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入要查找的数组元素");
- int x = sc.nextInt();
- int index = getIndex(arr,x);
- System.out.println(x+"的角标为"+index);
- }
- public static void print(int[] arr ) {
- System.out.print("[");
- for(int i =0;i < arr.length;i++){
- if(i!=arr.length-1) {
- System.out.print(arr[i]+",");
- }else {
- System.out.println(arr[i]+"]");
- }
- }
- }
- public static int getIndex(int[] arr, int x) {
- for (int i = 0;i < arr.length ;i++ ) {
- if (x == arr[i]) {
- return i;
- }
- }
- return -1;
- }
- }
- /*
- 冒泡排序
- */
- class ArrayBubble {
- public static void main(String[] args) {
- int[] arr = new int []{12,56,3,98,54,456,29};
- System.out.println("排序前:");
- printArray(arr);
- bubbleArray(arr);
- System.out.println("排序后:");
- printArray(arr);
- }
- public static void printArray(int [] arr)
- {
- System.out.print("[");
- for(int x=0;x<arr.length;x++)
- {
- if(x!=arr.length-1)
- System.out.print(arr[x]+",");
- else
- System.out.println(arr[x]+"]");
- }
- }
- public static void bubbleArray(int[] arr) {
- for (int i = 0;i < arr.length-1 ;i++ ) {
- for (int j = 0; j < arr.length-i-1 ;j++ ) {
- if(arr[j]>arr[j+1])
- {
- swap(arr,j,j+1);
- /*int temp = arr[j];
- arr[j] = arr[j+1];
- arr[j+1] = temp;*/
- }
- }
- }
- }
- public static void swap(int[] arr,int a,int b) {
- int temp = arr[a];
- arr[a] = arr[b];
- arr[b] = temp;
- }
- }
复制代码
|