找出陣列最小值
1 /** 2 * 3 * 首先創建一個長度是5的陣列 4 然后給陣列的每一位賦予隨機整數 5 通過for回圈,遍歷陣列,找出最小的一個值出來 6 * @author 李勇 7 * 8 */ 9 public class ForDemo3 { 10 11 public static void main(String[] args) { 12 13 int[] arr = new int[5]; 14 15 Random random = new Random(); 16 int r = random.nextInt(100+1); 17 18 arr[0] = random.nextInt(100+1); 19 arr[1] = random.nextInt(100+1); 20 arr[2] = random.nextInt(100+1); 21 arr[3] = random.nextInt(100+1); 22 arr[4] = random.nextInt(100+1); 23 24 int min = arr[0]; 25 System.out.println("陣列中的元素有:"); 26 for(int i = 0;i<arr.length;i++) { 27 System.out.println(arr[i]+""); 28 29 if(min>arr[i]) { 30 min = arr[i]; 31 32 } 33 34 } 35 System.out.println("陣列中最小的值是"+min); 36 37 } 38 }
實作陣列反轉
第一種方式:
1 package control.flow; 2 3 import java.util.Random; 4 5 /** 6 * 7 * 實作陣列的反轉 8 * @author 李勇 9 * 10 */ 11 public class ForDemo4 { 12 13 public static void main(String[] args) { 14 15 int[] arr = new int[5]; 16 17 Random random = new Random(); 18 19 arr[0] = random.nextInt(100+1); 20 arr[1] = random.nextInt(100+1); 21 arr[2] = random.nextInt(100+1); 22 arr[3] = random.nextInt(100+1); 23 arr[4] = random.nextInt(100+1); 24 25 /* 26 * 99 45 34 23 14 反轉之前 27 * 28 * 14 23 34 45 99反轉之后 29 * 30 */ 31 32 33 System.out.println("陣列反轉之前的效果是:"); 34 35 System.out.println(arr[0]); 36 System.out.println(arr[1]); 37 System.out.println(arr[2]); 38 System.out.println(arr[3]); 39 System.out.println(arr[4]); 40 41 for(int min = 0,max = arr.length-1;min<=max;min++,max--) { 42 43 int temp = arr[min]; 44 arr[min] = arr[max]; 45 arr[max] = temp; 46 47 } 48 49 50 51 System.out.println("------------------"); 52 53 System.out.println("陣列反轉后的效果:"); 54 for (int i = 0; i < arr.length; i++) { 55 System.out.println(arr[i]+""); 56 } 57 58 } 59 }
第二種方式:使用另外一個陣列接收完成陣列的反轉
1 package control.flow; 2 3 import java.util.Random; 4 5 6 /** 7 * 8 * 首先創建一個長度是5的陣列 9 然后給陣列的每一位賦予隨機整數 10 通過for回圈,遍歷陣列,找出最小的一個值出來 11 * @author 李勇 12 * 13 */ 14 public class ForDemo5 { 15 16 17 18 public static void main(String[] args) { 19 20 int[] arr = new int[5]; 21 Random random = new Random(); 22 23 arr[0] = random.nextInt(100+1); 24 arr[1] = random.nextInt(100+1); 25 arr[2] = random.nextInt(100+1); 26 arr[3] = random.nextInt(100+1); 27 arr[4] = random.nextInt(100+1); 28 29 System.out.println("陣列元素反轉之前的效果:"+""); 30 for (int i = 0; i < arr.length; i++) { 31 System.out.println(arr[i]+""); 32 33 } 34 System.out.println("==================="); 35 /** 36 * 使用第二個陣列實作元素交換 37 */ 38 39 int[] arr2 = new int[arr.length]; 40 for (int i = 0; i < arr2.length; i++) { 41 arr2[i] = arr[arr2.length-1-i];// 42 43 /** 44 * 含義:陣列長度為5,arr2.length-1代表陣列最大索引,陣列角標是從0開始的,那么最大索引就是4, 45 * arr2.length-1-i就代表4-0,因為i=0代表陣列最小索引,使用arr2[i]接收最大索引,那么就是arr2第0個索引等于arr的最大索引,也就是第四個索引,以此類推 46 * 一直向下減4-0 = 4,將最大索引賦值arr2第0個索引, 4-1 =3,將3索引的值賦值給arr2的第1個索引,以此類推 : 47 * 48 * int[] arr = new int[]{4,6,3,7,1} 49 * int[] arr2 = new int[]{1,7,3,6,4} 50 * 51 * @param args 52 */ 53 54 55 56 } 57 58 System.out.println("陣列元素反轉后的效果:"+""); 59 for (int i = 0; i < arr.length; i++) { 60 System.out.println(arr2[i]+""); 61 62 } 63 64 65 66 67 68 69 70 } 71 72 73 }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/181809.html
標籤:Java
上一篇:第六章第三十六題(幾何:正多邊形的面積)(Geometry: area of a regular polygon) - 編程練習題答案
