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]
}
}
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]
}
}
System.out.println(animals.size()); // 3
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,
}
0 → Delete 1st Delete 1 → 2 Delete 2nd → 3rd ...etc
animals.remove(1);
System.out.println(animals); // [dog, pig]
animals.clear();
System.out.println(animals); // []
Recommended Posts