1. The requirements for defining a TestList class are as follows (1) Use the List interface and generics to create an ArrayList object list, which is required to be of String type; (2) Add "China", "Usa", "India", "J

1. The requirements for defining a TestList class are as follows
(1) Use the List interface and generics to create an ArrayList object list, which is required to be of String type;
(2) Add "China", "Usa", "India", and "Japan" to the list String;
(3) Print out the list
(4) Traverse the list elements in 3 ways
(5) Delete the element with index 2, and then print out the list;

package cn.edu.ahtcm.bean;
import java.util.*;
public class Testlist {
    
    

    public static void main(String[] args){
    
    
        ArrayList<String> list = new ArrayList<>();
        list.add("China");
        list.add("Usa");
        list.add("India");
        list.add("Japan");
        Collections.sort(list);
        System.out.println(list);
        for (String i :list){
    
    
            System.out.println(i);
        }
        for (int i = 0; i < list.size();i++){
    
    
            System.out.println(list.get(i));
        }
        String a;
        for (String value : list){
    
    
            a = value;
            System.out.println(a);
        }
        list.remove(2);
        System.out.println(list);
    }

}

Guess you like

Origin blog.csdn.net/winds_tide/article/details/117133991