Lists
Seoul National University of Science and Technology
Information Technology Management
Lecture slides index
May 6, 2025
import java.util.ArrayList;
at the top of the program.ArrayList<Type> list = new ArrayList<>()
, where Type is the type of the values to be stored in the list (e.g.String
).ArrayList
ArrayList
.ArrayList<String>
.new ArrayList<>();
.ArrayList<Integer>
; and a list that includes double-type variables is defined in the form ArrayList<Double>
.int
or double
hold their actual values.ArrayList
, in contrast, contain a reference to the location that contains the value(s) relating to that variable.
ArrayList
assumes that all the variables contained in it are reference types.int
variable into Integer
when one is added to a list, and the same occurs when a variable is retrieved from a list.double
-type variables, which are converted to Double
.Integer
-type variables, variables of type int
can also be added to it.ArrayList<Integer> integers = new ArrayList<>();
int integer = 1;
integers.add(integer);
ArrayList<Double> doubles = new ArrayList<>();
double d = 4.2;
doubles.add(d);
add
, which takes the value to be added as a parameter.get
, which is given the place of retrieval as a parameter.// import list so that the program can use it
import java.util.ArrayList;
public class WordListExample {
public static void main(String[] args) {
// create the word list for storing strings
ArrayList<String> wordList = new ArrayList<>();
// add two values to the word list
wordList.add("First");
wordList.add("Second");
// retrieve the value from position 0 of the word list, and print it
System.out.println(wordList.get(0));
}
}
The exercise template asks the user for strings and adds them to a list.
The program stops reading when the user enters an empty string.
The program then prints the first element of the list.
Your assignment is to modify the program so that instead of the first value, the third value on the list is printed.
Remember that programmers start counting from zero!
The program is allowed to malfunction if there are fewer than three entries on the list, so you don’t need to prepare for such an event at all.
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
while (true) {
String input = scanner.nextLine();
if (input.equals("")) {
break;
}
list.add(input);
}
System.out.println(list.get(0));
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> numbers = new ArrayList<>();
while (true) {
int number = Integer.valueOf(scanner.nextLine());
if (number == 0) {
break;
}
numbers.add(number);
}
System.out.println(numbers.get(0));
}
}
IndexOutOfBoundsException
error.main
method, whereupon ArrayList’s get
method was called.get
method of ArrayList called the rangeCheck
method, in which the error occurred.rangeCheck
method, we’d have good reason to guess that it checks if a searched place is contained within a given desired range.IndexOutOfBounds
.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// Modify this program to cause an indexOutOfBoundsException
ArrayList<String> lines = new ArrayList<>();
lines.add("Never has a man influenced physics so profoundly as Niels Bohr in the early 1900's");
lines.add("Going back to this time period, little was known about atomic structure; Bohr set out");
lines.add("to end the obscurity of physics. However, things didn't come easy for Bohr. He had to");
lines.add("give up most of his life for physics and research of many hypothesis. But, this is why");
lines.add("you and I have even heard of the quantum theory and atomic structures. Bohr came");
lines.add("up with his quantum theory while studying...");
System.out.println(lines.get(0));
}
}
i
.add
method of numbers
with 8 as parameter, the number 8 would be placed at index 6.get
with the parameter 4 the fifth number in the list would be retrieved.ArrayList<String> researchers= new ArrayList<>();
researchers.add("Madeline");
researchers.add("Mike");
researchers.add("Lawrence");
import
command.import java.util.ArrayList;
to the top of the program.import java.util.Scanner;
.ArrayList<String> professors = new ArrayList<>();
professors.add("Obregon");
professors.add("Kwon");
professors.add("Sim");
professors.add("Park");
System.out.println(professors.get(0));
System.out.println(professors.get(1));
System.out.println(professors.get(2));
System.out.println(professors.get(3));
int
), and it can be used as a part of an expression or stored in an integer variable for later use.ArrayList<String> list = new ArrayList<>();
System.out.println("Number of values on the list: " + list.size());
list.add("First");
System.out.println("Number of values on the list: " + list.size());
int values = list.size();
list.add("Second");
System.out.println("Number of values on the list: " + values);
Output
Note
Be sure to use the size
method of the list.
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
while (true) {
String input = scanner.nextLine();
if (input.equals("")) {
break;
}
list.add(input);
}
}
}
index
variable to keep track of the place that is to be outputted.ArrayList<String> teachers = new ArrayList<>();
teachers.add("Simon");
teachers.add("Samuel");
teachers.add("Ann");
teachers.add("Anna");
int index = 0;
if (index < teachers.size()) {
System.out.println(teachers.get(index)); // index = 0
index = index + 1; // index = 1
}
if (index < teachers.size()) {
System.out.println(teachers.get(index)); // index = 1
index = index + 1; // index = 2
}
if (index < teachers.size()) {
System.out.println(teachers.get(index)); // index = 2
index = index + 1; // index = 3
}
if (index < teachers.size()) {
System.out.println(teachers.get(index)); // index = 3
index = index + 1; // index = 4
}
if (index < teachers.size()) {
// this will not be executed since index = 4 and teachers.size() = 4
System.out.println(teachers.get(index));
index = index + 1;
}
if
statements into a while
loop that is repeated until the condition index < teachers.size()
no longer holds (i.e., the value of the variable index
grows too great).ArrayList<String> teachers = new ArrayList<>();
teachers.add("Simon");
teachers.add("Samuel");
teachers.add("Ann");
teachers.add("Anna");
int index = 0;
// Repeat for as long as the value of the variable `index`
// is smaller than the size of the teachers list
while (index < teachers.size()) {
System.out.println(teachers.get(index));
index = index + 1;
}
for
-loop, after which the program looks like this.i
:Integer
, and the value to be printed is stored in a variable called number
before printing.import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
while (true) {
String input = scanner.nextLine();
if (input.equals("")) {
break;
}
list.add(input);
}
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
while (true) {
String input = scanner.nextLine();
if (input.equals("")) {
break;
}
list.add(input);
}
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> numbers = new ArrayList<>();
while (true) {
int number = Integer.valueOf(scanner.nextLine());
if (number == -1) {
break;
}
numbers.add(number);
}
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> numbers = new ArrayList<>();
while (true) {
int number = Integer.valueOf(scanner.nextLine());
if (number == -1) {
break;
}
numbers.add(number);
}
}
}
Sample Output 1
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
while (true) {
int input = Integer.valueOf(scanner.nextLine());
if (input == -1) {
break;
}
list.add(input);
}
System.out.println("");
// implement finding the greatest number in the list here
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
while (true) {
int input = Integer.valueOf(scanner.nextLine());
if (input == -1) {
break;
}
list.add(input);
}
System.out.println("");
// implement here finding the indices of a number
}
}
Sample Output 1
Sample Output 1
Tip
ArrayList<String> teachers = new ArrayList<>();
teachers.add("Simon");
teachers.add("Samuel");
teachers.add("Ann");
teachers.add("Anna");
for (String teacher: teachers) {
System.out.println(teacher);
}
for (TypeOfVariable nameOfVariable: nameOfList)
, where TypeOfVariable
is the list’s element type, and nameOfVariable
is the variable that is used to store each value in the list as we go through it.import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
while (true) {
int input = Integer.valueOf(scanner.nextLine());
if (input == -1) {
break;
}
list.add(input);
}
System.out.println("");
// implement the calculation of the sum of the numbers in the list here
}
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// implement here a program, that first reads user input
// adding them on a list until user gives -1.
// Then it computes the average of the numbers on the list
// and prints it.
}
}
ArrayList<String> list = new ArrayList<>();
list.add("First");
list.add("Second");
list.add("Third");
list.remove(1);
System.out.println("Index 0 so the first value: " + list.get(0));
System.out.println("Index 1 so the second value: " + list.get(1));
Sample Output
remove
is the same type as the values in the list, but not an integer, (integers are used to remove from a given index), it can be used to remove a value directly from the list.ArrayList<String> list = new ArrayList<>();
list.add("First");
list.add("Second");
list.add("Third");
list.remove("First");
System.out.println("Index 0 so the first value: " + list.get(0));
System.out.println("Index 1 so the second value: " + list.get(1));
Sample Output
int
type parameter to the remove method.valueOf
method of the Integer class.ArrayList<Integer> list = new ArrayList<>();
list.add(15);
list.add(18);
list.add(21);
list.add(24);
list.remove(2);
list.remove(Integer.valueOf(15));
System.out.println("Index 0 so the first value: " + list.get(0));
System.out.println("Index 1 so the second value: " + list.get(1));
Sample Output
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(3);
numbers.add(1);
numbers.add(6);
numbers.add(0);
numbers.remove(3);
ArrayList<Integer> numbers= new ArrayList<>();
numbers.add(2);
numbers.add(6);
numbers.add(5);
numbers.add(3);
numbers.remove(Integer.valueOf(2));
true
or false
) that indicates whether or not that value is stored in the list.ArrayList<String> list = new ArrayList<>();
list.add("First");
list.add("Second");
list.add("Third");
System.out.println("Is the first found? " + list.contains("First"));
boolean found = list.contains("Second");
if (found) {
System.out.println("Second was found");
}
// or more simply
if (list.contains("Second")) {
System.out.println("Second can still be found");
}
Sample Output
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
while (true) {
String input = scanner.nextLine();
if (input.equals("")) {
break;
}
list.add(input);
}
}
}
print
prints the values in the list one by one.public static void print(ArrayList<String> list) {
for (String value: list) {
System.out.println(value);
}
}
print
method that was implemented above.print
, the name of the list variable is strings
, but inside the method print
the variable is called list
– the name of the variable that stores the list could also be printables
, for instance.printSmallerThan
methodArrayList<Integer> list = new ArrayList<>();
- list.add(1);
list.add(2);
list.add(3);
list.add(2);
list.add(1);
- printSmallerThan(list, 3);
Sample Output
public static void printNumbersInRange(ArrayList<Integer> numbers, int lowerLimit, int upperLimit)
in the exercise template.ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(3);
numbers.add(2);
numbers.add(6);
numbers.add(-1);
numbers.add(5);
numbers.add(1);
System.out.println("The numbers in the range [0, 5]");
printNumbersInRange(numbers, 0, 5);
System.out.println("The numbers in the range [3, 10]");
printNumbersInRange(numbers, 3, 10);
void
keyword, and the actual returning of the value is done by the return
command.
public static int sum(ArrayList<Integer> numbers)
in the exercise template.Earlier we have used integers, floating point numbers, etc. as method parameters.
When variables such as int
are used as method parameters, the value of the variable is copied for the method’s use.
The same occurs in the case that the parameter is a list.
Lists, among practically all the variables that can store large amounts of information, are reference-type variables.
This means that the value of the variable is a reference that points to the location that contains the information.
When a list (or any reference-type variable) is copied for a method’s use, the method receives the value of the list variable, i.e., a reference.
In such a case the method receives a reference to the real value of a reference-type variable, and the method is able to modify the value of the original reference type variable, such as a list.
In practice, the list that the method receives as a parameter is the same list that is used in the program that calls the method.
public static void removeFirst(ArrayList<Integer> numbers) {
if (numbers.size() == 0) {
return;
}
numbers.remove(0);
}
public static void removeLast(ArrayList<String> strings)
in the exercise template.ArrayList<String> exercises1 = new ArrayList<>();
ArrayList<String> exercises2 = new ArrayList<>();
exercises1.add("Ada Lovelace");
exercises1.add("Hello World! (Ja Mualima!)");
exercises1.add("Six");
exercises2.add("Adding a positive number");
exercises2.add("Employee's pension insurance");
System.out.println("The size of list 1: " + exercises1.size());
System.out.println("The size of list 2: " + exercises2.size());
System.out.println("The first value of the first list " + exercises1.get(0));
System.out.println("The last value of the second list " + exercises2.get(exercises2.size() - 1));
Each list is its own separate entity, and the list methods always concern the list that was used to call the method.
A summary of some list methods is provided below.
It’s assumed that the created list contains variables of type string.
Adding to a list is done with the method add
that receives the value to be added as a parameter.
size
; it returns an integer.get
that is given the index at which the value resides as a parameter.ArrayList<String> list = new ArrayList<>();
list.add("hello world!");
String string = list.get(0);
System.out.println(string);
remove
.contains
.Computer Language