标签:print move output pen code port cep numbers amp
(1)数组排序及插入元素
1 package JavaEE.JavaBaseExampleTest.Array; 2 3 import java.util.Arrays; 4 /** 5 * 使用sort()方法对Java数组进行排序,及使用 insertElement () 方法向数组插入元素 6 * 定义了 printArray() 方法来打印数组 7 */ 8 public class ArrayAddAndGetLength { 9 public static void main(String[] args) throws Exception { 10 int[] array = {2, 5, -2, 6, -3, 8, 0, -7, -9, 4}; 11 Arrays.sort(array); 12 printArray("数组排序:", array); 13 int index = Arrays.binarySearch(array, 1); 14 System.out.println("元素 1 所在位置(负数为不存在):" + index); 15 int newIndex = -index - 1; 16 array = insertElement(array, 1, newIndex); 17 printArray("数组添加元素 1", array); 18 19 getArrayLength(); 20 } 21 22 private static void printArray(String message, int[] array) { 23 System.out.println(message + "[length:" + array.length + "]"); 24 for (int i = 0; i < array.length; i++) { 25 if (i != 0) { 26 System.out.print(", "); 27 } 28 System.out.print(array[i]); 29 } 30 System.out.println(); 31 } 32 33 private static int[] insertElement(int[] original, int element, int index) { 34 int length = original.length; 35 int[] destination = new int[length + 1]; 36 System.arraycopy(original, 0, destination, 0, index); 37 destination[index] = element; 38 System.arraycopy(original, index, destination, index + 1, length - index); 39 return destination; 40 } 41 private static void getArrayLength(){ 42 String[][] data = new String[2][5]; 43 System.out.println("第一维数组长度:"+ data.length); 44 System.out.println("第二维数组长度:" + data[0].length); 45 } 46 }
(2)在数组初始化后对数组进行扩容
1 package JavaEE.JavaBaseExampleTest.Array; 2 3 /** 4 * 在数组初始化后对数组进行扩容 5 */ 6 public class ArrayBecomeBig { 7 public static void main(String[] args){ 8 String[] names = new String[] {"a", "b", "c"}; 9 String[] extended = new String[5]; 10 extended[3] = "d"; 11 extended[4] = "e"; 12 System.arraycopy(names, 0, extended, 0, names.length); 13 for (String str:extended){ 14 System.out.println(str); 15 } 16 } 17 }
(3)删除数组元素
1 package JavaEE.JavaBaseExampleTest.Array; 2 3 import java.util.ArrayList; 4 5 /** 6 * 使用 remove () 方法来删除数组元素 7 */ 8 public class ArrayDeleteFactor { 9 public static void main(String[] args){ 10 ArrayList objArray = new ArrayList(); 11 objArray.clear(); 12 objArray.add(0, "第0个元素"); 13 objArray.add(1, "第1个元素"); 14 objArray.add(2, "第2个元素"); 15 System.out.println("删除前:" + objArray); 16 objArray.remove(1); 17 System.out.println("删除后:" + objArray); 18 } 19 }
(4)查找数组的最值
1 package JavaEE.JavaBaseExampleTest.Array; 2 3 import java.util.*; 4 5 /** 6 * 通过 Collection 类的 Collection.max() 和 Collection.min() 方法来查找数组中的最大和最小值 7 */ 8 public class ArrayGetMinAndMax { 9 public static void main(String[] args){ 10 Integer[] numbers = {8, 5, 6, 9, 10, 0, -2, 4}; 11 int min = Collections.min(Arrays.asList(numbers)); 12 int max = Collections.max(Arrays.asList(numbers)); 13 System.out.println("最小值:" + min); 14 System.out.println("最大值:" + max); 15 } 16 }
(5)判断数组是否相等
1 package JavaEE.JavaBaseExampleTest.Array; 2 3 import java.util.Arrays; 4 5 /** 6 * 使用 equals ()方法来判断数组是否相等 7 */ 8 public class ArrayIfSame { 9 public static void main(String[] args){ 10 int[] arr1 = {1, 2, 3, 4 ,5}; 11 int[] arr2 = {1, 2, 3, 4 ,5}; 12 int[] arr3 = {1, 2, 3}; 13 System.out.println("数组 arr1 是否与数组 arr2 相等? : " + Arrays.equals(arr1, arr2)); 14 System.out.println("数组 arr2 是否与数组 arr3 相等? : " + Arrays.equals(arr2, arr3)); 15 16 } 17 }
(6)合并数组
1 package JavaEE.JavaBaseExampleTest.Array; 2 3 import java.util.Arrays; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 /** 8 * 通过 List 类的 Arrays.toString () 方法和 List 类的 list.Addall(array1.asList(array2) 方法将两个数组合并为一个数组 9 */ 10 public class ArrayMixAndInPut { 11 public static void main(String[] args){ 12 ArrayMix(); 13 ArrayInput(); 14 } 15 16 private static void ArrayMix(){ 17 String[] a = {"A", "E", "I"}; 18 String[] b = {"O", "U"}; 19 List list = new ArrayList(Arrays.asList(a)); 20 list.addAll(Arrays.asList(b)); 21 Object[] c = list.toArray(); 22 System.out.println(Arrays.toString(c)); 23 } 24 //通过 Java Util 类的 Array.fill(arrayname,value) 方法和 25 // Array.fill(arrayname ,starting index ,ending index ,value) 方法向数组中填充元素 26 private static void ArrayInput(){ 27 int[] array = new int[6]; 28 Arrays.fill(array, 100); 29 for (int i = 0, n = array.length; i < n; i++){ 30 System.out.println(array[i]); 31 } 32 System.out.println(); 33 Arrays.fill(array, 3, 6, 50); 34 for (int i = 0, n = array.length; i < n; i++){ 35 System.out.println(array[i]); 36 } 37 } 38 }
(7)反转数组
1 package JavaEE.JavaBaseExampleTest.Array; 2 3 import java.util.*; 4 5 /** 6 * 使用 Collections.reverse(ArrayList) 将数组进行反转 7 */ 8 public class ArrayReverseAndOutput { 9 public static void main(String[] args){ 10 ArrayReverse(); 11 ArrayOutput(); 12 } 13 14 private static void ArrayReverse(){ 15 ArrayList arrayList = new ArrayList(); 16 arrayList.add("A"); 17 arrayList.add("B"); 18 arrayList.add("C"); 19 arrayList.add("D"); 20 System.out.println("反转前数组:" + arrayList); 21 Collections.reverse(arrayList); 22 System.out.println("反转后数组:" + arrayList); 23 } 24 25 private static void ArrayOutput(){ 26 String[] StrArray = new String[3]; 27 StrArray[0] = "hello"; 28 StrArray[1] = "java"; 29 StrArray[2] = "!"; 30 for (String data:StrArray){ 31 System.out.println(data); 32 } 33 } 34 35 }
(8)查找数组中元素
1 package JavaEE.JavaBaseExampleTest.Array; 2 3 import java.util.Arrays; 4 5 /** 6 * 1 使用sort()方法对Java数组进行排序 7 * 2 使用 binarySearch() 方法来查找数组中的元素 8 */ 9 public class ArraySearchAndSort { 10 public static void main(String[] args){ 11 SortAndSearch(); 12 } 13 private static void SortAndSearch(){ 14 int[] array = {2, 5, -2, 6, -3, 8, 0, -7, -9, 4}; 15 Arrays.sort(array); 16 printArray("数组的排序结果为", array); 17 int index = Arrays.binarySearch(array, 2); 18 System.out.println("元素 2 在第 [" + index + "] 个位置"); 19 } 20 private static void printArray(String message, int[] array){ 21 System.out.println(message + ":[length : " + array.length + "]"); 22 for (int i = 0; i < array.length; i++){ 23 if (i != 0){ 24 System.out.print(","); 25 } 26 System.out.print(array[i]); 27 } 28 System.out.println(); 29 } 30 }
1 package JavaEE.JavaBaseExampleTest.Array; 2 3 import java.util.ArrayList; 4 5 /** 6 * 使用 contains () 方法来查找数组中的指定元素 7 */ 8 public class ArraySearchFactor { 9 public static void main(String[] args){ 10 ArrayList objArray1 = new ArrayList(); 11 ArrayList objArray2 = new ArrayList(); 12 objArray2.add(0, "common1"); 13 objArray2.add(1, "common2"); 14 objArray2.add(2, "common3"); 15 objArray1.add(0, "common1"); 16 objArray1.add(1, "common2"); 17 System.out.println("数组objArray1元素:" + objArray1); 18 System.out.println("数组objArray2元素:" + objArray2); 19 System.out.println("数组 objArray1 是否包含元素 common2 ? : " + objArray1.contains("common2")); 20 System.out.println("数组 objArray2 是否包含数组 objArray1 ? : " + objArray2.contains(objArray1)); 21 } 22 }
(9)计算数组的交集、并集、差集
1 package JavaEE.JavaBaseExampleTest.Array; 2 3 import java.util.ArrayList; 4 import java.util.HashSet; 5 import java.util.Set; 6 7 public class ArraySubtractBothAnd extends Exception{ 8 public static void main(String[] args) { 9 ArrayList objArray1 = new ArrayList(); 10 ArrayList objArray2 = new ArrayList(); 11 objArray2.add(0, "common1"); 12 objArray2.add(1, "common2"); 13 objArray2.add(2, "common3"); 14 objArray2.add(3, "common5"); 15 objArray1.add(0, "common1"); 16 objArray1.add(1, "common2"); 17 objArray1.add(3, "common3"); 18 objArray1.add(2, "common4"); 19 System.out.println("array1元素:" + objArray1); 20 System.out.println("array2元素:" + objArray2); 21 // 使用 removeAll () 方法来计算两个数组的差集 22 objArray1.removeAll(objArray2); 23 System.out.println("array1 和 array2 的差集为:" + objArray1); 24 // 使用 retainAll () 方法来计算两数组的交集 25 objArray1.retainAll(objArray2); 26 System.out.println("array1 和 array2 的交集为:" + objArray1); 27 BothArray(); 28 } 29 30 // 使用 union ()方法来计算两数组的并集 31 private static void BothArray(){ 32 String[] arr1 = {"1", "2", "3"}; 33 String[] arr2 = {"4", "5", "6"}; 34 String[] resule_union = union(arr1, arr2); 35 System.out.println("并集结果如下:"); 36 for (String str:resule_union){ 37 System.out.println(str); 38 } 39 } 40 // 求两个字符串数组的并集,利用set的元素唯一性 41 private static String[] union(String[] arr1, String[] arr2){ 42 Set<String> set = new HashSet<String>(); 43 for (String str:arr1){set.add(str);} 44 for (String str:arr2){set.add(str);} 45 String[] result = { }; 46 return set.toArray(result); 47 } 48 }
标签:print move output pen code port cep numbers amp
原文地址:https://www.cnblogs.com/skygrass0531/p/12299194.html