4.1 元素排序/查找元素位置 Arrays.sort、Arrays.binarySearch

  1. public static void main(String args[]) throws Exception {
  2. int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };
  3. Arrays.sort(array);
  4. printArray("数组排序结果为", array); // -9, -7, -3, -2, 0, 2, 4, 5, 6, 8
  5. int index = Arrays.binarySearch(array, 2);
  6. System.out.println("元素 2 在第 " + index + " 个位置");
  7. }

4.2 获取最大值和最小值 Collections 工具类

  1. Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};
  2. int min = (int) Collections.min(Arrays.asList(numbers));
  3. int max = (int) Collections.max(Arrays.asList(numbers));

4.3 数组合并 Arrays.asList、addAll

  1. public class Main {
  2. public static void main(String args[]) {
  3. String a[] = { "A", "E", "I" };
  4. String b[] = { "O", "U" };
  5. List list = new ArrayList(Arrays.asList(a));
  6. list.addAll(Arrays.asList(b));
  7. Object[] c = list.toArray();
  8. System.out.println(Arrays.toString(c));
  9. }
  10. }

4.4 数组填充 Arrays.fill

  1. public class FillTest {
  2. public static void main(String args[]) {
  3. int array[] = new int[6];
  4. Arrays.fill(array, 100); // 100, 100, 100, 100, 100, 100
  5. for (int i=0, n=array.length; i < n; i++) {
  6. System.out.println(array[i]);
  7. }
  8. System.out.println();
  9. Arrays.fill(array, 3, 6, 50); // 100, 100, 100, 50, 50, 50
  10. for (int i=0, n=array.length; i< n; i++) {
  11. System.out.println(array[i]);
  12. }
  13. }
  14. }

4.5 数组扩容/复制 System.arraycopy

  1. String[] names = new String[] { "A", "B", "C" };
  2. String[] extended = new String[5];
  3. System.arraycopy(names, 0, extended, 0, names.length); // A, B, C, NULL, NULL

4.6 数组交集 retainAll

  1. public class Main {
  2. public static void main(String[] args) {
  3. ArrayList objArray = new ArrayList(); ArrayList objArray2 = new ArrayList();
  4. objArray2.add(0,"common1"); objArray2.add(1,"common2"); objArray2.add(2,"notcommon");
  5. objArray2.add(3,"notcommon1"); objArray.add(0,"common1"); objArray.add(1,"common2"); objArray.add(2,"notcommon2");
  6. System.out.println("array1 数组元素:"+objArray); // [common1, common2, notcommon2]
  7. System.out.println("array2 数组元素:"+objArray2); // [common1, common2, notcommon, notcommon1]
  8. objArray.retainAll(objArray2);
  9. System.out.println("array2 & array1 数组交集为:"+objArray); // [common1, common2]
  10. }
  11. }

4.7 数组差集 removeAll

  1. public class Main {
  2. public static void main(String[] args) {
  3. ArrayList objArray = new ArrayList(); ArrayList objArray2 = new ArrayList();
  4. objArray2.add(0,"common1"); objArray2.add(1,"common2"); objArray2.add(2,"notcommon");
  5. objArray2.add(3,"notcommon1"); objArray.add(0,"common1"); objArray.add(1,"common2"); objArray.add(2,"notcommon2");
  6. System.out.println("array1 数组元素:"+objArray); // [common1, common2, notcommon2]
  7. System.out.println("array2 数组元素:"+objArray2); // [common1, common2, notcommon, notcommon1]
  8. objArray.removeAll(objArray2);
  9. System.out.println("array1 与 array2 数组差集为:"+objArray); // [notcommon2]
  10. }
  11. }

4.8 查找元素是否存在 contains

  1. ArrayList<String> objArray = new ArrayList<String>();
  2. objArray.add(0,"common1"); objArray.add(1,"common2");
  3. objArray.contains("common2"); // true

4.9 数组比较 Arrays.equals

  1. int[] ary = {1,2,3,4,5,6}; int[] ary1 = {1,2,3,4,5,6};
  2. Arrays.equals(ary, ary1); // true