Difference between revisions of "ArrayList Tutorial"

From Progzoo
Jump to: navigation, search
(Problem 1 - Sort a List)
(ArrayList Interface Tutorial)
 
(16 intermediate revisions by the same user not shown)
Line 1: Line 1:
==SET08112 ArrayList Interface Tutorial==
+
==ArrayList Interface Tutorial==
In this tutorial you will use the ArrayList and the HashMap or TreeMap to solve some common problems.
+
In this tutorial you will use the ArrayList - these exercises are designed to get you started.
 
You will use methods such as:
 
You will use methods such as:
 
* java.util.Collections.sort
 
* java.util.Collections.sort
Line 6: Line 6:
 
* get
 
* get
  
==Problem 1 - Sort a List==
+
==Sort a List==
 
<question className="SortStudents" title="Using Sort" copyfile="students.txt">
 
<question className="SortStudents" title="Using Sort" copyfile="students.txt">
 
The file  from http://progzoo.net/students.txt  contains a list of students:
 
The file  from http://progzoo.net/students.txt  contains a list of students:
  Andrews, Julie
+
  Whitaker, Forest
  Arliss, George
+
  Winslet, Kate
  Bancroft, Anne
+
  De Havilland, Olivia
  Barrymore, Lionel
+
  Donat, Robert
  Bates, Kathy
+
  Douglas, Michael
  Baxter, Warner
+
  Dressler, Marie
 
  ...
 
  ...
 
Create a programme SortStudents.java that reads in this file and outputs the students in Alphabetical order.
 
Create a programme SortStudents.java that reads in this file and outputs the students in Alphabetical order.
Line 63: Line 63:
 
</question>
 
</question>
  
==Problem 2 - List Routes==
+
==List Routes==
 
<question className="SortStudents" title="Using Sort" copyfile="year1.txt">
 
<question className="SortStudents" title="Using Sort" copyfile="year1.txt">
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.
+
The file http://progzoo.net/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
 
  Brando, Marlon BSC (HONS) DIGITAL MEDIA F/T
 
  Bridges, Jeff BSC (HONS) INTERACTIVE MEDIA DESIGN F/T
 
  Bridges, Jeff BSC (HONS) INTERACTIVE MEDIA DESIGN F/T
Line 118: Line 118:
 
</answer>
 
</answer>
 
</question>
 
</question>
==Problem 3 - Who's missing?==
+
 
 +
==Who's missing?==
 
<question className="SortStudents" title="Using Sort" copyfile="students.txt present.txt">
 
<question className="SortStudents" title="Using Sort" copyfile="students.txt present.txt">
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.
+
The file http://progzoo.net/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
 
  Brody, Adrien
 
  Irons, Jeremy
 
  Irons, Jeremy
Line 128: Line 129:
 
*Create an array list called present
 
*Create an array list called present
 
*Open the file present.txt and add every line to the ArrayList present
 
*Open the file present.txt and add every line to the ArrayList present
*Open the file student.txt
+
*Open the file students.txt
 
*For each line in the file – if that student is not in present print the name.
 
*For each line in the file – if that student is not in present print the name.
 
  if (!present.contains(line)) System.out.println(line);
 
  if (!present.contains(line)) System.out.println(line);
<prog>
+
<prog><![CDATA[
 
import java.util.ArrayList;
 
import java.util.ArrayList;
 
import java.io.BufferedReader;
 
import java.io.BufferedReader;
Line 138: Line 139:
 
public class SortStudents{
 
public class SortStudents{
 
   public static void main(String[]argv) throws Exception{
 
   public static void main(String[]argv) throws Exception{
     ArrayList&lt;String> names = new ArrayList&lt;String>();
+
     ArrayList<String> present = new ArrayList<String>();
 
     String line;
 
     String line;
 
     BufferedReader fh
 
     BufferedReader fh
 
       = new BufferedReader(new FileReader("present.txt"));
 
       = new BufferedReader(new FileReader("present.txt"));
 
     while (null!=(line=fh.readLine())){
 
     while (null!=(line=fh.readLine())){
       names.add(line);
+
       present.add(line);
 +
    }
 +
    fh.close();
 +
    fh = new BufferedReader(new FileReader("students.txt"));
 +
    while (null!=(line=fh.readLine())){
 +
      System.out.println(line);
 
     }
 
     }
 
   }
 
   }
 
}
 
}
</prog>
+
]]></prog>
<answer>
+
<answer><![CDATA[
 
import java.util.ArrayList;
 
import java.util.ArrayList;
 
import java.io.BufferedReader;
 
import java.io.BufferedReader;
Line 155: Line 161:
 
public class SortStudents{
 
public class SortStudents{
 
   public static void main(String[]argv) throws Exception{
 
   public static void main(String[]argv) throws Exception{
     ArrayList&lt;String> names = new ArrayList&lt;String>();
+
     ArrayList<String> present = new ArrayList<String>();
 
     String line;
 
     String line;
 
     BufferedReader fh
 
     BufferedReader fh
 
       = new BufferedReader(new FileReader("present.txt"));
 
       = new BufferedReader(new FileReader("present.txt"));
 
     while (null!=(line=fh.readLine())){
 
     while (null!=(line=fh.readLine())){
       names.add(line);
+
       present.add(line);
 
     }
 
     }
 
     fh.close();
 
     fh.close();
 
     fh = new BufferedReader(new FileReader("students.txt"));
 
     fh = new BufferedReader(new FileReader("students.txt"));
 
     while (null!=(line=fh.readLine())){
 
     while (null!=(line=fh.readLine())){
       if (!names.contains(line)) System.out.println(line);
+
       if (!present.contains(line))
 +
        System.out.println(line);
 
     }
 
     }
 
   }
 
   }
 
}
 
}
</answer>
+
]]></answer>
 
</question>
 
</question>
  
==Problem 4 - Count Routes==
+
==Count Routes==
 
<question className="CountRoutes" title="Count Routes" copyfile="year1.txt">
 
<question className="CountRoutes" title="Count Routes" copyfile="year1.txt">
 
Use the file year1.txt to print a list of all the routes in alphabetical order. We also want a count of how many students are in each route.
 
Use the file year1.txt to print a list of all the routes in alphabetical order. We also want a count of how many students are in each route.
Line 234: Line 241:
 
</question>
 
</question>
  
==Problem 5 – Use Map to count regions==
+
==Use Map to count regions==
 
<question className="CountRegions" title="Count Regions" copyfile="bbc.txt">
 
<question className="CountRegions" title="Count Regions" copyfile="bbc.txt">
Read in the file bbc.txt and output a list of region and the number of countries in each.
+
Read in the file http://progzoo.net/bbc.txt and output a list of region and the number of countries in each.
 
This problem is just like the previous problem. If you use a TreeMap in place of a hash map then the output will be in order.
 
This problem is just like the previous problem. If you use a TreeMap in place of a hash map then the output will be in order.
 
<prog>
 
<prog>
Line 286: Line 293:
 
</question>
 
</question>
  
==Problem 6 – Use Map to find associates==
+
==Use Map to find associates==
 
<question className="FindRegions" title="Find Associates Regions" copyfile="bbc.txt">
 
<question className="FindRegions" title="Find Associates Regions" copyfile="bbc.txt">
 
Read in the file bbc.txt and output a list of countries by region.
 
Read in the file bbc.txt and output a list of countries by region.
Line 292: Line 299:
 
  TreeMap&lt;String,ArrayList&lt;String>> region
 
  TreeMap&lt;String,ArrayList&lt;String>> region
 
     = new TreeMap&lt;String,ArrayList&lt;String>>();
 
     = new TreeMap&lt;String,ArrayList&lt;String>>();
<prog>
+
<prog><![CDATA[
 
import java.util.TreeMap;
 
import java.util.TreeMap;
 
import java.util.ArrayList;
 
import java.util.ArrayList;
Line 300: Line 307:
 
public class FindRegions{
 
public class FindRegions{
 
   public static void main(String[]argv) throws Exception{
 
   public static void main(String[]argv) throws Exception{
     TreeMap&lt;String,ArrayList&lt;String>> region
+
     TreeMap<String,ArrayList<String>> regions
       = new TreeMap&lt;String,ArrayList&lt;String>>();
+
       = new TreeMap<String,ArrayList<String>>();
 
     String line;
 
     String line;
 
     BufferedReader fh
 
     BufferedReader fh
Line 307: Line 314:
 
     while (null!=(line=fh.readLine())){
 
     while (null!=(line=fh.readLine())){
 
       String [] s = line.split("\t");
 
       String [] s = line.split("\t");
 +
      String name = s[0];
 +
      String region = s[1];
 +
      System.out.println("  "+name);
 
     }
 
     }
 
   }
 
   }
 
}
 
}
</prog>
+
]]></prog>
 
<answer>
 
<answer>
 
import java.util.TreeMap;
 
import java.util.TreeMap;
Line 339: Line 349:
 
</answer>
 
</answer>
 
</question>
 
</question>
 +
 +
==Process Log Files==
 +
A single line from the log file of sqlzoo.net http://progzoo.net/access_log looks like this:
 +
98.100.11.98 - - [26/Jan/2010:17:34:09 +0000] "GET /3.htm HTTP/1.1" 200 32441 "http://www.sqlzoo.net/3a.htm" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; MDDR; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2; MS-RTC LM 8)"
 +
Fields are separated by spaces.
 +
This line represents one “hit”
 +
 +
<table>
 +
<tr><td>98.100.11.98</td><td>is the ip address of the visitor</td></tr>
 +
<tr><td> - -</td><td>means they were not authenticated</td></tr>
 +
<tr><td>26/Jan/2010:17:34:09 +0000</td><td>Date and time according to my server</td></tr>
 +
<tr><td>/3.htm</td><td>is the address of the web page visited (full address is http://sqlzoo.net/3.htm)</td></tr>
 +
<tr><td>200</td><td>means the request was successful (404 is not found, 500 is server error)</td></tr>
 +
<tr><td>32441</td><td>is the size of the page</td></tr>
 +
<tr><td>http://www.sqlzoo.net/3a.htm</td><td>is the referrer – where the incoming link is coming from</td></tr>
 +
<tr><td>Mozilla/4.0 (compatible; MSIE 8.0; Windows…</td><td>Identifies the operating system and browser</td></tr>
 +
</table>
 +
 +
Questions - these cannot be answered on progzoo - they take too long
 +
*Give the ip addresses of the top three users?
 +
*What are the three most popular pages?
 +
*Who are my 10 best referrers (don’t count sqlzoo references)?
 +
*What proportion of my traffic is robotic? List all the robots and the number of hits. You should assume that all robots visit robot.txt and no humans visit that page.

Latest revision as of 21:36, 8 September 2013

ArrayList Interface Tutorial

In this tutorial you will use the ArrayList - these exercises are designed to get you started. You will use methods such as:

  • java.util.Collections.sort
  • add
  • get

Sort a List

The file from http://progzoo.net/students.txt contains a list of students:

Whitaker, Forest 
Winslet, Kate 
De Havilland, Olivia 
Donat, Robert 
Douglas, Michael 
Dressler, Marie 
...

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


[Font] [Default] [Show] [Resize] [History] [Profile]

List Routes

The file http://progzoo.net/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:

  • Use line.split("\t") to get the route
  • Use name.contains(st[1]) to add the route only if it is not already in the ArrayList
  • You do not need to sort before output



[Font] [Default] [Show] [Resize] [History] [Profile]

Who's missing?

The file http://progzoo.net/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 students.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);


[Font] [Default] [Show] [Resize] [History] [Profile]

Count Routes

Use the file year1.txt to print a list of all the routes in alphabetical order. We also want a count of how many students are in each route.

BENG (HONS) COMPUTER NETWORKS AND DISTRIBUTED SYSTEMS F/T	10
BENG (HONS) COMPUTER SECURITY AND FORENSICS F/T	28
BENG (HONS) COMPUTING F/T	22
BENG (HONS) EMBEDDED COMPUTER SYSTEMS F/T	1
...

How to do this:

  • See the section “Use Map to aggregate” from the lecture
  • Use the HashMap structure to map String to Integer, the String will be the route name, the Integer will be the number of students
  • HashMap supports methods put and get
  • You can use two passes to process get the answer.
    • On the first pass run over the list and put the value 0 against every route you encounter.
    • On the second pass use put to add 1 to each route you encounter.
  • It is better to use a single pass:
    • For each route if the key exists then +1 of the value, if the key does not exist then put 1.


[Font] [Default] [Show] [Resize] [History] [Profile]

Use Map to count regions

Read in the file http://progzoo.net/bbc.txt and output a list of region and the number of countries in each. This problem is just like the previous problem. If you use a TreeMap in place of a hash map then the output will be in order.


[Font] [Default] [Show] [Resize] [History] [Profile]

Use Map to find associates

Read in the file bbc.txt and output a list of countries by region. One way to do this is to build up a list of countries for each region. To do that you could use a structure such as:

TreeMap<String,ArrayList<String>> region
   = new TreeMap<String,ArrayList<String>>();


[Font] [Default] [Show] [Resize] [History] [Profile]

Process Log Files

A single line from the log file of sqlzoo.net http://progzoo.net/access_log looks like this:

98.100.11.98 - - [26/Jan/2010:17:34:09 +0000] "GET /3.htm HTTP/1.1" 200 32441 "http://www.sqlzoo.net/3a.htm" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; MDDR; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2; MS-RTC LM 8)"

Fields are separated by spaces. This line represents one “hit”

98.100.11.98is the ip address of the visitor
- -means they were not authenticated
26/Jan/2010:17:34:09 +0000Date and time according to my server
/3.htmis the address of the web page visited (full address is http://sqlzoo.net/3.htm)
200means the request was successful (404 is not found, 500 is server error)
32441is the size of the page
http://www.sqlzoo.net/3a.htmis the referrer – where the incoming link is coming from
Mozilla/4.0 (compatible; MSIE 8.0; Windows…Identifies the operating system and browser

Questions - these cannot be answered on progzoo - they take too long

  • Give the ip addresses of the top three users?
  • What are the three most popular pages?
  • Who are my 10 best referrers (don’t count sqlzoo references)?
  • What proportion of my traffic is robotic? List all the robots and the number of hits. You should assume that all robots visit robot.txt and no humans visit that page.