web123456

Basic use of Array List collection

  • package ArrayListDemo;
  • import ;
  • public class ArrayListDemo1 {
  • public static void main(String[] args) {
  • //1. Create a collection object
  • // Generics: Specify the type of data stored in the collection
  • /*
  • * boolean add(E e)
  • * boolean remove(E e)
  • * E remove(int index, E e)
  • * E set(int index , E e) Modify
  • * E get(int index) query
  • * int size Get length
  • *
  • * */
  • ArrayList<String> list = new ArrayList<String>();
  • //Add elements
  • ("123");
  • ("csac");
  • //Delete the element
  • // boolean remove = ("123");
  • // (remove);
  • //Index deletion will delete elements that have been deleted on the index
  • // String remove = (0);
  • // (remove);
  • //Modify elements
  • // String set = (1, "aaa");
  • // (set);
  • //Query a single element
  • // String s = (0);
  • // (s);
  • for (int i = 0; i < (); i++) {
  • String s = (i);
  • (s);
  • }
  • // (list);
  • }
  • }