[Learn JAVA from scratch | Article 11] ArrayList collection

Table of contents

Foreword: 

ArrayList: 

Common member methods:

Case exercise:

1. Collection traversal method (reference data type):

2: Collection traversal method (basic data type):

Summarize: 


Foreword: 

In this chapter, we will introduce a class that is widely used in JAVA: the ArrayList collection. The weight of this class is comparable to the vector class in C++. That is to say, if you want to use java in the future, you will inevitably use this class a lot. Therefore, we must be serious and master the application of this class proficiently.

ArrayList: 

ArrayList is a class in the Java Collections Framework , which provides a resizable array implementation and implements the List interface.

It's very similar to a normal array, but it's resizable. It can automatically grow or shrink as needed.

ArrayList can store any type of object such as String, Integer, Object etc. Elements stored in ArrayList can be accessed by index, called element number, numbering starts from 0.

ArrayList utilizes its internal array to implement many common methods such as adding elements, removing elements and traversing elements. Additionally, ArrayList provides methods such as sorting and searching to make it easier to use.

ArrayList cannot directly store basic data types, it can only store reference data types

Prerequisites:

Packages need to be imported:

import java.util.ArrayList;

When creating an ArryList class, we should specify the stored data type:

ArrayList<类型> list = new ArrayList<类型>();

Common member methods:

1. add(): Add elements to the ArrayList.

ArrayList<String> list = new ArrayList<String>();
list.add("John");

2. get(): Get the element at the specified index position in the ArrayList.

String name = list.get(0);

3. set(): Set the element value at the specified index position in the ArrayList.

list.set(0, "Tom");

4. remove(): Delete the element at the specified index position or the specified element in the ArrayList.

list.remove(0);
list.remove("Alice");

5. size(): Get the size of ArrayList.

int size = list.size();

6. clear(): Clear the ArrayList.

list.clear();

7. indexOf(): Find the first index position of the specified element in the ArrayList.

int index = list.indexOf("Bob");

8. contains(): Check whether the specified element is contained in the ArrayList.

boolean isContain = list.contains("John");

9. toArray(): Convert ArrayList to an array and return it.

String[] array = list.toArray(new String[list.size()]);

These methods are just some of them; ArrayList has many other useful methods. If you have other requirements, it is recommended to consult relevant documents or tutorials.

Case exercise:

1. Collection traversal method (reference data type):

Requirements: define a collection, add strings, and traverse

Traversal format: element 1, element 2, element 3.

import java.util.ArrayList;

public class test09 {
    public static void main(String[] args) {
        //1.创建集合
        ArrayList<String> list = new ArrayList<String>();

        //添加元素
        list.add("aaa");
        list.add("ddd");
        list.add("ccc");
        list.add("eee");

        //进行遍历
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+",");
        }
    }
}

2: Collection traversal method (basic data type):

Requirements: Define a collection, add words and numbers, and traverse

Traversal format: element 1, element 2, element 3.

The problem with this question is: It is known that collections cannot store basic data types, so how do we make the collection ArrayList store basic data types?
The answer is to convert the basic data type into its corresponding wrapper class:

- byte corresponds to Byte
- short corresponds to Short
- int corresponds to Integer
- long corresponds to Long
- float corresponds to Float
- double corresponds to Double
- char corresponds to Character
- boolean corresponds to Boolean

Wrapper classes can encapsulate basic data types into objects, which is very useful when performing some special operations. For example, using the Integer wrapper class can convert the value of type int to a string of different bases such as binary, octal, and hexadecimal. At the same time, the wrapper class also provides many other methods, such as comparing the size of two numbers, converting strings to numbers, and so on.

import java.util.ArrayList;

public class test09 {
    public static void main(String[] args) {
        //1.创建集合
        ArrayList<Integer> list = new ArrayList<Integer>();

        //添加元素
        list.add(1);
        list.add(2);
        list.add(3);


        //进行遍历
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+",");
        }
    }
}

 Case 3: Create a student class, use this student class as the basic element of storage, and create an ArrayList collection.

Requirements: The attributes of the student class are: name, age.

import java.util.ArrayList;
import java.util.Scanner;


public class test10 {
    public static void main(String[] args) {
        //1.创建集合:
        ArrayList<student>  list= new ArrayList<student>();
        //2.录入数据:
         Scanner sc= new Scanner(System.in);
        for (int i = 0; i < 3; i++) {

            String name =sc.next();
            int age =sc.nextInt();
            student d1 = new student();
            d1.setName(name);
            d1.setAge(age);
            list.add(d1);
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.println("姓名:"+ list.get(i).getName()+"年龄:"+list.get(i).getAge());
        }

    }
}

Summarize: 

Arraylist is a very practical class. It provides an array that will not cross the boundary, which greatly improves our data processing effect. Therefore, we must master it proficiently to win over JAVA.

If my content is helpful to you, please like, comment and bookmark . Creation is not easy, everyone's support is my motivation to persevere!

Guess you like

Origin blog.csdn.net/fckbb/article/details/131292748