Generics (generic) reverses all elements in the array
My Java Foundation has been not very good, recently took the time to strengthen their basic knowledge, basic skills, to go more practical.
For the map collection, we have to use the enhanced for loop mechanism skillfully (for each), where EntrySet method is important.
@Test Public voidTest1 () {Map<String,Integer> Map = NewHashMap<String,Integer>();Map.Put"AA",1);Map.Put"BB",2);Set<Entry<String,Integer>> Set = Map.EntrySet (); for (Entry<String,Integer>Entry:Set) {System.Out.println (entry.GetKey ()+ "=" +Entry.GetValue ()); } }
Furthermore, generics defined on a class can only be valid for non-static member variables of a class, not static.
Why is the static member invalid?
We only give it an example of a generic type when we are in the new instance of the class, and the static member variable, we can directly class name. XXX is called, never know what its type is.
/** * Write a generic method that swaps elements on an array of any two positions * / Private Static<T>void Swap(t[] arr,intPOS1,intPOS2) {T temp = arr[pos1]; ARR[POS1] = Arr[pos2]; ARR[POS2] = temp; }/** * Write a generic method that receives an arbitrary array and reverses all elements in the array * @param arr */ Private Static<T>void Reverse(t[] arr) {intStart =0;intEnd = arr.length-1; while(true) {if(Start>=end) Break; T temp = Arr[start]; Arr[start] = Arr[end]; Arr[end] = temp; Start + +; End--; } }
Generics (generic) reverses all elements in the array