package com.gooagoo.javautil;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class TestMain
{
public static void main(String[] args)
{
/****************************arrays****************************/
int[] arr = {1,3,2,6};
for(int iTemp:arr){
System.out.println(iTemp);
}
System.out.println("-------------------------");
int tarIndex2 = Arrays.binarySearch(arr, 2); // 数组无序,二分查找
System.out.println(tarIndex2); // wrong result index
System.out.println("-------------------------");
Arrays.sort(arr); // 数组排序
for(int jTemp:arr){
System.out.println(jTemp);
}
System.out.println("-------------------------");
int tarIndex = Arrays.binarySearch(arr, 2); // 数组有序,二分查找
System.out.println(tarIndex); // right result index
System.out.println("-------------------------");
String[] strArr = {"Ai","Jg","eo","ch"};
String[] strArr2 = new String[4];
Arrays.fill(strArr2, "l"); // fill
System.out.println(Arrays.toString(strArr2)); // toString
String[] copyStr1 = Arrays.copyOf(strArr, strArr.length); // copy
System.out.println(Arrays.toString(copyStr1));
System.out.println(Arrays.equals(strArr, copyStr1)); // equals before sort
Arrays.sort(copyStr1); // sort, 注意,默认排序是区分大小写的
System.out.println("默认(区分大小写)排序:大写在前,小写在后: " + Arrays.toString(copyStr1));
Arrays.sort(copyStr1,String.CASE_INSENSITIVE_ORDER);
System.out.println("忽略大小写排序: " +Arrays.toString(copyStr1)); // sort,忽略大小写
Arrays.sort(copyStr1,String.CASE_INSENSITIVE_ORDER);
Collections.reverse(Arrays.asList(copyStr1));
System.out.println("忽略大小写逆序排序: " +Arrays.toString(copyStr1)); // sort,忽略大小写逆序排序
System.out.println(Arrays.equals(strArr, copyStr1)); // equals,注意:元素顺序是否相同
String[] copyStr2 = Arrays.copyOfRange(strArr,2,strArr.length); // range copy
System.out.println(Arrays.toString(copyStr2));
System.out.println("------------------------");
/******************************collections*********************************/
List<Integer> iList = Arrays.asList(4,3,9,6);
Iterator<Integer> iIter = iList.iterator();
while(iIter.hasNext()){
System.out.println(iIter.next());
}
System.out.println("-------------------------");
int index = Collections.binarySearch(iList, 6); // 集合无序,二分查找
System.out.println(index);
System.out.println("-------------------------");
Collections.sort(iList); // 集合排序
for(Integer temp:iList){
System.out.println(temp);
}
System.out.println("-------------------------");
int index2 = Collections.binarySearch(iList, 6); // 集合有序,二分查找
System.out.println(index2);
/****************************arrays convert to collections*********************/
System.out.println("-------------------------");
List<String> strList = Arrays.asList(copyStr1);
System.out.println(strList.toString());
/***************************collections convert to arrays***********************/
System.out.println("-------------------------");
String[] resArr = (String[])strList.toArray();
System.out.println(Arrays.toString(resArr));
}
}
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class TestMain
{
public static void main(String[] args)
{
/****************************arrays****************************/
int[] arr = {1,3,2,6};
for(int iTemp:arr){
System.out.println(iTemp);
}
System.out.println("-------------------------");
int tarIndex2 = Arrays.binarySearch(arr, 2); // 数组无序,二分查找
System.out.println(tarIndex2); // wrong result index
System.out.println("-------------------------");
Arrays.sort(arr); // 数组排序
for(int jTemp:arr){
System.out.println(jTemp);
}
System.out.println("-------------------------");
int tarIndex = Arrays.binarySearch(arr, 2); // 数组有序,二分查找
System.out.println(tarIndex); // right result index
System.out.println("-------------------------");
String[] strArr = {"Ai","Jg","eo","ch"};
String[] strArr2 = new String[4];
Arrays.fill(strArr2, "l"); // fill
System.out.println(Arrays.toString(strArr2)); // toString
String[] copyStr1 = Arrays.copyOf(strArr, strArr.length); // copy
System.out.println(Arrays.toString(copyStr1));
System.out.println(Arrays.equals(strArr, copyStr1)); // equals before sort
Arrays.sort(copyStr1); // sort, 注意,默认排序是区分大小写的
System.out.println("默认(区分大小写)排序:大写在前,小写在后: " + Arrays.toString(copyStr1));
Arrays.sort(copyStr1,String.CASE_INSENSITIVE_ORDER);
System.out.println("忽略大小写排序: " +Arrays.toString(copyStr1)); // sort,忽略大小写
Arrays.sort(copyStr1,String.CASE_INSENSITIVE_ORDER);
Collections.reverse(Arrays.asList(copyStr1));
System.out.println("忽略大小写逆序排序: " +Arrays.toString(copyStr1)); // sort,忽略大小写逆序排序
System.out.println(Arrays.equals(strArr, copyStr1)); // equals,注意:元素顺序是否相同
String[] copyStr2 = Arrays.copyOfRange(strArr,2,strArr.length); // range copy
System.out.println(Arrays.toString(copyStr2));
System.out.println("------------------------");
/******************************collections*********************************/
List<Integer> iList = Arrays.asList(4,3,9,6);
Iterator<Integer> iIter = iList.iterator();
while(iIter.hasNext()){
System.out.println(iIter.next());
}
System.out.println("-------------------------");
int index = Collections.binarySearch(iList, 6); // 集合无序,二分查找
System.out.println(index);
System.out.println("-------------------------");
Collections.sort(iList); // 集合排序
for(Integer temp:iList){
System.out.println(temp);
}
System.out.println("-------------------------");
int index2 = Collections.binarySearch(iList, 6); // 集合有序,二分查找
System.out.println(index2);
/****************************arrays convert to collections*********************/
System.out.println("-------------------------");
List<String> strList = Arrays.asList(copyStr1);
System.out.println(strList.toString());
/***************************collections convert to arrays***********************/
System.out.println("-------------------------");
String[] resArr = (String[])strList.toArray();
System.out.println(Arrays.toString(resArr));
}
}