Java ArrayList Set Explanation of Java Dynamic Array

  • 2021-12-04 18:39:26
  • OfStack

Catalog 1. Overview and Basic Usage of ArrayList Collection 1. Overview 2. Basic Usage 2. Detailed introduction of ArrayList Collection 1. Define an ArrayList Collection 2. Methods commonly used by ArrayList Collection 3. Store "class" in ArrayList Collection 4. Traverse ArrayList Collection 5. Store basic data type in ArrayList Collection 6. ArrayList as parameter of method 7. ArrayList as return value of method

1. Overview and basic use of the ArrayList collection

1. Overview

ArrayList is an implementation of collections, and Collection is the parent class of all collection classes.

Because the length of the array cannot be changed at run time, there is an ArrayList collection.

2. Basic use

Create an ArrayList collection


import java.util.ArrayList;// Don't forget the guide bag 
//<E> Represents generics ,E Can be defined as all reference types , For example String , class, etc 
ArrayList<E> list = new ArrayList<>();

Convert other types of collections to ArrayList


ArrayList<String> setList = new ArrayList<>(new HashSet())

Commonly used methods

(1) Add data-add ()


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

(2) Get Data-get ()


list.get(i);//i Index the elements of the collection 

(3) Delete Data-remove ()


list.remove(i);//i Index the elements of the collection 

(4) Get the length of the set-size ()


int l = list.size();
System.out.println(" The length of the collection is: "+l);

2. Detailed introduction to the ArrayList collection

1. Define an ArrayList collection

(1) Grammatical format


ArrayList<E> list = new ArrayList<>();

(2) Interpretation

< E > Represents generics and represents the types contained in the collection

Generics can only be reference types, not base types

For the ArrayList collection, the result of direct printing is not the address value, but the content. If it is empty, print []

Starting from jdk 1.7, you can write nothing inside the right angle brackets

2. Common methods of ArrayList collection

Defining a collection

Defines a collection of ArrayList with a generic type of String


ArrayList<String> list = new ArrayList<>();
System.out.println(list);// The printed result is: []

Add Element

public boolean add (E e): Adds elements, types, and generics 1 to the collection


// Only string types can be added, and other types will report errors 
list.add("CSDN");
list.add("aaaa");
list.add("bbbb");
System.out.println(list);
// The result of printing the collection is: [CSDN, aaaa, bbbb]

From the ArrayList class, we can see that the add method has a Boolean return value, which can be used to return whether the data was added successfully.


boolean res = list.add("ssss");
System.out.println(" Is the addition successful "+res);

For the ArrayList collection, the add method 1 will be added successfully (other collections are not), so you can not use the return value of the add method.

Get Element

public E get (int index): Gets the element from the collection, with the parameter index representing the element index number.


ArrayList<String> setList = new ArrayList<>(new HashSet())
0

The get method has a return value of a generic type corresponding to the definition of the collection, so pay attention to the data type when receiving data.

Delete Element

public E remove (int index): Deletes an element from the collection, with the parameter index representing the element index number.


ArrayList<String> setList = new ArrayList<>(new HashSet())
1

The remove method has a return value of a generic type corresponding to the definition of the collection. After the deletion operation, the data can be received and the deleted data can be viewed.

Gets the length of the collection

public int size (): Gets the length of the set


ArrayList<String> setList = new ArrayList<>(new HashSet())
2

3. Save "class" in ArrayList collection

(1) First define a standard class for Student


public class Student {
    private String name;
    private int age;
    public Person() {}
    public Person(String name, int age) {this.name = name;this.age = age;}
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
    public int getAge() {return age;}
    public void setAge(int age) {this.age = age;}
}

(2) Create a collection and store it in a "class"


ArrayList<String> setList = new ArrayList<>(new HashSet())
4

4. Traversing the ArrayList collection


ArrayList<String> setList = new ArrayList<>(new HashSet())
5

5. Store the base data type into the ArrayList collection

If you want to put a primitive data type into an ArrayList collection, you must use the wrapper class corresponding to the primitive type.

The wrapper class corresponding to the basic type is as follows:

基本类型 包装类(引用类型,包装类都位于java.lang包下)
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

"tips"

Except for int and char, which are not capitalized, the other six basic types of wrapping classes are capitalized.


ArrayList<String> setList = new ArrayList<>(new HashSet())
6

6. ArrayList as a parameter to the method

Case analysis:

1. ArrayList as a parameter

2. Use "{}" for opening and ending, and use "," to separate each element


ArrayList<String> setList = new ArrayList<>(new HashSet())
7

7. ArrayList as the return value of the method

Case analysis:

1. Use the ArrayList collection as the return value of the method

2. Use a large set to store 20 numbers, and filter even numbers into a small set


import java.util.ArrayList;
import java.util.Random;
public class ArrayListReturn {
    public static void main(String[] args) {    
        ArrayList<Integer> list = returnArrayList();
        System.out.println(list);
    }
    
 // Define the requirements method and set the return value type to ArrayList Set 
    public static ArrayList<Integer> returnArrayList(){
        Random r = new Random();
        // Defining a large set 
        ArrayList<Integer> listBig = new ArrayList<>();
        // Defining small collections 
        ArrayList<Integer> listSmall = new ArrayList<>();
        
        for (int i = 0; i < 20; i++) {
            int num = r.nextInt(10);
            // Adding data to a collection 
            listBig.add(num);
            // Whether the added data is even or not is judged, and if so, the data is stored in a small set 
            if(listBig.get(i)%2 == 0){
                listSmall.add(listBig.get(i));
            }
        }
        System.out.println(" Even number 1 Total: "+listSmall.size()+" A ");
        // The return value is a small collection 
        return listSmall;
    }
}

Related articles: