The Java array iterates to remove the remove of sample code

  • 2020-04-01 02:22:59
  • OfStack

Without further ado, go straight to the code

package com.b; 

import java.util.ArrayList; 

//Array traverses delete, add
public class Core2 { 
    private String name; 
    private int num; 
    private String color; 

    public Core2() { 
    } 

    public Core2(String a, int b, String c) { 
        name = a; 
        num = b; 
        color = c; 
    } 

    public String getName() { 
        return name; 
    } 

    public String getColor() { 
        return color; 
    } 

    public int getNum() { 
        return num; 
    } 

    public static void main(String[] args) { 
        Core2 c = new Core2(" The little red ", 13, " red ");//Create three instances
        Core2 c2 = new Core2(" Xiao Ming ", 12, " yellow ");//The object
        Core2 c3 = new Core2(" Jack Bauer ", 123, " blue "); 
        Core2 c4 = new Core2(" small li", 23, " white "); 
        Core2 c5 = new Core2(" Little love ", 4, "blue"); 
        Core2 c6 = new Core2(" A small north ", 2, "brave"); 
        ArrayList aa = new ArrayList();//A collection of the container
        aa.add(c); 
        aa.add(c2); 
        aa.add(c3); 
        aa.add(c4); 
        // aa.remove(1); 
        aa.add(2, c5); 
        aa.add(1, c6); 
        aa.remove(c4); 
        System.out.println(" The size is: " + aa.size()); 
        for (int i = 0; i < aa.size(); i++) { 
            Core2 m = (Core2) aa.get(i);//Arraylist traversal and addition
            System.out.println(" The first " + (i + 1) + " Student names: " + m.getName() + ", Student number is: "
                    + m.getNum() + ", The color is: " + m.getColor()); 

        } 

    } 

} 
 The size is: 5
 The first 1 Student name: xiaohong , Student number is: 13, The color is: red  
 The first 2 Student name: small north , Student number is: 2, The color is: brave 
 The first 3 Student name: xiaoming , Student number is: 12, The color is: yellow  
 The first 4 Student name: xiao ai , Student number is: 4, The color is: blue 
 The first 5 Student name: jack Bauer , Student number is: 123, The color is blue 

Related articles: