Fundamentals of Java language programming: arrays

Source: Internet
Author: User

In Java, an array is a variable that stores data of the same data type in memory. Each data element in the array belongs to the same data type.

First, the basic elements of the array

1. Identifier: That is, the name of the array, used to distinguish between different arrays.

2. Array elements: Data stored in the array.

3. Array subscript: The number of elements in a group, easy to access data.

4. Element type: The array elements stored in the array should be of the same data type.

Ii. steps to use an array

1. Declaring an array, 2. Allocating space, 3. Assigning, 4. Processing data

Iii. Common Mistakes

1. Array subscript starting from 0, 2. Array access out of bounds

Iv. default values for array elements

1.int-0 2.double-0.0 3.char-' \u0000 ' 4.boolean-false 5.string-null

Array cannot be divided into two lines when declaring and assigning values

Correct assignment method: int[] nums={1,2,3};

Int[] Nums=new int[]{2,3,5};

Int[] Nums=new int[5];       Nums[0]=1; nums[1]=2;

V. Common operation of arrays

Traversal (elements in the loop output array): Positive order for (int i=0;i<nums.length;i++) {}

Reverse for (int i=nums.length-1;i>0;i--) {}

Maximum: Max compares the elements in the array in turn, assigning a large value to the max

Min compares the elements in the array in turn, assigning a small value to min

Insertion algorithm: 1. Once the array is created, the length is immutable, creating a new array with a new array whose length is more than the old 1 array name. length

2. Assign the elements in the old array to the new array in turn, and the length of the array is old.

3. Compare to find the position of the element, in ascending order to find the first bigger than him, descending to find the first smaller than him, and then index=i;break;

4. Move backwards to position

for (int i=nums2.length-1;i>=index;i--) {

NUMS2[J]=NUMS[J-1];

}

5. Insert element: Nums2[index]=insert;

6. Traversing a new array

The class of the arrays operation array (static method) is the class name Method name ();

1.sort (); Sort Ascending

2.fill (); Assigns all elements of the array to Val

3.binarySearch (); the subscript of the query element in the array

4.copyOf (); Copies the original array into a new array of length

5.equals (); compare two arrays for equality

6.toString (); Converts an array to a string

Seven, Arraysindexoutofboundexception: Error index value, that is, array subscript out of bounds exception.

Eight, bubble sort

n=nums.length; outer n-1; inner layer N-1-i

Fundamentals of Java language programming: 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.