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++) {
   list.add(r.nextInt(33)+1);
  }
  for(int i=0;i<list.size();i++){
   System.out.println(list.get(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<>();
  mymethod(li);
 }
 public static void mymethod(ArrayList<String> s){
  s.add("hello");
  s.add("da");
  s.add("nhao");
  System.out.print("{");
  for(int i=0;i<s.size();i++){
   if(i==s.size()-1){
    System.out.print(s.get(i)+"}");
   }
   else
    System.out.print(s.get(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<>();
  mythod(biglist,smalist);
 }
 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);
   a.add(digit);
   if(a.get(i)%2==0){
    b.add(digit);
   }
  }
  System.out.println(a);
  System.out.println(b);
 }
}

Related articles: