About List [Java]

Initialization ① add

Main.java


package com.test;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> animals = new ArrayList<>();
        animals.add("dog");
        animals.add("cat");
        animals.add("pig");
        System.out.println(animals); // [dog, cat, pig]
    }
}

Initialization ② Arrays.asList

Main.java


package com.test;

import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> animals = Arrays.asList("dog", "cat", "pig");
        System.out.println(animals); // [dog, cat, pig]
    }
}

Returns the current number of elements size

System.out.println(animals.size()); // 3

Reference element by specifying index get (index)

0 → Refer to the first element See 1 → 2nd element See 2 → 3rd element ...etc

 for (int i = 0; i < animals.size(); i++) {
     System.out.print(animals.get(i) + ","); // dog,cat,pig,
 }

Remove by specifying index remove (index)

0 → Delete 1st Delete 1 → 2 Delete 2nd → 3rd ...etc

animals.remove(1);
System.out.println(animals); // [dog, pig]

Delete all elements clear

animals.clear();
System.out.println(animals); // []

Recommended Posts

About List [Java]
About Java Array List
[Java] About Java 12 features
[Java] About arrays
Java memorandum (list)
Clone Java List.
Something about java
Where about java
[Java] About interface
About Java class
About Java arrays
About java inheritance
About interface, java interface
About java var
About Java literals
About Java commands
About Java log output
About Java functional interface
[java] sort in list
About class division (Java)
About [Java] [StreamAPI] allMatch ()
About Java method binding
[Java] About anonymous classes
About method splitting (Java)
About Java Polymorphism super ()
About inheritance (Java Silver)
About Java String class
About Java access modifiers
About Java lambda expressions
About Java entry points
About Java 10 Docker support
Personal summary about Java
[Java] About enum type
All about Java programming
[Memo] Java Linked List
About java abstract class
A note about Java GC
What I researched about Java 8
[Java] List type / Array type conversion
About an instance of java
What I researched about Java 6
[Gradle] About Java plug-in tasks
About Java variable declaration statements
What I researched about Java 9
[Java] About try-catch exception handling
About Java class loader types
[Java Silver] About equals method
[Java] About String and StringBuilder
Java
[Java] List OS-dependent standard libraries
About =
About Alibaba Java Coding Guidelines
About Java class variables class methods
List aggregation in Java (Collectors.groupingBy)
About Java Packages and imports
About abstract classes in java
Java List Group, Sort, etc.
List data structure [Java / Scala]
[Android / Java] Learned about DataBinding
What I researched about Java 5
[Java] Delete the elements of List