Java self study - common methods of ArrayList

Common methods of ArrayList

Step 1: add

add can be used in two ways:
The first is to add the object directly, adding the object at the end

heros.add(new Hero("hero " + i));

The second is to add objects at the specified location

heros.add(3, specialHero);

package collection;
 
import java.util.ArrayList;
 
import charactor.Hero;
 
public class TestCollection {
    public static void main(String[] args) {
        ArrayList heros = new ArrayList();
 
        // Add 5 objects to ArrayList
        for (int i = 0; i < 5; i++) {
            heros.add(new Hero("hero " + i));
        }
        System.out.println(heros);
 
        // Add object at specified location
        Hero specialHero = new Hero("special hero");
        heros.add(3, specialHero);
 
        System.out.println(heros.toString());
 
    }
 
}
package charactor;
  
public class Hero {
    public String name;
    public float hp;
  
    public int damage;
  
    public Hero() {
  
    }
  
    // Add a construction method to initialize name
    public Hero(String name) {
  
        this.name = name;
    }
  
    // Override toString method
    public String toString() {
        return name;
    }
  
}

Step 2: judge whether it exists

Determine whether an object is in the container through the methods contains
Judgment criteria: whether the object is the same, not whether the name is the same

package collection;
 
import java.util.ArrayList;
 
import charactor.Hero;
 
public class TestCollection {
    public static void main(String[] args) {
        ArrayList heros = new ArrayList();
 
        // Initialize 5 objects
        for (int i = 0; i < 5; i++) {
            heros.add(new Hero("hero " + i));
        }
        Hero specialHero = new Hero("special hero");
        heros.add(specialHero);
 
        System.out.println(heros);
        // Determine whether an object is in a container
        // Judgment criteria: whether the object is the same, not whether the name is the same
        System.out.print("Although a new object's name is also called hero 1,however contains The return is:");
        System.out.println(heros.contains(new Hero("hero 1")));
        System.out.print("But on specialHero Judgement, contains The return is:");
        System.out.println(heros.contains(specialHero));
    }
 
}

Step 3: get the object at the specified location

get the object at the specified location. If the input subscript is out of range, an error will also be reported

package collection;
 
import java.util.ArrayList;
 
import charactor.Hero;
 
public class TestCollection {
    public static void main(String[] args) {
        ArrayList heros = new ArrayList();
 
        // Initialize 5 objects
        for (int i = 0; i < 5; i++) {
            heros.add(new Hero("hero " + i));
        }
        Hero specialHero = new Hero("special hero");
        heros.add(specialHero);
         
        //Gets the object at the specified location
        System.out.println(heros.get(5));
        //If it goes beyond the scope, it will still report an error
        System.out.println(heros.get(6));
 
    }
 
}

Step 4: get the location of the object

indexOf is used to determine the position of an object in the ArrayList
As with contains, the criterion is whether the object is the same, not whether the name value of the object is the same

package collection;
 
import java.util.ArrayList;
 
import charactor.Hero;
 
public class TestCollection {
    public static void main(String[] args) {
        ArrayList heros = new ArrayList();
 
        // Initialize 5 objects
        for (int i = 0; i < 5; i++) {
            heros.add(new Hero("hero " + i));
        }
        Hero specialHero = new Hero("special hero");
        heros.add(specialHero);
 
        System.out.println(heros);
        System.out.println("specialHero Location:"+heros.indexOf(specialHero));
        System.out.println("New hero, but the name is\"hero 1\"Location:"+heros.indexOf(new Hero("hero 1")));
 
    }
}

Step 5: delete

Remove is used to remove objects from the ArrayList

remove can delete the elements of ArrayList according to the subscript

heros.remove(2);

It can also be deleted according to the object

heros.remove(specialHero);

package collection;
 
import java.util.ArrayList;
 
import charactor.Hero;
 
public class TestCollection {
    public static void main(String[] args) {
        ArrayList heros = new ArrayList();
 
        // Initialize 5 objects
        for (int i = 0; i < 5; i++) {
            heros.add(new Hero("hero " + i));
        }
        Hero specialHero = new Hero("special hero");
        heros.add(specialHero);
         
        System.out.println(heros);
        heros.remove(2);
        System.out.println("Delete object with subscript 2");
        System.out.println(heros);
        System.out.println("delete special hero");
        heros.remove(specialHero);
        System.out.println(heros);
         
    }
}

Step 6: replace

set is used to replace the element at the specified location

package collection;
 
import java.util.ArrayList;
 
import charactor.Hero;
 
public class TestCollection {
    public static void main(String[] args) {
        ArrayList heros = new ArrayList();
 
        // Initialize 5 objects
        for (int i = 0; i < 5; i++) {
            heros.add(new Hero("hero " + i));
        }
        Hero specialHero = new Hero("special hero");
        heros.add(specialHero);
         
        System.out.println(heros);
        System.out.println("Replace element with subscript 5 with\"hero 5\"");
        heros.set(5, new Hero("hero 5"));
        System.out.println(heros);
    }
}

Step 7: get the size

Size to get the size of the ArrayList

package collection;
 
import java.util.ArrayList;
 
import charactor.Hero;
 
public class TestCollection {
    public static void main(String[] args) {
        ArrayList heros = new ArrayList();
 
        // Initialize 5 objects
        for (int i = 0; i < 5; i++) {
            heros.add(new Hero("hero " + i));
        }
        Hero specialHero = new Hero("special hero");
        heros.add(specialHero);
        System.out.println(heros);
        System.out.println("Obtain ArrayList Size:");
        System.out.println(heros.size());
    }
}

Step 8: convert to array

toArray can convert an ArrayList object to an array.
It should be noted that if you want to convert to a Hero array, you need to pass an Object of Hero array type to toArray(), so that the toArray method can know which type of array you want to convert, otherwise you can only convert to an Object array

package collection;
 
import java.util.ArrayList;
 
import charactor.Hero;
 
public class TestCollection {
    public static void main(String[] args) {
        ArrayList heros = new ArrayList();
 
        // Initialize 5 objects
        for (int i = 0; i < 5; i++) {
            heros.add(new Hero("hero " + i));
        }
        Hero specialHero = new Hero("special hero");
        heros.add(specialHero);
        System.out.println(heros);
        Hero hs[] = (Hero[])heros.toArray(new Hero[]{});
        System.out.println("array:" +hs);
 
    }
}

Step 9: add all objects in another container

addAll adds all objects in another container

package collection;
 
import java.util.ArrayList;
 
import charactor.Hero;
 
public class TestCollection {
    public static void main(String[] args) {
        ArrayList heros = new ArrayList();
 
        // Initialize 5 objects
        for (int i = 0; i < 5; i++) {
            heros.add(new Hero("hero " + i));
        }
 
        System.out.println("ArrayList heros:\t" + heros);
          
        //Add all the elements in another container to the container
        ArrayList anotherHeros = new ArrayList();
        anotherHeros.add(new Hero("hero a"));
        anotherHeros.add(new Hero("hero b"));
        anotherHeros.add(new Hero("hero c"));
        System.out.println("anotherHeros heros:\t" + anotherHeros);
        heros.addAll(anotherHeros);
        System.out.println("Turn the other one. ArrayList All elements of are added to the current ArrayList:");
        System.out.println("ArrayList heros:\t" + heros);
         
    }
}

Step 10: empty

clear clears an ArrayList

package collection;
 
import java.util.ArrayList;
 
import charactor.Hero;
 
public class TestCollection {
    public static void main(String[] args) {
        ArrayList heros = new ArrayList();
 
        // Initialize 5 objects
        for (int i = 0; i < 5; i++) {
            heros.add(new Hero("hero " + i));
        }
 
        System.out.println("ArrayList heros:\t" + heros);
        System.out.println("Use clear empty");
        heros.clear();
        System.out.println("ArrayList heros:\t" + heros);
          
    }
}

Keywords: Java

Added by louisp on Wed, 06 Nov 2019 04:49:16 +0200