Some objects in Java are called Containers (container). A container can contain multiple objects, each of which is called an element in a container. A container is a data structure (data structure) encapsulated by an object.
A vessel full of dreams
Different data structures have different ways of organizing elements, or they can have different operations. According to the specific implementation of different, the operational efficiency of the data structure are also different. The same is true for containers in Java. We need to choose the right container to meet the changing needs.
(about the data structure more content, can refer to the armchair: algorithm and data structure)
Array
Arrays (array) are the most common data structures. An array is an ordered set of elements of the same type and has a fixed size (which can hold a fixed number of elements). Arrays can be randomly accessed (random access) elements based on subscript (index). In memory, an array is usually a contiguous unit of storage.
Java supports arrays in this data structure. We need to describe the type and size of each array. As follows:
public class Test
{public
static void Main (string[] args)
{
human[] persons = new HUMAN[2]; Array size 2
persons[0] = new Human (160);
PERSONS[1] = new Human (170);
Int[] A = {1, 2, 3, 7, 9}; Array size 5
System.out.println (a[2]);
String[] names = {"Tom", "Jerry", "Luffy"}; Array size 3
System.out.println (names[0]);
}
When describing a type, add a [] after the type description (Human) to indicate an array. When you use new to create a container, you need to describe the size of the array.
We can invoke an element by using the array name [subscript]. We can initialize the elements of an array one by one, or we can initialize the array with {} at the same time as the declaration.
For arrays of primitive types, such as human[], the array stores references to objects.
We can call the System.arraycopy () method to effectively copy the array:
public class Test
{public
static void Main (string[] args)
{
int[] Afrom = {1, 2, 3, 7, 9};//array size 5
int[] aTo = new Int[3];
System.arraycopy (Afrom, 1, aTo, 0, 3);
System.out.println (ato[1]);
}
In System.arraycopy (), Afrom for the array to be copied, the ATO for the array that you want to copy, 1 for the afrom of the elements you want to copy out, 0 for the starting position of the elements in the ATO where you want to store replication, 3 for the total number of elements you want to copy.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/