Basis of Collections and Arrays

Collections Tools:

Sort operation:

Reverse void (List List) // inversion
void shuffle (List List) // random ordering
void Sort (List List) // Sort Ascending natural ordering
void Sort (List List, Comparator C) // custom ordered from Comparator logic control sequencing
void the swap (List List, I int, int J) // swap elements of two index positions
void Rotate (List List, int Distance) // rotation. When the distance is positive, after List whole distance element to the front. When the distance is negative, the front distance elements integrally moved to the back of the list.

ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(-1);
arrayList.add(3);
arrayList.add(3);
arrayList.add(-5);
arrayList.add(7);
arrayList.add(4);
arrayList.add(-9);
arrayList.add(-7);
System.out.println("原始数组:");
System.out.println(arrayList);
// void reverse(List list):反转
Collections.reverse(arrayList);
System.out.println("Collections.reverse(arrayList):");
System.out.println(arrayList);


Collections.rotate(arrayList, 4);
System.out.println("Collections.rotate(arrayList, 4):");
System.out.println(arrayList);

// void sort(List list),按自然排序的升序排序
Collections.sort(arrayList);
System.out.println("Collections.sort(arrayList):");
System.out.println(arrayList);

// void shuffle(List list),随机排序
Collections.shuffle(arrayList);
System.out.println("Collections.shuffle(arrayList):");
System.out.println(arrayList);

// void swap(List list, int i , int j),交换两个索引位置的元素
Collections.swap(arrayList, 2, 5);
System.out.println("Collections.swap(arrayList, 2, 5):");
System.out.println(arrayList);

// 定制排序的用法
Collections.sort(arrayList, new Comparator<Integer>() {

  @Override
  public int compare(Integer o1, Integer o2) {
    return o2.compareTo(o1);
  }
});
System.out.println("定制排序后:");
System.out.println(arrayList);

Find and replace operations:

binarySearch int (List List, Object Key) // List of binary search, index returns, attention must be ordered List
int max (Collection Coll) // according to the natural order of the elements, returns the largest element. Min analogy int (Collection Coll)
int max (Coll Collection, Comparator C) // The custom ordering, returns the largest element, controlled by the collation Comparatator class. Analog int min (Coll Collection, Comparator C)
void Fill (List list, Object obj) // replace all the elements specified in the list with the specified elements.
Frequency int (Collection c, Object O) // count the number of occurrences of elements
int indexOfSubList (List list, List target) // statistical target index in the list of the first occurrence, or -1 if not found, the analogy int lastIndexOfSubList ( Source List, List target).
boolean replaceAll (List List, oldVal Object, Object newVal) // replace the old elements with new elements

ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(-1);
arrayList.add(3);
arrayList.add(3);
arrayList.add(-5);
arrayList.add(7);
arrayList.add(4);
arrayList.add(-9);
arrayList.add(-7);
ArrayList<Integer> arrayList2 = new ArrayList<Integer>();
arrayList2.add(-3);
arrayList2.add(-5);
arrayList2.add(7);
System.out.println("原始数组:");
System.out.println(arrayList);

System.out.println("Collections.max(arrayList):");
System.out.println(Collections.max(arrayList));

System.out.println("Collections.min(arrayList):");
System.out.println(Collections.min(arrayList));

System.out.println("Collections.replaceAll(arrayList, 3, -3):");
Collections.replaceAll(arrayList, 3, -3);
System.out.println(arrayList);

System.out.println("Collections.frequency(arrayList, -3):");
System.out.println(Collections.frequency(arrayList, -3));

System.out.println("Collections.indexOfSubList(arrayList, arrayList2):");
System.out.println(Collections.indexOfSubList(arrayList, arrayList2));

System.out.println("Collections.binarySearch(arrayList, 7):");
// 对List进行二分查找,返回索引,List必须是有序的
Collections.sort(arrayList);
System.out.println(Collections.binarySearch(arrayList, 7));

Arrays tools:

Sort by: the Sort ()

// *************排序 sort****************
int a[] = { 1, 3, 2, 7, 6, 5, 4, 9 };
// sort(int[] a)方法按照数字顺序排列指定的数组。
Arrays.sort(a);
System.out.println("Arrays.sort(a):");
for (int i : a) {
  System.out.print(i);
}
// 换行
System.out.println();

// sort(int[] a,int fromIndex,int toIndex)按升序排列数组的指定范围
int b[] = { 1, 3, 2, 7, 6, 5, 4, 9 };
Arrays.sort(b, 2, 6);
System.out.println("Arrays.sort(b, 2, 6):");
for (int i : b) {
  System.out.print(i);
}
// 换行
System.out.println();

int c[] = { 1, 3, 2, 7, 6, 5, 4, 9 };
// parallelSort(int[] a) 按照数字顺序排列指定的数组(并行的)。同sort方法一样也有按范围的排序
Arrays.parallelSort(c);
System.out.println("Arrays.parallelSort(c):");
for (int i : c) {
  System.out.print(i);
}
// 换行
System.out.println();

// parallelSort给字符数组排序,sort也可以
char d[] = { 'a', 'f', 'b', 'c', 'e', 'A', 'C', 'B' };
Arrays.parallelSort(d);
System.out.println("Arrays.parallelSort(d):");
for (char d2 : d) {
  System.out.print(d2);
}
// 换行
System.out.println();

//对字符串排序的情况
String[] strs = { "abcdehg", "abcdefg", "abcdeag" };
Arrays.sort(strs);
System.out.println(Arrays.toString(strs));//[abcdeag, abcdefg, abcdehg]

Find: binarySearch ()

// *************查找 binarySearch()****************
char[] e = { 'a', 'f', 'b', 'c', 'e', 'A', 'C', 'B' };
// 排序后再进行二分查找,否则找不到
Arrays.sort(e);
System.out.println("Arrays.sort(e)" + Arrays.toString(e));
System.out.println("Arrays.binarySearch(e, 'c'):");
int s = Arrays.binarySearch(e, 'c');
System.out.println("字符c在数组的位置:" + s);

Compare: the equals ()

// *************比较 equals****************
char[] e = { 'a', 'f', 'b', 'c', 'e', 'A', 'C', 'B' };
char[] f = { 'a', 'f', 'b', 'c', 'e', 'A', 'C', 'B' };
/*
* 元素数量相同,并且相同位置的元素相同。 另外,如果两个数组引用都是null,则它们被认为是相等的 。
*/
// 输出true
System.out.println("Arrays.equals(e, f):" + Arrays.equals(e, f));

Padding: the Fill ()

// *************填充fill(批量初始化)****************
int[] g = { 1, 2, 3, 3, 3, 3, 6, 6, 6 };
// 数组中所有元素重新分配值
Arrays.fill(g, 3);
System.out.println("Arrays.fill(g, 3):");
// 输出结果:333333333
for (int i : g) {
  System.out.print(i);
}
// 换行
System.out.println();

int[] h = { 1, 2, 3, 3, 3, 3, 6, 6, 6, };
// 数组中指定范围元素重新分配值
Arrays.fill(h, 0, 2, 9);
System.out.println("Arrays.fill(h, 0, 2, 9);:");
// 输出结果:993333666
for (int i : h) {
  System.out.print(i);
}

Transfer list: asList ()

// *************转列表 asList()****************
/*
* 返回由指定数组支持的固定大小的列表。
* (将返回的列表更改为“写入数组”。)该方法作为基于数组和基于集合的API之间的桥梁,与Collection.toArray()相结合 。
 * 返回的列表是可序列化的,并实现RandomAccess 。
 * 此方法还提供了一种方便的方式来创建一个初始化为包含几个元素的固定大小的列表如下:
*/
List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
System.out.println(stooges);

To String: toString ()

// *************转字符串 toString()****************
/*
* 返回指定数组的内容的字符串表示形式。
*/
char[] k = { 'a', 'f', 'b', 'c', 'e', 'A', 'C', 'B' };
System.out.println(Arrays.toString(k));// [a, f, b, c, e, A, C, B]

Copy: copyOf ()

// *************复制 copy****************
// copyOf 方法实现数组复制,h为数组,6为复制的长度
int[] h = { 1, 2, 3, 3, 3, 3, 6, 6, 6, };
int i[] = Arrays.copyOf(h, 6);
System.out.println("Arrays.copyOf(h, 6);:");
// 输出结果:123333
for (int j : i) {
  System.out.print(j);
}
// 换行
System.out.println();
// copyOfRange将指定数组的指定范围复制到新数组中
int j[] = Arrays.copyOfRange(h, 6, 11);
System.out.println("Arrays.copyOfRange(h, 6, 11):");
// 输出结果66600(h数组只有9个元素这里是从索引6到索引11复制所以不足的就为0)
for (int j2 : j) {
  System.out.print(j2);
}
// 换行
System.out.println();

Guess you like

Origin www.cnblogs.com/itzlg/p/11374850.html