Collection ArrayList in Java

Keywords: Java arraylist

catalogue

Collection overview

ArrayList construction method and addition method

common method

Store the string and traverse the case

Store student objects and traverse

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? That's the assembly

First recall the set concept in python: set set s = {1,2,3} is a data structure without duplicate elements. There is no order and no index. The elements can be numbers, strings and not list dictionary tuples

The collection in java is different. The characteristics of its collection class: it provides a storage model with variable storage space, and the stored data capacity can be changed. At present, let's learn one: ArrayList

ArrayList<E> :

  • Resizable array implementation
  • <E> : is a special data type, generic.

Where E appears, we can replace it with the reference data type, for example: ArrayList < string >, ArrayList < student >

ArrayList construction method and addition method

Create an empty collection object as follows, and use the add method to add the specified element to the collection

package com.test;

import com.sun.xml.internal.org.jvnet.fastinfoset.sax.FastInfosetReader;

import java.util.ArrayList;
import java.util.Scanner;

public class ScannerDemo {
    public static void main(String[] args) {
        //Create an empty collection object
        ArrayList<String> array = new ArrayList<String>();
        System.out.println(array); //Output []

        //Public Boolean add (E) appends the specified element to the end of the collection
        System.out.println(array.add("hello")); //Output true
        array.add(" world");
        System.out.println(array);   //Output [Hello, world]

        //Adds an element at the specified index location, starting at 0
        array.add(1,"nihao");
        System.out.println(array);  //Output [Hello, nihao, world]
    }
}

common method

  • arr.remove("element")       Deletes the specified element
  • array.remove(1)           Delete element at index position 1
  • array.set(1, "hello")     Set the value at index 1   Replace element with "hello"     
  • array.size()                   Get collection length
  • array.get(1)                   Gets the element at index 1
package com.test;

import com.sun.xml.internal.org.jvnet.fastinfoset.sax.FastInfosetReader;

import java.util.ArrayList;
import java.util.Scanner;

public class ScannerDemo {
    public static void main(String[] args) {
        //Create an empty collection object
        ArrayList<String> array = new ArrayList<String>();
        //Add elements to the collection
        array.add("hello");
        array.add("world");
        System.out.println(array);  //Output [hello, world]
        //1. ramove deletes the specified element
        /*array.remove("world");
        System.out.println(array);  //Output hello*/

        //2. remove deletes the element at the specified index position
        /*array.remove(1);
        System.out.println(array);  //Output hello*/

        //3. set modifies the element at the specified index
        array.set(1,"Hello");
        System.out.println(array);  //Output [hello]
    }
}

Store the string and traverse the case

Requirements: create a collection of stored strings, store 3 string elements, and use the program to traverse the collection on the console

package com.test;

import com.sun.xml.internal.org.jvnet.fastinfoset.sax.FastInfosetReader;

import java.util.ArrayList;

public class ScannerDemo {
    public static void main(String[] args) {
        //Create an empty collection object
        ArrayList<String> array = new ArrayList<String>();
        array.add("hello");
        array.add("world");
        array.add("hehe");
        System.out.println(array);
        for(int i=0; i<array.size(); i++){
            System.out.println(array.get(i));
        }
    }
}

Store student objects and traverse  

Requirements: create a collection for storing student objects, store 3 student objects, and use the program to traverse the collection on the console

Idea:

  • Define student classes
  • Create collection object
  • Create student object
  • Add student object to collection
  • Traversal set, using the general traversal format

Because you want to store student objects, you must first have student classes

Student.java

package com.test;
//Student class
public class Student {
    private String name;
    private int age;
    //Define parameterless construction method
    public Student(){}
    //Parametric construction method
    public Student(String name, int age){
        this.name = name;
        this.age  = age;
    }

    public String getName(){
        return name;
    }

    public int getAge(){
        return age;
    }
}

ArrayListTest.java

package com.test;
import java.util.ArrayList;

public class ArrayListTest {
    public static void main(String[] args) {
        //Create collection object
        ArrayList<Student> array = new ArrayList<Student>();
        //Create student object
        Student s1 = new Student("Chen Chen", 18);
        Student s2 = new Student("Xiao Ming", 19);
        Student s3 = new Student("Xiao Hong", 20);

        //Add student object to collection
        array.add(s1);
        array.add(s2);
        array.add(s3);
        //Traversal set
        for(int i=0; i<array.size(); i++){
             Student s = array.get(i);
            System.out.println(s.getName()+","+s.getAge());
        }
    }
}

Posted by JustGotAQuestion on Tue, 07 Sep 2021 18:34:13 -0700