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
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" >
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
(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
Boolean Add (Object O): Adds an element at the end of the list, starting at index 0
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
int size (): Returns the number of elements in the list
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 |