1.Array & Arrays and Collection & Collections Differences
(1) Collection ": is an interface that together with its subclasses constitutes a Collection collection framework;
Collections: is a class, a tool class that serves collections , and provides a series of static methods for searching, sorting, threading, and so on for various collections.
(2) Array: provides a way to dynamically create and access Java arrays;
Arrays: a tool class for manipulating arrays, which are static methods that manipulate arrays . one of the more important methods:aslist () converts an array into a list collection.
2.Collections common methods (see API) (1) public static <t extends Comparable<? Super t>> VoidSort(list<t> List)arrange according to the natural order of the elements(2) public static <T> voidSort(List<t> List, comparator<? Super T> C)sorts the specified list according to the order of the specified comparer
1 import java.util.ArrayList; 2 import java.util.Collections; 3 Import Java.util.Comparator; 4 Import java.util.List; 5 6/** 7 * Sort Method Demo: 8 * 9 * * public class Sortdemo {Ten public static void main (STR Ing[] args) (list<string> al = new Arraylist<string> (), al.add ("013"), and Al.add ("the"); L.add ("002"); System.out.println (AL); Collections.sort (AL), System.out.println (AL), Collections.sort (al,new lencompare ()); 25 System.out.println (AL); 26} 27} 28
(3) public static <T> int BinarySearch(List< extendscomparable< Super t>> list, T key): binary search
searches the specified list for the specified object using the binary search method. Before making this call, the list must be sorted ascending According to the natural order of the list elements
(4) public static <T> int BinarySearch(List< extends t> list, T key,Comparator<? super t> C)
Searches the specified list for the specified object using the binary search method. Before making this call, the list must be sorted in ascending order based on the specified comparer.
(5) public static void reverse (List<?> list): Reverses the order of the elements in the specified list (6) public static void swap (List<?> list, int i, int j): swaps an element at the specified position in the specified list (7) public static <T> void Fill (List<? super t> list, T obj)replaces all elements in the specified list with the specified element. (8) public static <T> void copy (List< Super t> dest,List< extends t> src)
Copies all elements from one list to another list . After you do this, the index of each copied element in the target list is equal to the index of that element in the source list. The target list must be at least the same length as the source list. If the target list is longer, the remaining elements in the target list are not affected.
(9) public static <t extendsObject&comparable<? super t>> T min(Collection< exten DS t> Coll)
Returns the smallest element of a given collection based on the natural order of the elements.
(Ten) public static <T> T min(Collection< extends t> Coll,Comparator<? Super t> Comp)
Returns the smallest element of a given collection, based on the order produced by the specified comparer.
(one) public static <t extendsObject&comparable<? super T>>t Max(Collection< extends t> coll)
returns the largest element of a given collection, based on the natural order of the elements .
Public static <T> boolean ReplaceAll(List<T> list, T oldval, T newval)
Replaces all occurrences of a specified value in a list with another value.
3.Arrays common methods (see API) (1) public static <T> list<t>aslist(T ... a)
returns a list of fixed sizes supported by the specified array. This method is the same as Collection.toArray() together, serves as a bridge between an array-based API and an collection-based API . The returned list is serializable and implemented RandomAccess .
(2) public static void sort(long/char/int/double/byte/Object[] a)
Sorts the specified array of types numerically in ascending order. The sorting algorithm is an optimized fast sorting method.
(3) public static void sort(long/char/int/double/byte/Object[] A, int fromIndex,int Toindex)
sorts the specified range of arrays of the specified type in ascending order of numbers. The sort ranges from index FromIndex(including) to index Toindex(not included). (If fromindex==toindex, the sort range is empty.) )
(4) public static <T> void sort(t[] A, int fromIndex, int toindex,Comparator<? super t> C)
Sorts the specified range of the specified object array according to the order produced by the specified comparer.
(5) public static int binarysearch(Type [] A, long key)
Use the binary search method to search for the specified array of types to obtain the specified value. The array must be sorted (by method) before this call is made sort(long[]) . If no array is ordered, the result is indeterminate. If the array contains more than one element with the specified value, it is not guaranteed which one is found.
(6) public static Boolean equals(Type [] A, long[] A2)
Returns trueif two of the specified array of types are equal to each other. If two arrays contain the same number of elements, and all corresponding element pairs in the two arrays are equal, the two arrays are considered equal. In other words, if two arrays contain the same elements in the same order, the two arrays are equal. In addition, if two array references are null, they are considered equal.
(7) public static void fill(Type [] A, long Val)
Assigns the specified type value to each element of the specified long array.
(8) public static inthashcode(Type [] a): Returns Hashcode (9) public static String based on the contents of the specified arraytoString(Type [] a): Returns a string representation of the contents of the specified array.
1 Public classArraysdemo {2 3 /** 4 * @param args 5 * *6 Public Static voidMain (string[] args) {7 8 //string[] arr = new string[3];9 //swap (arr,1,2);Ten One //int[] arr = {45,23,78,11,99}; A //system.out.println (arrays.tostring (arr)); - - the -String[] STRs = {"ABC","haha","NBA","ZZ"}; - - //boolean B = mycontains (STRs, "NBA"); + //system.out.println (b); - + A //Discover this method in the collection itself. Contains at //Converts an array into a collection. Oh, well, the array goes to the list collection, just to manipulate the elements in the array using the method of the collection. -list<string> list = Arrays.aslist (STRs); - //list.add ("QQ");//java.lang.unsupportedoperationexception//Note that you cannot use the collection additions and deletions method. Cannot change length. - //system.out.println (List.contains ("NBA")); - - in /* If the array is a reference data type, the array element is directly used as the collection element when it is turned into a collection . * If the array is a base data type, the array object will be used as the element in the collection. * * the int[] arr = {45,23,78,11,99}; *list<int[]> List2 = arrays.aslist (arr); $System.out.println (list2.get (0));Panax Notoginseng -} the + /** * @param arr * @param key * @return * / $ Public Static BooleanMycontains (string[] arr,string key) { - - for(inti = 0; i < arr.length; i++) { theString str = arr[i]; - Wuyi if(Str.equals (key)) the return true; - Wu} - return false; About $} - - Public Static<T>voidSwap (t[] arr,intXintY -{ AT temp = arr[x]; +ARR[X] = Arr[y]; theArr[y] = temp; -} $ the}
1 Public classCollectiontoarray {2 3 /** 4 * @param args 5 * *6 Public Static voidMain (string[] args) {7 8 //collection to an array. Collection ToArray9 Ten Onelist<string> list =NewArraylist<string> (); A -List.add ("ABC"); -List.add ("haha"); the / * * The array length passed in, if it is less than the set length, a new array of length and set length is created in the method. * If the incoming array length is greater than or equal to the collection length, the incoming array is used. So the recommended length is defined as the size of the set (); Why do you want to transfer the collection to an array? * is to limit the operation of elements, such as additions and deletions. * * * -string[] arr = List.toarray (NewString[list.size ()]); - -System.out.println (arrays.tostring (arr)); - -} in -}
Code from Creative Podcasts
2018-01-03
Java Container---Arrays & Collections Tool Classes