One-dimensional and two-dimensional arrays in the Java Foundation

Source: Internet
Author: User
Tags array length

1. Array: A collection of sets of data with the same data type.

One-dimensional arrays

2. Creating a one-dimensional array

A. Arrays as objects allow memory allocation using the New keyword, before using an array, you must first define the type of the array variable to which the array is declared.

declaring an array:

syntax: array element type array name [];

array element type [] array name;

Note: array element type: Determines the data type of the array, including the base data type and the fee base data type.

Array Name: Define yourself for a valid identifier

[]: Indicates that the variable is an array type variable, and a single "[]" represents a one-dimensional array to be created.

Example: int arr[];

String[] arr;

Note : A. After declaring an array, it is not possible to access any of his elements, because declaring an array simply gives the name of the array and the data type of the element.

B. The actual use of the array also allocates memory space for it, and the length of the array must be specified when allocating memory space.

Allocate memory space:

Syntax: array name =new array element type [number of array elements];

Note: array name: The name of the array variable that has been declared;

NEW: keyword for allocating space to an array

Number of array elements: Specifies the number of variables in the array, that is, the length of the array.

For example: Arr=new int[5];

Note : A. The subscript of an array in memory space starts at 0.

B. When using the New keyword to allocate memory for an array, each element in the arrays is 0.

An array of type int initial value is: 0 The initial value of the Boolean type is: False the initial value of the char type is: "

Declaring an array and allocating memory can be executed concurrently

Syntax: array type array name [] =new array element type [number of array elements]

For example: int day[]=new int[30]; Created a one-dimensional array of day, which developed an array length of 30.

Note: no matter which method declares an array, the preceding brackets do not fill in the contents.

Initializing an array

The way the array is initialized is to include the data type within the curly braces, separating the values of the array elements with commas, and allocating a certain amount of space to the system itself.

For example: int arr []=new int[] {1,2,3,4,5,6};

int arr []={1,2,3,4,5,6};

Note: when initializing an array, you can omit the new operator and the length of the group.

Two-dimensional arrays

Two-dimensional arrays: if each element in a one-dimensional array is still a one-dimensional array, then it is a two-dimensional array.

Creation of two-dimensional arrays

Syntax: array element type array name [];

array element type [] array name;

Note: array element type: Determines the data type of the array, including the base data type and the fee base data type.

Array Name: Define yourself for a valid identifier

Symbol []:[]: Indicates that the variable is an array type variable, two "[]" means to create a two-dimensional array.

Note: As with a one-dimensional array, if a two-dimensional array is declared without allocating memory space, the New keyword class is also used to allocate memory space before the individual elements can be accessed.

Allocate memory space:

Syntax: array element type array name =new array element type [number of elements of array] [number of array elements];

Note: The number of the first bracket is the number of one-dimensional arrays, and the number of the second bracket is the length of those one-dimensional arrays.

For example: int a[][]=new int[2][4]//The assigned a has two one-dimensional arrays with a length of 4.

Initialization of two-dimensional arrays:

Syntax: Type b[][]={value1,value2,value3,value4,value5 ...}

Note: Type: Array data type

B: Array name, a valid identifier

Valuen: The value of each element in the array

For example: int myarr[][]={{12,10},{1,2,3}}; Myarr[1][2]=3

Sorting on an array: the static sorts () method of the arrays class enables you to sort the array.

Syntax: Arrays.sort (object)

Comment: object: The array name to sort

Return value: the sorted array.

For example: int arr []={18,45,34,12,20};

Arrays.sort (arr);

for (int i = 0; i < arr.length; i++) {

System.out.println (Arr[i])
}

The result is:

12

18
20
34
45

Populate replace array elements: Replace the elements in the arrays with the static method fill () of the arrays class. The Fill method has two parameter types, int and byte two.

1. Syntax: Fill (int a, int value)

Note: A: Array to be substituted for elements

Value: To store the values of all the elements in the array

Return value: the populated array.

For example: int a[]=new int[5]; Creates an array of type int with a length of 5

Arrays.fill (a,10); Populating with 10 pairs of arrays

for (int i = 0; i < arr.length; i++) {

System.out.println (Arr[i]);
}

Original result: 0 0 0 0;

Results now: 10 10 10 10

2. Syntax: Fill (int[] A, int fromIndex, intfromindex, int value)

Note: A: The array to be populated

Value: To store the values of all the elements in the array

Return value: The array after the replacement element.

FromIndex: The index of the first element that is populated with the specified value (including)

Toindex: The index of the last element that is populated with the specified value (not included)

For example: int a[]={45,2,10,56,14};

Arrays.fill (a,1,5,8);

for (int i = 0; i < arr.length; i++) {

System.out.println (Arr[i]);
}

The result is: 8 8 8

Copy array: Copyof method of Arrays class and Copyofrange method for copying of array

CopyOf () copies the array to the specified length

Copyofrange () copies the specified length of the specified array into a new array.

Syntax: copyOf (arr,int newlength) copyofrange (arr,int formindex, int toindex)

Note: A: The array object to be copied

return Value: Copies the array after the specified position.

FromIndex: Specifies the index position at which to start copying the array, FromIndex must be between 0 and the length of the entire array (including)

Toindex: To copy the last index position of the range, it can be greater than the length of the array a (not included)

For example:

int a[]={45,2,10,56,14};

Arrays. Copyofrange (a,1,4)

for (int i = 0; i < a.length; i++) {

System.out.println (A[i]);
}

The result:2

One-dimensional and two-dimensional arrays in the Java Foundation

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.