Lists in Java
In this post, you will learn how to work with lists so that you can use that same knowledge and thus evolve.
- a list is an ordered set of values and is much easier to manipulate than an array;
- the values that make up a list are called elements (just like in arrays).
package com.caffeinealgorithm.programminginjava;
import java.util.ArrayList;
import java.util.List;
public class Lists {
public void Run() {
List<String> colors = new ArrayList<>();
colors.add("Blue");
colors.add("Green");
colors.add("Yellow");
colors.add("Red");
colors.add("Orange");
colors.remove("Orange");
// colors.clear();
System.out.printf("Number of colors: %d\n", colors.size()); // Number of colors: 4
System.out.printf("First color: %s\n", colors.get(0)); // First color: Blue
System.out.printf("Last color: %s", colors.get(colors.size() - 1)); // Last color: Red
}
}
Don’t forget to watch the video and you can always read this post in Portuguese.