Java array (translated from Java tutorials)

Source: Internet
Author: User
Tags array example

From http://www.cnblogs.com/ggjucheng/archive/2012/12/17/2821925.html

English from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Array

An array is an object container that holds a fixed number of values of the same type. When an array is created, the length of the array is created. After creation, its length is fixed. You have read the array example, "Hello world! "Program. This chapter discusses more details about arrays.

Each member of the array becomes an element, and each element is accessed through a digital index. For example, the number index starts from 0 and contains 9th elements, which are accessed through index 8.

The following program,Arraydemo, create an integer array, put some values into it, and then output each value to the standard output.

Class arraydemo {public static void main (string [] ARGs) {// declares an array of integers int [] anarray; // allocates memory for 10 integers anarray = new int [10]; // initialize first element anarray [0] = 100; // initialize second element anarray [1] = 200; // etc. anarray [2] = 300; anarray [3] = 400; anarray [4] = 500; anarray [5] = 600; anarray [6] = 700; anarray [7] = 800; anarray [8] = 900; anarray [9] = 1000; system. out. println ("element at index 0:" + anarray [0]); system. out. println ("element at Index 1:" + anarray [1]); system. out. println ("element at index 2:" + anarray [2]); system. out. println ("element at index 3:" + anarray [3]); system. out. println ("element at index 4:" + anarray [4]); system. out. println ("element at index 5:" + anarray [5]); system. out. println ("element at index 6:" + anarray [6]); system. out. println ("element at index 7:" + anarray [7]); system. out. println ("element at index 8:" + anarray [8]); system. out. println ("element at index 9:" + anarray [9]) ;}}

Program output is

 
Element at index 0: 100 element at Index 1: 200 element at index 2: 300 element at index 3: 400 element at index 4: 500 element at index 5: 600 element at index 6: 700 element at index 7: 800 element at index 8: 900 element at index 9: 1000

In the real-world solution, the biggest possibility is to use a loop structure to traverse every element in the array, instead of writing a row like the previous line.Code. However, the example clearly illustrates the array syntax.

 

Declare array Variables

The above program uses the following code DeclarationAnarray:

 
// Declares an array of integersint [] anarray;

Just like declaring other types of variables, the array declaration has two parts: the array type and the array name. The type of an array is writtenType[], Type is the data type of the elements contained in the array. The special symbol in square brackets indicates that this variable contains the array. The size of the array, not part of its type (so square brackets are empty ). The array name follows the rules and conventions of the command specification. Like other types of variables, the Declaration does not actually create an array-it simply tells the compiler that the variable will hold the array of this type.

Similarly, other types of arrays can be declared:

Byte [] character; short [] character; long [] character; float [] anarrayoffloats; double [] anarrayofdoubles; Boolean [] character; char [] anarrayofchars; string [] anarrayofstrings;

You can also put brackets behind the array name:

 
// This form is discouragedfloat anarrayoffloats [];

However, this form is not recommended by Convention. The specified type should appear when the array type is determined in brackets.

 

Create, initialize, and access the Array

One way to create an array is to use the new operator. The arraydemo program allocates an array of 10 elements and assigns an array referenceAnarray variable.

 
// Create an array of integersanarray = new int [10];

If the declaration is missing, the compiler reports an error. The error message is similar to the following:

 
Arraydemo. Java: 4: Variable anarray may not have been initialized.

The following rows assign values to each element of the array:

 
Anarray [0] = 100; // initialize first elementanarray [1] = 200; // initialize second elementanarray [2] = 300; // etc.

Each array element is accessed through a digital index:

System. out. println ("element 1 at index 0:" + anarray [0]); system. out. println ("element 2 at Index 1:" + anarray [1]); system. out. println ("element 3 at index 2:" + anarray [2]);

You can use shorthand syntax to create and initialize Arrays:

 
Int [] anarray = {100,200,300,400,500,600,700,800,900,100 0 };

The length of the array is determined by the length of the value between {And.

 

Use two or more brackets to declare an array (multi-dimensional array ). For example, string [] [] names. Each element must be accessed through a related digital index.

JavaProgramming LanguageMulti-dimensional arrays simply look like elements are arrays of arrays. Unlike C or Fortran, the consequence of doing so is that the row is allowed to be different in length. As shown in the multidimarraydemo program:

 
Class multidimarraydemo {public static void main (string [] ARGs) {string [] [] names = {"mr. "," mrs. "," Ms. "},{" Smith "," Jones "}}; // mr. smith system. out. println (Names [0] [0] + Names [1] [0]); // Ms. jones system. out. println (Names [0] [2] + Names [1] [1]);}

The program output is as follows:

 
Mr. smithms. Jones

Finally, use the built-in Length attribute to display the length of any array, code

 
System. Out. println (anarray. Length );

The length of the output array to the standard output.

 

Copy Array

The system class has an arraycopy method that can effectively copy data from one array to another:

 
Public static void arraycopy (Object SRC, int srcpos, object DEST, int destpos, int length)

The two object Parameters specify the source array SRC and the target array DeST. The remaining three int parameters indicate the starting position of the source array, the starting position of the target array, and the number of elements in the array to be copied.

The next program, arraycopydemo, declares a char array, spelling "decaffeinated", usingArraycopy copies the string of the first array to the second array:

Class arraycopydemo {public static void main (string [] ARGs) {char [] copyfrom = {'D', 'E', 'C', 'A', 'F ', 'F', 'E', 'I', 'n', 'A', 't', 'E', 'D '}; char [] copyto = new char [7]; system. arraycopy (copyfrom, 2, copyto, 0, 7); system. out. println (new string (copyto ));}}

The program output is as follows:

 
Caffein
Related Article

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.