Week 4 (Lists)

  1. Read http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html.

    Watch video (reading the Java api docs on ArrayLists)

    Watch video (simple intro to lists)

  2. Write a program that creates the ArrayList whose zeroth element is 1, whose first element is "Hello" and whose second element is 4. Print out the list (in one statement using the ArrayList toString() method. Solution

    Watch video (Looping through lists)

  3. Write a program that creates the ArrayList whose zeroth element is 1, whose first element is 3, whose second element is 3 and whose 4th element is "hello". Print out the list, each element on a new line (using a for loop). Solution

    Watch video (Introduction to Generics)

  4. Write a program that creates the ArrayList <Integer> whose zeroth element is 1, whose first element is 3, whose second element is 3 and whose 4th element is 6. Print out the list, each element on a new line (using a for loop). Solution

    Watch video (Non-destructive methods using lists)

  5. append1 has two methods append1 and append2. The method append2 is better. Why? Run append1 to see that append1 affects its parameters (bad) whereas append2 does not.

  6. (Easy Assignment) Write a method whose heading is static ArrayList reverse (ArrayList a) which returns a list the same as a but in reverse; You will need a loop, using the size() method, the add() method and the get(int i) method from the ArrayList class. It should return an ArrayList whose zeroth element is the same as the last element of a whose next element is the last but one of a etc. It will have the same number of elements as a. Your method should not affect its parameter. Test out your method by calling it in main a few times.

    Watch video (Help with easy assignment 1)

    Upload your assignment to directory IS52013B-assignments-2010-11 or to IS52014B-assignments-2010-11 depending on your course.

  7. Here is a program which reads from a URL.

    import java.io.*;
    import java.net.*;
    import java.util.Scanner;
    
    public class getURL {
    
       public static void main (String[] args) throws Exception {
    
          URL u;
          Scanner s;
          
          
             u = new URL("http://localhost");
    
             
             s = new Scanner(u.openStream());
    
             
             while (s.hasNext()) {
                System.out.println(s.nextLine());
             }
    
          
       }  // end of main
    
    } // end of class definition
    

    Convert it so the URL come from the command line.

    Watch video (reading from a url)

    For the first few weeks we are going to play with this HTML Parser. Read all the documentation. Find and try out all the sample programs.

    To use it, you need to put htmlparser.jar in your CLASSPATH.

    If you are using Eclipse you can add htmlparser.jar to the Java Build Path of the current Java Project. See, for example, http://www.cs.iastate.edu/~lmiller/cs362/ClasspathandEclipse.htm on how to do it in Eclipse.

    Watch video (using the html parser)

  8. Try the following program:

    import org.htmlparser.util.*;
    import org.htmlparser.*;
    import org.htmlparser.tags.*;
    import org.htmlparser.filters.*;
    
    class test1
    {
    
    public static void main(String args[]) throws Exception	
    { 
    Parser parser = new Parser (args[0]);
     NodeList list = parser.parse (new LinkStringFilter("")); // no filter
    	for (int i=0;i<list.size();i++)
    	{
    	    System.out.println(((LinkTag)(list.elementAt(i))).extractLink());
    	}
    }
    }
    

    Run with

    java -cp .:htmlparser.jar test1 http://somewebsite/
    

  9. What is the type of list.elementAt(i) above?
  10. Look in the documentation. for this type to find the other methods that can be applied to list.elementAt(i).
  11. Find the parser.parse() method in the documentation and try it with some different parameters.
  12. Try week1/test1.java. What does it do? Does it give duplicates?

  13. (hard assignment)

Week 4 Resources

s.danicic@gold.ac.uk
Sebastian Danicic BSc MSc PhD (Reader in Computer Science)
Dept of Computing, Goldsmiths, University of London, London SE14 6NW
Last updated 2015-09-04