Java Note 6-arrays

Source: Internet
Author: User

Knowledge outline
First, the application of the array
Since the array is used to store data, its operation always increases, deletes, changes, and checks.
An array is the most basic data structure, and it has the highest efficiency on the query. But in increasing,
The operational efficiency of the deletion is minimal.

Because
Once the length of the array is determined, it cannot be changed, so when you add elements, you need to
Re-extend a larger array out.
Once an array element is deleted, all the elements behind it are moved 1 times forward in turn.

---------------------
int[] arr1 = new INT[5];
int[] arr2 = arr1; Ok
Only 1 objects are created here, but there are 2 references, and these 2 references point to an object.
Such as:
ARR2[3] = 100;
System.out.println (Arr1[3]); 100

------------------
Copy of an array element
There are 2 different ways
1. Using the System.arraycopy method
System.arraycopy (
Object src//Source Array
An int srcpos//where the source array starts
An object dest//the target array
An int destpos//where the target array begins to put
, int length//How many elements to copy from the source array to the destination array
);
Note: Before using this method, both the source array and the destination array must be initialized.
Such as:
int[] arr1 = new int[]{1,2,3,4,5}; [1 2 3 4 5]
int[] arr2 = new int[arr1.length+2]; [0 0 0 0 0 0 0]
//
System.arraycopy (arr1,1,arr2,2,arr1.length-2);
At this point, the elements in the arr2 are: [0 0 2 3 4 0 0]

This method is very efficient.

2. Tool methods using the Java.util.Arrays class
Int[] CopyOf (int[] original, int newlength);
This method has many overloads, which are based on the original array and the length to create a
New array, the element value of this new array is the same as the original element, if this
Newlength is larger than the original array length, the extra element is populated with 0 if the
Newlength is smaller than the original array length, the extra elements are truncated.

Such as:
int[] arr1 = new int[]{1,2,3,4,5};
int[] arr2 = arrays.copyof (arr1,arr1.length);
The ARR2 element value is identical to the arr1.
int[] Arr3 = arrays.copyof (arr1, arr1.length + 2);
The ARR3 element is [1 2 3 4 5 0 0]
int[] Arr4 = arrays.copyof (arr1 arr1.length-2);
The ARR4 element is [1 2 3]

---
Arrays Array Tool Class
ToString method
CopyOf method
Fill method
...

Java Note 6-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.