Copy this fragment to the run() method of an exercise in IntelliJ.
Try to run it.
// add this to the imports, otherwise Java won't recognize the ArrayList type. // import java.util.ArrayList; // create a new variable "listOfNames", and assign a new, empty list of strings to this variable ArrayList<String> listOfNames = new ArrayList<>(); // add two different names listOfNames.add("Alper"); listOfNames.add("Muhammed"); // show the names SaxionApp.printLine("The first name in the list is " + listOfNames.get(0)); SaxionApp.printLine("The second name is " + listOfNames.get(1));
Change the code so your own name is also added to the list, and make a second change that your own name is also printed to the screen.
ArrayList<String> listOfNames = new ArrayList<>();
listOfNames.add("Alper");
listOfNames.add("Muhammed");
listOfNames.add("Joris"); // add another name
SaxionApp.printLine("The first name in the list is " + listOfNames.get(0));
SaxionApp.printLine("The second name is " + listOfNames.get(1));
SaxionApp.printLine("The third name is " + listOfNames.get(2)); // show the third name with .get(2).
It is only possible to get items from the list when they were first added to the list.
Try to add a printLine statement that gets the 4th name on the list, using .get(3). What happens?
ArrayList<String> listOfNames = new ArrayList<>();
// three names
listOfNames.add("Alper");
listOfNames.add("Muhammed");
listOfNames.add("Joris");
SaxionApp.printLine("The first name in the list is " + listOfNames.get(0));
SaxionApp.printLine("The second name is " + listOfNames.get(1));
SaxionApp.printLine("The third name is " + listOfNames.get(2));
// the application crashes on listOfNames.get(3) and will not execute the printLine:
SaxionApp.printLine("The fourth name is " + listOfNames.get(3));
Remove the crashing line again.
It is also possible to use a variable to add values to the list. Ask the user to enter their own name and add that name to the list. Also show that name on screen afterwards.
ArrayList<String> listOfNames = new ArrayList<>();
listOfNames.add("Alper");
listOfNames.add("Muhammed");
listOfNames.add("Joris");
// ask user for their name and store it in the variable userName
SaxionApp.print("Please enter your name: ");
String userName = SaxionApp.readString();
listOfNames.add(userName); // add the contents of the variable to the list
SaxionApp.printLine("The first name in the list is " + listOfNames.get(0));
SaxionApp.printLine("The second name is " + listOfNames.get(1));
SaxionApp.printLine("The third name is " + listOfNames.get(2));
SaxionApp.printLine("Your name is " + listOfNames.get(3)); // show the 4th name on screen
This is not very efficient, to show each name separately. We can use a loop to accomplish this. Copy this code into your run method and make it work.
ArrayList<String> listOfNames = new ArrayList<>(); listOfNames.add("Alper"); listOfNames.add("Muhammed"); listOfNames.add("Joris"); listOfNames.add("Maurice"); int i = 0; while (i < 4) { String name = listOfNames.get( ); // change this line to make it work SaxionApp.printLine("Name no " + i + " is " + name); i++; }
ArrayList<String> listOfNames = new ArrayList<>();
listOfNames.add("Alper");
listOfNames.add("Muhammed");
listOfNames.add("Joris");
listOfNames.add("Maurice");
int i = 0;
while (i < 4) {
String name = listOfNames.get(i); // i is used to get each name
SaxionApp.printLine("Name no " + i + " is " + name);
i++;
}
Now change the program, so there is not a fixed list of names: remove the four lines that add the names. Instead, use an endless while loop to ask for the name and add it to this list.
Copy the following code into your run method and add code to the if statement that adds the name to the listOfNames list. Try to run it: it will work, but it also crashes if there are less than four names.
ArrayList<String> listOfNames = new ArrayList<>(); while (true) { SaxionApp.print("Please enter a name (or empty to stop): "); String name = SaxionApp.readString(); if (name.length() > 0) { // <--- add your code here. } else { break; } } int i = 0; while (i < 4) { String name = listOfNames.get(i); SaxionApp.printLine("Name no " + i + " is " + name); i++; }
ArrayList<String> listOfNames = new ArrayList<>();
while (true) {
SaxionApp.print("Please enter a name (or empty to stop): ");
String name = SaxionApp.readString();
if (name.length() > 0) {
listOfNames.add(name); // add the name to the list
} else {
break;
}
}
int i = 0;
while (i < 4) {
String name = listOfNames.get(i);
SaxionApp.printLine("Name no " + i + " is " + name);
i++;
}
Why does it crash when having less than four names?
int i = 0;
while (i < 4) { // <--- because the loop always assumes four names here
// ...and here it tries to get an element from this list
// using .get(i), that may not exist. The app will then crash.
String name = listOfNames.get(i);
SaxionApp.printLine("Name no " + i + " is " + name);
i++;
}
Instead of always assuming four, we should change the application, so it will do the loop as many times as needed.
With listOfNames.size()
we can get the number of items on the list. Change the code so it uses the list size in the loop.
ArrayList<String> listOfNames = new ArrayList<>();
while (true) {
SaxionApp.print("Please enter a name (or empty to stop): ");
String name = SaxionApp.readString();
if (name.length() > 0) {
listOfNames.add(name);
} else {
break;
}
}
int i = 0;
while (i < listOfNames.size()) {
String name = listOfNames.get(i);
SaxionApp.printLine("Name no " + i + " is " + name);
i++;
}
Create a new application that adds a few names to a new list. Show on screen how many items the list contains. Ask the user which item they want to see from the list. Show that item on screen. If the user requests an item that does not exist, show an error and ask again. Example:
The list with names contains 4 items. Which item do you want to see (starting with 0)? > 1 Item number 1 is: Metesayit
ArrayList<String> listOfNames = new ArrayList<>();
listOfNames.add("Berke");
listOfNames.add("Metesayit");
listOfNames.add("Luuk");
SaxionApp.printLine("The list of names contains " + listOfNames.size() + " items.");
SaxionApp.printLine("Which item do you want to see (starting with 0)?");
int chosenItem = SaxionApp.readInt();
String chosenName = listOfNames.get(chosenItem);
SaxionApp.printLine("Item number " + chosenItem + " is: " + chosenName);
Change the code so the user can only choose an item that actually exists, and otherwise gets an error message.
ArrayList<String> listOfNames = new ArrayList<>();
listOfNames.add("Berke");
listOfNames.add("Metesayit");
listOfNames.add("Luuk");
SaxionApp.printLine("The list of names contains " + listOfNames.size() + " items.");
SaxionApp.printLine("Which item do you want to see?");
int chosenItem = SaxionApp.readInt();
// because ArrayList index starts with 0, the choseItem has to be smaller than
// the total item count (if size() is 3, then only items 0 1 and 2 exist).
if (chosenItem > 0 && chosenItem < listOfNames.size()) {
String chosenName = listOfNames.get(chosenItem);
SaxionApp.printLine("Item number " + chosenItem + " is: " + chosenName);
} else {
SaxionApp.printLine("The chosen item is incorrect.");
}
It is possible to remove an name with the .remove()
method, for example: listOfNames.remove("Peter")
.
Change the code so it removes the chosen item from the list.
After removing the item, add a loop that shows all items on the list.
ArrayList<String> listOfNames = new ArrayList<>();
listOfNames.add("Lyon");
listOfNames.add("Melih");
listOfNames.add("Nigel");
SaxionApp.printLine("The list of names contains " + listOfNames.size() + " items.");
SaxionApp.printLine("Which item do you want to remove?");
int chosenItem = SaxionApp.readInt();
if (chosenItem > 0 && chosenItem < listOfNames.size()) {
String chosenName = listOfNames.get(chosenItem);
SaxionApp.printLine("Item " + chosenName + " will be removed");
listOfNames.remove(chosenName); // here the item is removed from the list
// or: listOfNames.remove(chosenItem); // this is also correct
} else {
SaxionApp.printLine("The chosen item is incorrect.");
}
SaxionApp.printLine();
SaxionApp.printLine("The list is now:");
// a standard loop to show the contents of a list
int i = 0;
while (i < listOfNames.size()) {
SaxionApp.printLine(listOfNames.get(i));
i++;
}
Create a program that asks numbers in an endless loop until the user enters the number 0. In the loop, add the numbers to a list of numbers. Note: a list of numbers can be created using ArrayList<Integer>
.
Print the numbers afterwards.
ArrayList<Integer> numbers = new ArrayList<>();
while (true) {
int nr = SaxionApp.readInt();
if (nr == 0) {
break;
}
numbers.add(nr);
}
int i = 0;
while (i < numbers.size()) {
SaxionApp.printLine(numbers.get(i));
i++;
}
Iterating over a list to show all items in the list is something that is done very often. Java has an easier way of doing this, the enhanced for-loop. It looks like this for a ArrayList of String values:
for (String name : listOfNames) { SaxionApp.printLine(name); }
Change your program, so it uses the enhanced for loop instead of the while
.
ArrayList<Integer> numbers = new ArrayList<>();
while (true) {
int nr = SaxionApp.readInt();
if (nr == 0) {
break;
}
numbers.add(nr);
}
for (int number : numbers) {
SaxionApp.printLine(number);
}
Change the code, so instead of printing all the numbers afterwards, the program shows the sum of all the numbers combined.
ArrayList<Integer> numbers = new ArrayList<>();
while (true) {
int nr = SaxionApp.readInt();
if (nr == 0) {
break;
}
numbers.add(nr);
}
int sum = 0;
for (int number : numbers) {
SaxionApp.printLine(number);
sum = sum + number; // or sum += number
}
SaxionApp.print("The total is: " + sum);