Generics (generic) reverses all elements in the array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.