SET08112 ArrayList Interface Tutorial
In this tutorial you will use the ArrayList and the HashMap or TreeMap to solve some common problems. You will use methods such as:
Problem 1 - Sort a List
The file from http://www.dcs.napier.ac.uk/~cs66/SET08112/tut2/students.txt contains a list of students:
ADAMS, ROBERT HASTIE, MARTIN OTHIENO, JUDITH ANDERSON, ROSS ...
Create a programme SortStudents.java that reads in this file and outputs the students in Alphabetical order.
- You can use an ArrayList to hold the students.
- ArrayList<String> names = new ArrayList<String>();
- You can use java.util.Collections.sort to sort the ArrayList
Problem 2 - List Routes
The file year1.txt contains a list of all students in year one. Each line contains the name of the student and the name of the route (programme) they follow separated by a tab.
Brando, Marlon BSC (HONS) DIGITAL MEDIA F/T Bridges, Jeff BSC (HONS) INTERACTIVE MEDIA DESIGN F/T Brody, Adrien BENG (HONS) COMPUTER SECURITY AND FORENSICS F/T
Write a program to read in this file and output a list of routes with no duplicates. You can use a program very similar to the one above – but this time:
line.split("\t")to get the route
name.contains(st)to add the route only if it is not already in the ArrayList
- You do not need to sort before output
Problem 3 - Who's missing?
The file present.txt contains those students from students.txt who attended a particular tutorial. We want a list of all those students who were not present.
Brody, Adrien Irons, Jeremy Spacek, Sissy
How to do this…
- Create an array list called present
- Open the file present.txt and add every line to the ArrayList present
- Open the file student.txt
- For each line in the file – if that student is not in present print the name.
if (!present.contains(line)) System.out.println(line);