Declaration, initialization, and reference of a 6.Java one-dimensional array

Source: Internet
Author: User

The declaration of a one-dimensional array must be declared when it is used, as is the case with arrays, which must be declared before they are used. First look at the following code, how to declare a variable.
int A;
A careful analysis: int refers to the data type of the variable, a refers to the variable name, and the declaration of the variable can be contacted by the declaration of the array.
int a[];
To analyze it carefully: int is the data type of all the data in the exponential group, or it is the data type of the array, a[] represents the array name.

the declaration of a primitive type array has several forms:
int a[];
Int[] A;
There is no difference between the two forms, the use of the same effect, readers can choose according to their own programming habits. Initialization of one-dimensional arraysthe initialization of an array is divided into static initialization and dynamic initialization: Static initialization is assigned at the same time as the array is declared, and dynamic initialization is assigned after the array is declared.

1. Static initialization
int intarray[]={1,2,3,4};
String stringarray[]={"abc", "How", "You"};

2. Dynamic initialization
int intarray[];
Intarray = new Int[5];

String stringarray[];
String Stringarray = new string[3];/* creates a reference space (32 bits) for each element in the array */
stringarray[0]= New String ("how");//Open space for the first array element
stringarray[1]= New String ("is");//Open space for the second array element
stringarray[2]= New String ("You");//A reference to a space-one-dimensional array element for a third array elementone-dimensional array elements are referenced in the following way:
Arrayname[index]
Index is an array subscript, which can be an integer constant or an expression, and the subscript starts at 0. Each array has a property length that indicates its lengths, for example: Intarray.length indicates the length of the array intarray.

In the process of writing a program, if you want to reference the length of an array, you typically use the variable "length", which is typically used in the following format:
array name. length

Example: Create an integer array A with 10 elements, assign a value to each array element by A[i]=i*i, and then output the result.
public class arrary1{
public static void Main (string[] args) {
Int[] A;
A=new INT[10];
int i;
for (i=0;i<10;i++) {
System.out.println ("a[i]=" + (i*i));
}
}
}
Operation Result:
A[0]=0
A[1]=1
A[2]=4
A[3]=9
A[4]=16
A[5]=25
a[6]=36
a[7]=49
A[8]=64
a[9]=81
The program first makes the array declaration "int[] a", and then creates a "a=new int[10]", and finally uses the Loop statement to output all the data in the array.

A few notes:
    • Arrays are object-type data, and the use of new is noted when declaring an array.
    • When declaring an array, be sure to consider the maximum capacity of the array to prevent the lack of capacity. Once an array is declared, its capacity is fixed and cannot be changed. If you want to change the capacity when you run the program, you need to use the array list. The array list is not part of this chapter and is described in detail in the Data structure section.
    • In fact, the array has a disadvantage, that is, once declared, can not change the capacity, this is also the reason for its low frequency of use. Typically stored data uses an array list or vector, both of which data structures are stored.

Declaration, initialization, and reference of a 6.Java one-dimensional 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.