An array (array) Description of Java

Source: Internet
Author: User

Code Description:

1  PackageArray;2 3 Importjava.util.ArrayList;4 Importjava.util.Arrays;5 Importjava.util.List;6 7 /**8 * Instructions for using the array:9 * Content:Ten * 1, array instantiation; One * 2, array and ArrayList conversion; A * 3. Instantiate ArrayList via array - * Difference: - *--array ([]): The most efficient, but its capacity is fixed and can not be changed dynamically; the *--arraylist: Capacity can be dynamically increased, but sacrificing efficiency; -  * https://www.cnblogs.com/wangbin2188/p/6524200.html -  */ -  Public classArraydemo { +      Public Static voidMain (string[] args) { -         //Instantiation 1 +string[] Array =NewString[2]; AArray[0] = "AAA"; atARRAY[1] = "BBB"; -  -         //Instantiation 2 -String[] Array2 = {"A", "B", "C"}; -          for(String s:array2) { - System.out.println (s); in         } -  to //data element Fill value +Arrays.fill (Array2, "D"); -          for(String s:array2) { the System.out.println (s); *         } $         //The arrays method contains: sort (), BinarySearch (), Equals (), fill (), aslist ()Panax Notoginseng  -         //instantiate ArrayList with array theList List =NewArrayList (Arrays.aslist ("A", "B")); + System.out.println (list); A  theList List2 =arrays.aslist (array); + System.out.println (list2); -  $  $         //ArrayList convert to array - Arraydemo.listtoarray (); -  the         //array conversion to ArrayList - arraydemo.arraytolist ();Wuyi  the     } -  Wu     /** - * ArrayList converted to array About      */ $      Public Static voidListtoarray () { -list<string> list =NewArraylist<string>(); -List.add ("Wang Lihu"); -List.add ("Zhang San"); AList.add ("John Doe"); +         intSize =list.size (); thestring[] Array = (string[]) List.toarray (Newstring[size]); -          for(inti = 0; i < Array.Length; i++) { $ System.out.println (Array[i]); the         } the     } the  the     /** - * Array converted to ArrayList in      */ the      Public Static voidarraytolist () { thestring[] Array =NewString[3]; AboutArray[0] = "Wang Lihu"; theArray[1] = "Zhang San"; theARRAY[2] = "John Doe"; thelist<string> list =arrays.aslist (array); +          for(inti = 0; I < list.size (); i++) { - System.out.println (List.get (i)); the         }Bayi     } the}

Operation Result:

Abcddd[a, B][AAA, BBB] Wang Lihu Zhang Sanli Tiger Dick and Harry Process finished with exit code 0

An array (array) Description of Java

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.