Lists

This page is under construction. Please come back later.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.pureprogrammer.Utils;
public class Lists1 {
public static void main(String[] args) {
final int NUM_SQUARES = 5;
List<Integer> squares = new ArrayList<>();
// Put the squares into the list
for (int i = 0; i < NUM_SQUARES; ++i) {
squares.add(i * i);
}
// Print out the squares from the list
for (int i = 0; i < squares.size(); ++i) {
System.out.println(Utils.format("{0:d}^2 = {1:d}", i, squares.get(i)));
}
System.out.println(Utils.listToString(squares));
}
}
Output
$ javac -Xlint Lists1.java
$ java -ea Lists1
0^2 = 0
1^2 = 1
2^2 = 4
3^2 = 9
4^2 = 16
[0, 1, 4, 9, 16]
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.pureprogrammer.Utils;
public class Lists2 {
public static void main(String[] args) {
List<Integer> squares = Utils.listFromIntegers(new int[]{0, 1, 4, 9, 16, 25});
// Print out the squares from the list
for (int i = 0; i < squares.size(); ++i) {
System.out.println(Utils.format("{0:d}^2 = {1:d}", i, squares.get(i)));
}
}
}
Output
$ javac -Xlint Lists2.java
$ java -ea Lists2
0^2 = 0
1^2 = 1
2^2 = 4
3^2 = 9
4^2 = 16
5^2 = 25
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.pureprogrammer.Utils;
public class Lists3 {
public static void main(String[] args) {
List<String> names = Utils.listFromStrings(new String[]{"Fred", "Wilma", "Barney", "Betty"});
for (String name : names) {
System.out.println(Utils.join("", "Hello, ", name, "!"));
}
names = Arrays.asList("Harry", "Ron", "Hermione");
for (String name : names) {
System.out.println(Utils.join("", "Hello, ", name, "!"));
}
}
}
Output
$ javac -Xlint Lists3.java
$ java -ea Lists3
Hello, Fred!
Hello, Wilma!
Hello, Barney!
Hello, Betty!
Hello, Harry!
Hello, Ron!
Hello, Hermione!
import org.pureprogrammer.Utils;
public class Lists4 {
public static void main(String[] args) {
// Print out the command line arguments
for (int i = 1; i <= args.length; ++i) {
System.out.println(Utils.join("", i, ":", args[i - 1]));
}
}
}
Output
$ javac -Xlint Lists4.java
$ java -ea Lists4 Fred Barney Wilma Betty
1:Fred
2:Barney
3:Wilma
4:Betty
$ javac -Xlint Lists4.java
$ java -ea Lists4 10 20 30 40
1:10
2:20
3:30
4:40
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.pureprogrammer.Utils;
public class Lists5 {
public static void main(String[] args) {
List<String> names = Utils.listFromStrings(new String[]{"Fred", "Wilma", "Barney", "Betty"});
// Print out the name based on command line argument 1-4
for (int i = 1; i <= args.length; ++i) {
final int x = Utils.stoiWithDefault(args[i - 1], 0);
System.out.println(Utils.join("", i, ":", names.get(x - 1)));
}
}
}
Output
$ javac -Xlint Lists5.java
$ java -ea Lists5 1 2 3 4
1:Fred
2:Wilma
3:Barney
4:Betty
$ javac -Xlint Lists5.java
$ java -ea Lists5 4 3 2 1
1:Betty
2:Barney
3:Wilma
4:Fred
$ javac -Xlint Lists5.java
$ java -ea Lists5 1 3
1:Fred
2:Barney
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Array Last Element
- Central Limit Theorem
- Chromatic Scale Frequencies
- Income Tax (Revisited)
- Morse Code
- Renard Numbers (Preferred Numbers)
- Sample Mean and Standard Deviation
- Sieve of Eratosthenes
References
- [[Java Language Specification]], Java SE 21 Edition, Gosling, et. al., 2023.
- [[Java Tutorials]]
- [[Java at TutorialsPoint]]
- Download Java at [[Amazon Corretto]], [[Azul Zulu]], [[Eclipse Temurin]] or [[Oracle JDK]]
Pure Programmer


