路漫漫其修遠兮,吾將上下而求索。
package com.mgk.array;
import java.util.*;
public class ArrayOptions {
public static void main(String[] args) {
int[] array = {2,5,-2,6,-3,8,0,-7,-9,4};
Arrays.sort(array);
printArray("數(shù)組排序結果為:", array);
int index = Arrays.binarySearch(array, 7);
int index2 = Arrays.binarySearch(array, 2);
//binarySearch 用二分查找算法在給定數(shù)組中搜索給定值的對象,數(shù)組在調用前必須排序好的,如果查找值包含
//在數(shù)組中,則返回搜索鍵的索引(排序后的索引位置),否則返回(-(插入點)-1),即(-()-1)
// 插入點是在排序數(shù)組中的插入位置,不是下標位置
System.out.println("元素 10 在第" + index + "個位置"); // -10
System.out.println("元素 2 在第" + index2 + "個位置"); // 5
// 數(shù)組的長度
printLength(array); // 10
String[][] data = new String[2][5];
System.out.println("data的長度:" + data.length);
System.out.println("二維數(shù)組的長度:" + data[0].length);
// 使用Collections.reverse(ArrayList) 將數(shù)組進行反轉
reverseArray();
// 使用for翻轉數(shù)組
reverse();
// 使用Collections.max() 獲取最大value
getArrayMax();
// 使用Collections.min() 獲取數(shù)組最小值
getArrayMin();
// 數(shù)組合并
arrayMerge();
// 數(shù)組填充
arrayFill();
// 查找數(shù)組中重復元素
int[] arr = {2,4,5,6,3,2,4,6,7,8,9};
findDupInArray(arr);
//刪除數(shù)組
delArrayValue();
// 數(shù)組差集
arrayDiff();
// 數(shù)組交集
arrayIns();
// 在數(shù)組中查找指定元素
searchElement();
// 判斷數(shù)組是否相等
equalsArray();
}
/**
* 打印數(shù)組
* @param message
* @param array
*/
private static void printArray(String message, int array[]) {
System.out.println((message + ":[lenght:" + array.length + "]"));
for(int i = 0; i < array.length; i++) {
if(i !=0 ) {
System.out.print(",");
}
System.out.print(array[i]);
}
System.out.println();
}
/**
* length數(shù)組的長度
* @param array
*/
private static void printLength(int[] array) {
System.out.println("數(shù)組的長度是:" + array.length);
}
/**
* 使用Collections.reverse(ArrayList) 反轉數(shù)組
*/
private static void reverseArray() {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("A");
arrayList.add("R");
arrayList.add("R");
arrayList.add("A");
arrayList.add("Y");
System.out.println("反轉前排序:" + arrayList); // 反轉前排序:[A, R, R, A, Y]
Collections.reverse(arrayList);
System.out.println("反轉后排序:" + arrayList); //反轉后排序:[Y, A, R, R, A]
}
/**
* 數(shù)組翻轉
*/
private static void reverse() {
int[] arr = {1,3,5,7,-3,-2,-1};
int[] arr1 = new int[arr.length];
System.out.println("翻轉后的數(shù)組:");
for(int i=0; i<arr.length; i++) {
arr1[i] = arr[arr.length-1-i];
System.out.print(arr1[i] + " "); //-1 -2 -3 7 5 3 1
}
}
/**
* 獲取最大值
*/
private static void getArrayMax() {
Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};
int max = Collections.max(Arrays.asList(numbers)); // arrays.asList() 將數(shù)組轉為集合
System.out.println("使用Collections.max() :");
System.out.println("最大值:" + max);
}
/**
* 獲取數(shù)組最小值
*/
private static void getArrayMin() {
Integer[] num = {3,44,6,32,61,1,34};
int min = Collections.min(Arrays.asList(num));
System.out.println("最小值:" + min);
}
/**
* 數(shù)組的合并
*/
private static void arrayMerge(){
String[] a = {"A", "R", "R"};
String[] b = {"A", "Y"};
List list = new ArrayList(Arrays.asList(a));
list.addAll(Arrays.asList(b));
Object[] c = list.toArray();
System.out.println("合并后的數(shù)組:" + Arrays.toString(c)); //合并后的數(shù)組:[A, R, R, A, Y]
}
/**
* 數(shù)組填充
* Arrays.fill(array, value);
* Arrays.fill(array, fromIndex, toIndex, value);
*/
private static void arrayFill() {
int[] array = new int[7];
Arrays.fill(array, 100); // 填充數(shù)組,每項值都為100
System.out.println("數(shù)組array的元素:");
for(int i=0; i<array.length; i++) {
System.out.println(array[i]);
}
System.out.println("數(shù)組的另一種填充方式:");
Arrays.fill(array, 3, 6, 66);
for(int i=0; i<array.length; i++) {
System.out.println(array[i]); // 100 100 100 66 66 66 100
}
}
/**
* 查找數(shù)組中相同的元素
* @param a
*/
public static void findDupInArray(int[] a) {
int count = 0;
for(int j=0; j<a.length; j++) {
for(int k=j+1; k<a.length; k++) {
if(a[j] == a[k]) {
count++;
}
}
if(count == 1) {
System.out.println("重復元素:" + a[j]); //2、4、6
}
count = 0;
}
}
/**
* 一、刪除數(shù)組中的元素:Java 的數(shù)組是固定長度的,無法直接刪除,
* 我們可以通過創(chuàng)建一個新數(shù)組,把原始數(shù)組中要保留的元素放到新數(shù)組中即可:
* 二、也可以使用 ArrayList 的 remove () 方法來刪除數(shù)組列表的元素:
*/
public static void delArrayValue() {
int[] oldArr = new int[] {2,3,4,5,6,7,8};
int num = 2; //要刪除的數(shù)組下標
int[] newArr = new int[oldArr.length - 1];
for(int i=0; i<newArr.length; i++) {
if(num < 0 || num >= oldArr.length) {
throw new RuntimeException("元素越界...");
}
if(i < num) {
newArr[i] = oldArr[i];
} else {
newArr[i] = oldArr[i+1];
}
}
System.out.println(Arrays.toString(oldArr)); // [2, 3, 4, 5, 6, 7, 8]
oldArr = newArr;
System.out.println(Arrays.toString(oldArr)); //[2, 3, 5, 6, 7, 8]
}
/**
* 使用removeall()方法來計算兩個數(shù)組的差集
*/
public static void arrayDiff() {
ArrayList objArray = new ArrayList();
ArrayList objArray2 = new ArrayList();
objArray2.add(0, "com1");
objArray2.add(1, "com2");
objArray2.add(2, "nocom1");
objArray2.add(3, "nocom2");
objArray.add(0, "com1");
objArray.add(1, "com2");
objArray.add(2,"nocom3");
System.out.println("objArray的元素:" + objArray); //[com1, com2, nocom3]
System.out.println("objArray2的元素:" + objArray2); // [com1, com2, nocom1, nocom2]
objArray.removeAll(objArray2);
System.out.println("objArray2與objArray數(shù)組的差集:" + objArray); // [nocom3]
}
/**
* 使用 retainAll () 方法來計算兩個數(shù)組的交集:
*/
public static void arrayIns() {
ArrayList arr1 = new ArrayList();
ArrayList arr2 = new ArrayList();
arr1.add(0,"com1");
arr1.add(1, "com2");
arr1.add(2, "notcom2");
arr2.add(0, "com1");
arr2.add(1, "com2");
arr2.add(2,"notcom");
arr2.add(3,"notcom1");
System.out.println("arr1的元素:" + arr1); // [com1, com2, notcom2]
System.out.println("arr2的元素:" + arr2); // [com1, com2, notcom, notcom1]
arr1.retainAll(arr2);
System.out.println("arr1與arr2的交集:" + arr1); // [com1, com2]
}
/**
* 使用contains()方法來查找數(shù)組中的指定元素
*/
public static void searchElement() {
ArrayList<String> arr1 = new ArrayList<String>();
ArrayList<String> arr2 = new ArrayList<String>();
arr1.add(0,"com1");
arr1.add(1, "com2");
arr1.add(2, "notcom2");
arr2.add(0, "com1");
arr2.add(1, "com2");
arr2.add(2,"notcom");
arr2.add(3,"notcom1");
System.out.println("arr1的元素:" + arr1); // [com1, com2, notcom2]
System.out.println("arr2的元素:" + arr2); // [com1, com2, notcom, notcom1]
System.out.println("arr1是否包含字符串 com2 ? : " + arr1.contains("com2")); // arr1是否包含字符串 com2 ? : true
System.out.println("arr2是否包含數(shù)組 arr1 ? :" + arr2.contains(arr1)); // arr2是否包含數(shù)組 arr1 ? :false
}
/**
* 使用Arrays.equals()判斷數(shù)組是否相等
*/
public static void equalsArray() {
int[] ary1 = {1,2,3,4,5,6};
int[] ary2 = {1,2,3,4,5,6};
int[] ary3 = {1,2,3,4};
System.out.println("數(shù)組ary1是否與數(shù)組 ary2 相等? :" + Arrays.equals(ary1, ary2)); // 數(shù)組ary1是否與數(shù)組 ary2 相等? :true
System.out.println("數(shù)組ary1是否與數(shù)組 ary3 相等?:" + Arrays.equals(ary1, ary3)); // 數(shù)組ary1是否與數(shù)組 ary3 相等?:false
}
}