Three ways to reverse the Java array elements

Source: Internet
Author: User

There are several implementations of the inverse of an array element, and here are three common types.

    • Direct array Element swap
@Testpublic void testreverseself () throws Exception {System.out.println ("Use reverseself"); String[] strings = { "Ramer", "jelly", "Bean", "cake"}; System.out.println ("\ T" + arrays.tostring (strings)); For (int start = 0, end = strings.length- 1; start < end; start++, end--) {String temp = strings[ end]; Strings[end] = Strings[start]; Strings[start] = temp; } System.out.println ("\ T" + arrays.tostring (Strings));}           

    • The order in which the arraylist:arraylist is deposited and taken out is the same, and you can use this feature to temporarily store the array elements.
@Testpublic void testarraylist () throws Exception {System.out.println ("Use ArrayList method"); String[] strings = { "Ramer", "jelly", "Bean", "cake"}; System.out.println ("\ T" + arrays.tostring (strings)); list<string> list = new arraylist<> (strings.length); For (int i = strings.length- 1; I >= 0; i--) {List.add (strings[i]);} strings = List.toarray (stri NGS); System.out.println ("\ T" + arrays.tostring (Strings));}           

    • using collections and Arrays tool classes
 @Test public void testcollectionsreverse () throws Exception {System.out.println ( "use Collections.reverse () method"); String[] strings = { "jelly",  "Bean",  "cake"}; System.out.println ( "\ T" + arrays.tostring (strings)); //This method is only for reference types, for basic types such as: //char[] cs = {' A ', ' B ', ' C ', ' G ', ' d '}; Span class= "Hljs-comment" >//should be defined or converted to the corresponding reference type: //character[] cs = {' A ', ' B ', ' C ', ' G ', ' d '}; Collections.reverse (Arrays.aslist (strings)); System.out.println ( "\ T" + arrays.tostring (Strings));       

    • Speed test:
@TestPublicvoidTesttimeduration ()Throws Exception {Recordtime (Arrayreverse.class,"Testcollectionsreverse"); Recordtime (Arrayreverse.class,"Testarraylist"); Recordtime (Arrayreverse.class,"Testreverseself");}PrivateStatic string[] strings =New string[1000000]; {for (int i =0; I <1000000; i++) {Strings[i] = string.valueof (i);}}/** * Records the total time of operation execution. * *@param <T> the generic type *@param clazz The Clazz *@param methodName The method name */Public <T> void recordtime (class<t> clazz, String methodName) { long start = System.currentti Memillis (); System.out.println ("Start:" + start); method[] Declaredmethods = Clazz.getdeclaredmethods (); For (Method method:declaredmethods) {String name = Method.getname (); if (name.equals (MethodName)) { try {method.invoke (clazz.newinstance ());} catch (Exception e) {e.printstacktrace ();}} } Long end = System.currenttimemillis (); System.out.println ("End:" + end); System.out.println ("duration:" + (End-start) + "MS");}          

    • Test results:

      Using the collections and Arrays tool classes: MS
      Using Arraylist:7 ms
      Direct array element swap: 4 ms
      When the amount of data is getting larger, the way you use ArrayList becomes slow.
      Using array elements directly is always the quickest thing to do.

    • Summary: It is simpler to use the collections and Arrays tool classes to invert array elements, but it is faster to operate on the original array and consumes the least amount of memory.

Three ways to reverse the Java array elements

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.