COREJAVAE10V1P3.10 3rd Java BASIC Programming Structure-3.10 Array (Arrays)

Source: Internet
Author: User

An array is a data structure that stores the same type

Declaration and initialization of arrays

int [] A; int a[]; int New int [100];
int New int [+];  for (int i = 0; i <; i++)     // fills the array with numbers 0 to

Cannot change its size once it is created

3.10.1 for Each loop

The For Each loop can be used to assign a value to an array

 for (variable:collection) statement
Int[] A = new int[100];
for (int element:a) System.out.println (element);

Output for each element of array A.

If you want to output an array directly, you can use the ToString method of the Arrays (Java.util.Arrays) class.

System.out.println (Arrays.tostring (a));

3.10.2 array initialization with anonymous array

int [] Smallprimes = {2, 3, 5, 7, one, one }; anonymous array initialization:newint[] {n, a,at EWint[] {n, a, a, A, A, a PNS}; is an int[] anonymous = {: A shorthand for 10> anonymous;

3.10.3 Copying an array

int [] copiedluckynumbers = arrays.copyof (luckynumbers, luckynumbers.length);

If the length exceeds the value, the assignment is 0.

3.10.4 command-line arguments

If you execute a program using the command Java message-g Cruel World

Then you can accept it in the main function.

Args[0]: "-G"args[1]: "Cruel"args[2]: "World"
 Public classmessage{ Public Static voidMain (string[] args) {if(Args.length = = 0 | | args[0].equals ("-H"))) System.out.print ("Hello,"); Else if(Args[0].equals ("-G")) System.out.print ("Goodbye,"); //Print the other command-line arguments       for(inti = 1; i < args.length; i++) System.out.print (" " +Args[i]); System.out.println ("!"); }}

3.10.5 Array Sorting

int New int [10000]; Arrays.sort (a)

3.10.6 Multi-dimensional arrays

Double [] balances;
int [] Magicsquare ={    {3, 2, +},    {5, ten, one, 8},    {9, 6, 7,},    {4 , 1}};
 for (double[] row:a)      for (double  value:row)          do something with value    
System.out.println (Arrays.deeptostring (a)); Fast output 2 multi-dimensional arrays

3.10.7 Irregular Array (ragged Arrays)

Java does not really have a strict multidimensional array, he is an array of array storage, each one-dimensional array of elements can be different, the type can be different.

COREJAVAE10V1P3.10 3rd Java BASIC Programming Structure-3.10 Array (Arrays)

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.