Detailed explanation of ArrayList class in Java

  • 2021-07-09 08:01:40
  • OfStack

ArratList class: Container that holds the same 1 data type (only reference data type, because it actually holds addresses inside)

1. Import its package import java. util. ArratList

2. Create the object ArrayList < E > Object name = new ArrayList < > ();  

E: Generic data type. Data of the specified type is stored in the specified object name. It cannot be omitted. Reference data type is required

3. Use is the object name. Method (parameter may be none)

Note: When printing the object name, it is not an address, but a list like List 1 in python, which stores all data [Element 1, Element 2]. If there is no data, it is an empty list []

4. Common methods of ArrayList

Object name. add (element)//Adds the element of the specified data type to the container. The return value is of type boolean, so you don't need to accept the return value, because the element will be successfully added (tail added)

Object name. get (i)//Gets the element subscript to i in the container, and the return value is the data type stored in the container

Object name. size ()//Gets the number of elements in the container

Object name. remove (i)//Removes the element subscript to i, and the return value is the deleted data element

5. If you want to use ArrayList to store shaped, floating-point and char types, and < E > Can only be a reference data type, how about? Java wraps basic data types for us as classes and indirectly as references

int------ > Integer ArrayList < Integer > li=new Arraylist < > (); //That is, to store integer elements

char---- > Character ArrayList < Character > li=new Arraylist < > (); //That is, the char element is stored

float--- > Float, byte-- > Byte, double-- > Double, long-- > Long//Only int and char wrapper class names are special, and the rest can be capitalized

3 Demo familiar with its use

//Randomly generated numbers are added to containers (Random and ArrayList)

import java.util.ArrayList;
import java.util.Random;
/* The random generation range is [1,33] Add the number of to the dynamic array */
public class DemoArrayList {
 public static void main(String[] args) {
  Random r=new Random();
  ArrayList<Integer> list=new ArrayList<>();
  for (int i = 0; i < 6; i++) {
  for(int i=0;i<list.size();i++){

//Defines the method output container to output {element @ element @ element... element} in the specified format

import java.util.ArrayList;

// Define method output ArrayList With { Element @ Element } Format output 
public class DemoSecond {
 public static void main(String[] args) {
  ArrayList<String> li=new ArrayList<>();
 public static void mymethod(ArrayList<String> s){
  for(int i=0;i<s.size();i++){

//Put 20 random numbers from a large set, of which even numbers are put into a small set

import java.util.ArrayList;
import java.util.Random;
// Defines a method to set the 20 Random numbers, of which even numbers are put into small sets 
public class DemoTest {
 public static void main(String[] args) {
  ArrayList<Integer> biglist=new ArrayList<>();
  ArrayList<Integer> smalist=new ArrayList<>();
 public static void mythod(ArrayList<Integer> a,ArrayList<Integer> b){
  Random r=new Random();
  for (int i = 0; i < 20; i++) {
   int digit=r.nextInt(50);

Related articles: