数组小Demo,
分享于 点击 45086 次 点评:170
数组小Demo,
找出数组最小值
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 }
相关文章
- 暂无相关文章
用户点评