Creating and using the ArrayList class

Posted by rcity on Tue, 21 Jan 2020 07:45:25 +0100

Class ArrayList

What is the ArrayList class

ArrayList is a variable length array that can be implemented to store data inside called elements. This class provides methods to manipulate elements stored internally. Elements can be added continuously in ArrayList, and their size grows automatically.

We know that the defined array length can't be changed, and ArrayList has overcome this point. You can add elements and lengthen the length at any time as needed.

The introduction of JDK? API is shown in the figure below:

How to use ArrayList

Three steps: Guide Package - create - use
Import Java. Util. ArrayList < E >
< E > represents a specified data type, called a generic.
Generics can only be reference types, not basic data types
E. Initial from Element. Where e appears, we can replace it with a reference data type, indicating which reference type of elements we will store.
For ArrayList, there is an angle bracket < E > for generics.
Generics: that is to say, all elements in a collection are of the same type.
Note: generics can only be reference types, not basic types.
matters needing attention:
For the ArrayList collection, what you print directly is not the address value, but the content.
If the content is empty, you get empty brackets: []

Sample code for creating reference of import package:

import java.util.ArrayList;
public class ArrayTestTest {

	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<>();
		System.out.println(list);
		
		list.add("Zhao Liying");
		System.out.println(list);
		
		list.add("Zhang San");
		list.add("Li Si");
		System.out.println(list);

	}
}

The output results are shown in the figure:

add(100) is wrong in this code because the type in angle brackets is String when creating ArrayList. In the list object, only String class can be stored.

Common methods of ArrayList

add() get() remove() size()

add() method, add an element
get(n) method, get element n
remove(n) method to remove element n
size() method, which returns the number of elements in the collection

Example code:

import java.util.ArrayList;
public class ArrayTestTest {

	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<>();
		System.out.println(list);
		
		list.add("Zhao Liying");
		System.out.println(list);
		
		list.add("Zhang San");
		list.add("Li Si");
		System.out.println(list);
		
		String name = list.get(1);
		System.out.println(name);
		
		String whoRemoved = list.remove(2);
		System.out.println("The removed element is:"+whoRemoved);
		System.out.println(list);
		
		int size = list.size();
		System.out.println("The length of the set is:"+size);
	}

}

Output results:

Traverse all elements

Traverse with for loop, size() method
The code is as follows:

import java.util.ArrayList;
public class ArrayTestTest {

	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<>();
		list.add("Zhao Liying");
		list.add("Zhang San");
		list.add("Li Si");
		list.add("Li Wu");
		list.add("Li Liu");
		list.add("Li Qi");
		list.add("Li ba");
		list.add("Li 9");
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		}
	}
}

Storing basic data types with ArrayList

The basic data type is stored in the object of the ArrayList class, and the wrapper class corresponding to the basic data type must be used. Write as follows:

		ArrayList<int> arrayA = new ArrayList<>();		

Generics cannot be directly written as int, long, float, etc. they need to be written as wrapper classes. The wrapper class is a reference type, which is available in the java.lang package.
The corresponding relationship between different basic data types and packaging types is as follows:

Basic types Packaging type
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Here, we need to pay special attention to the packing types of int and char, which are Integer and Character respectively. Other initials are big enough

Code example:

import java.util.ArrayList;
public class ArrayTestTest {

	public static void main(String[] args) {
		ArrayList<Integer> listA = new ArrayList<>();
		listA.add(100);
		listA.add(200);
		listA.add(300);
		System.out.println(listA);
		
		System.out.println(listA.get(1));
	}
}

The output results are as follows:

Custom generics

Generics in ArrayList can be defined as objects generated by their own defined classes.

Here you customize a Bean type and reference it in the ArrayList.
The code is as follows:

public class Bean {
	private String name;
	private int age;
	
	public Bean() {
		
	}
	public Bean(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;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

The Bean class is referenced in another class

import java.util.ArrayList;
public class ArrayTestTest {

	public static void main(String[] args) {
		ArrayList<Bean> listA = new ArrayList<>();
		
		Bean one = new Bean("Zhang San",20);
		Bean two = new Bean("Li Si",30);
		Bean three = new Bean("Wang Wu",16);
		
		listA.add(one);
		listA.add(two);
		listA.add(three);
		
		for(int i=0;i<listA.size();i++) {
			System.out.println("Full name:"+ listA.get(i).getName() + "Age:" + listA.get(i).getAge());
		}
	}

The output results are as follows:

-------

Published 40 original articles, won praise 3, visited 4870
Private letter follow

Topics: Java JDK