Learn from teacher Wang (ii) List interface

Source: Internet
Author: User

List interface
Speaker: Wang Shaohua QQ Group No.: 483773664
Learning Goals

1. Common implementation class of List interface

2, the use of ArrayList class

I. Overview
    • In the package: Java.util Package

    • Common classes that implement the list interface are ArrayList and LinkedList

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/7F/B5/wKioL1cp2wmSXFoIAAAVGOr3cps243.png " data_ue_src= "E:\My knowledge\temp\7629f3c3-2764-4d67-84c9-baf71aa5861c.png" >

    • Elements: Objects (including null), allow duplicates, store orderly

Second, ArrayList Collection class
(i) Characteristics of the ArrayList collection class
    • ArrayList implements a variable-length array

    • Allocates contiguous space in memory.

    • Pros: The efficiency of traversing elements and random access elements is high

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/7F/B7/wKiom1cp2jSCocTzAAAppjIdmvw516.jpg " data_ue_src= "E:\My knowledge\temp\34a430d4-6b0b-44be-9732-5446a65cc6d2.jpg" >

(ii) demand
    • Use collections to store information about multiple dogs, get the total number of stored dogs, and sequentially output related content

(iii) analysis

The number of elements is uncertain, and it is required to obtain the actual number of stored elements and to obtain and output the element information sequentially, which is required to implement the ArrayList class of the list interface.

The specific steps to achieve this requirement through ArrayList are as follows:

    • Create Multiple Dog objects

    • Create a ArrayList collection object and put multiple dog objects into it

    • Number of output dogs

    • Displays information about each dog by iterating through the collection

(iv) Methods for using the list interface
    1. Boolean Add (Object O): Adds an element at the end of the list, starting at index 0

    2. void Add (int index,object o): Adds an element at the specified index position. The index position must be between 0 and the number of elements in the list

    3. int size (): Returns the number of elements in the list

    4. Object get (int index): Returns the element at the specified index position. The removed element is of type object and requires a forced type conversion before use

(v), reference code
1234567891011121314151617181920212223 public class Test {    public static void main(String[] args) {        // 1、创建四个狗狗对象        Dog ououDog = new Dog("欧欧", "雪娜瑞");        Dog yayaDog = new Dog("亚亚", "拉布拉多");        Dog meimeiDog = new Dog("美美", "雪娜瑞");        Dog feifeiDog = new Dog("菲菲", "拉布拉多");        // 2、创建ArrayList集合对象并把四个狗狗对象放入其中        List dogs = new ArrayList();        dogs.add(ououDog);        dogs.add(yayaDog);        dogs.add(meimeiDog);        dogs.add(2, feifeiDog); // 添加feifeiDog到指定位置        // 3、输出集合中狗狗的数量Learn from teacher Wang (ii) List interface

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.