Run Application
Create new package named demo. In this package, create new java class named Main.java as below:
package demo;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
var names = new ArrayList<>();
names.add("name 1");
names.add("name 2");
names.add("name 3");
names.add("name 4");
System.out.println("Name List 1");
for (var name : names) {
System.out.println(name);
}
System.out.println("Name List 2");
for (var i = 0; i < names.size(); i++) {
System.out.println(names.get(i));
}
}
}
Output
Name List 1
name 1
name 2
name 3
name 4
Name List 2
name 1
name 2
name 3
name 4