Dark horse programmer full set of Java tutorials_ Java basics tutorial_ Set basis

1.1 collection overview

  • When programming, if you want to store multiple data and use the array storage format with fixed length, it may not meet our needs and can not adapt to the changing needs. Then, how to choose at this time—— aggregate
  • Collection features: provide a storage model with variable storage space, and the storage data capacity can be changed.
  • There are many collection classes. At present, let's learn one: ArrayList. ArrayList < E >: implementation of resizable array< E > is a special data type, generic.
  • How to use ArrayList: where E appears, we can replace it with reference data type. For example, ArrayList < string >, ArrayList < student >.

1.2 construction method and addition method of ArrayList

Method nameexplain
public ArrayList()Create an empty collection object
public boolean add(E e)Appends the specified element to the end of this collection
public void add(int index, E element)Inserts the specified element at the specified location in this collection
    public static void main(String[] args) {
        //ArrayList<String> array = new ArrayList<String>();
        /*This is a new feature of JDK7. New ArrayList < > () identifies this as a collection of String data types according to the previously defined array*/
        ArrayList<String> array = new ArrayList<>();

        array.add("hello");
        array.add("world");
        array.add("java");

        /*Error: IndexOutOfBoundsException: the index of the collection is out of bounds*/
        //array.add(4, "javaee");
        array.add(1,"javase");

        System.out.println(array);
    }

1.3 common methods of ArrayList set

Method nameexplain
public boolean remove(Object o)Delete the specified element and return whether the deletion is successful
public E remove(int index)Deletes the element at the specified index and returns the deleted element
public E set(int index,E element)Modify the element of the specified index and return the modified element
public E get(int index)Returns the element at the specified index
public int size()Returns the number of elements in the collection
    public static void main(String[] args) {
        ArrayList<String> array = new ArrayList<>();
        array.add("hello");
        array.add("world");
        array.add("java");

        array.remove("hello");
        System.out.println(array);//Output: [world, java]

        array.remove(0);
        System.out.println(array);//Output: [java]

        array.set(0,"JAVA");
        System.out.println(array);//Output: [JAVA]

        array.get(0);
        System.out.println(array);//Output: [JAVA]

        System.out.println(array.size());//Output: 1
    }

Case 1: store string and traverse

  • Requirements: create a collection of stored strings, store 3 string elements, and enable the program to traverse the collection on the console
  • Idea:
    (1) Create a collection object.
    (2) Adds a string object to the collection.
    (3) To traverse the collection, you must first get each element in the collection, which is implemented through the get(int index) method.
    (4) Secondly, to get the length of the set, this is implemented through the size() method.
    (5) General format for traversing collections
	for(int i=0;i<Collection object.size();i++){
		Collection object.get(i)Is the element at the specified index
	}
public class ArrayListDemo {
    public static void main(String[] args) {
        ArrayList<String> array = new ArrayList<>();

        array.add("l");
        array.add("p");
        array.add("a");

        for (int i=0; i<array.size();i++){
            System.out.println(array.get(i));
        }
    }
}

Case 2: storing student objects and traversing

  • Requirements: create a collection for storing student objects, store 3 student objects, and use the program to traverse the collection on the console
  • Idea:
    (1) Define student classes.
    (2) Create a collection object.
    (3) Create a student object.
    (4) Add a student object to the collection.
    (5) Traversal set, using the general traversal format.

Student.java

public class Student {
    private String name;
    private int age;

    public Student(){}

    public Student(String name, int age){
        this.name=name;
        this.age=age;
    }

ArrayListDemo2.java

    public static void main(String[] args) {
        ArrayList<Student> array = new ArrayList<Student>();

        Student s1 = new Student("l",11);
        Student s2 = new Student("p",12);
        Student s3 = new Student("a",13);
        array.add(s1);
        array.add(s2);
        array.add(s3);

        for (int i=0;i<array.size();i++) {
            System.out.println(array.get(i));
        }
    }

Case 3: the keyboard stores student objects and traverses them

  • Requirements: create a set to store student objects, store 3 student objects, use the program to traverse the set on the console, and the student's name and age come from keyboard entry.
  • Idea:
    (1) Define the student class. For the convenience of keyboard data entry, the member variables of the student class are defined as String type.
    (2) Create a collection object.
    (3) Enter the data required by the student object with the keyboard.
    (4) Enter the student object with the keyboard, and assign the data entered with the keyboard to the member variable of the student object.
    (5) Add a student object to the collection.
    (6) Traversal set, using the general traversal format.
  • Student.java
public class Student {
    private String name;
    private String age;

    Student(){}

    public Student(String name, String age){
        this.name = name;
        this.age = age;
    }
    ......

ArrayTest.java

public class ArrayTest {
    public static void main(String[] args) {
        ArrayList<Student> array = new ArrayList<>();

        for (int i=0;i<3;i++){
            addStudent(array);
        }
        
        for (int i=0;i<array.size();i++){
            System.out.println(array.get(i).getName() + "," + array.get(i).getAge());
        }
    }

    private static void addStudent(ArrayList<Student> array){
        Scanner sc = new Scanner(System.in);

        Student s = new Student();

        System.out.println("Please enter the student's name and age:");
        s.setName(sc.nextLine());
        s.setAge(sc.nextLine());

        array.add(s);
    }
}

Keywords: Java JavaEE

Added by Base on Thu, 04 Nov 2021 13:27:40 +0200