Java Advanced Learning (vi) container

Source: Internet
Author: User
Tags arrays data structures

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/

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.