A simple generic class for constructing lists in Java (later than 1.4) and C# (later than 2.0)

The following is the specification of useful members of a Java (generic) list handling class useful in developing translators. The closely equivalent C# (generic) version appears lower on this page.

The earlier versions of Java and C# did not have generic versions of these classes, but worked with elements of the Object class, which necessitated casting operations. For details of how to use these consult this page.

  import java.util.*;

  class ArrayList<E>
  // Class for constructing a list of elements of type E in Java

    public ArrayList<E>()
    // Empty list constructor

    public void add(E element)
    // Appends element to end of list

    public void add(int index, E element)
    // Inserts element at position index

    public E get(int index)
    // Retrieves an element from position index

    public E set(int index, E element)
    // Inserts element into the list at position index

    public void clear()
    // Clears all elements from list

    public int size()
    // Returns number of elements in list

    public boolean isEmpty()
    // Returns true if list is empty

    public boolean contains(E element)
    // Returns true if element is in the list

    public boolean indexOf(E element)
    // Returns position of element in the list

    public E remove(int index)
    // Removes the element at position index

    public E remove(E element)
    // Removes element from the list

  } // ArrayList
Here is a sample program showing the use of this library
   import library.*;
   import java.util.*;

   class ShowGenericList {
   // Demonstrate simple application of the generic ArrayList class in Java

     static class Entry {
       public String name;
       public int age;                                     // other fields could be added

       public Entry(String name, int age) {                // constructor
         this.name = name;
         this.age = age;
       }
     } // Entry

     static ArrayList<Entry> list = new ArrayList<Entry>(); // global for simplicity here!

     public static int position(String name) {
     // Finds position of entry with search key name in list, or -1 if not found
       int i = list.size() - 1;                            // index of last entry
       while (i >= 0 &&                                    // short-circuit protection
              !name.equals(list.get(i).name))              // no need to cast before extracting field
         i--;                                              // will reach -1 if no match
       return i;
     } // position

     public static void main (String[] args) {
     // Build a list of people's names and ages
       IO.writeLine("Supply a list of people's names and ages.  CTRL-Z to terminate");
       do {
         String name = IO.readWord();
         if (IO.eof()) break;
         int age = IO.readInt();
         IO.readLn();
         list.add(new Entry(name, age));                   // add to end of list
       } while (!IO.eof());

       IO.writeLine(list.size() + " items stored");        // report size of list

       Entry patEntry = new Entry("Pat", 63);              // that fellow again!
       list.set(0, patEntry);                              // insert him on position 0

       StringBuffer sb = new StringBuffer();               // demonstrate StringBuffer use
       for (int i = 0; i < list.size(); i++) {             // display each entry
         Entry e = list.get(i);                            // retrieve an item at position i
         IO.write(e.name, -16); IO.writeLine(e.age);       // -16 means "left justify"
         sb.append(e.name + " ");                          // add the names to a StringBuffer object
       }
       IO.writeLine();

       int where = position("Peter");                      // find the silly fellow!
       if (where < 0) IO.writeLine("Peter not found");
       else {
         Entry peter = (Entry) list.get(where);            // retrieve an item at position where
         IO.writeLine("Peter found at position " + where + ". He is " + peter.age + " years old");
       }

       if (sb.length() > 0) {
         IO.writeLine();
         IO.writeLine("Summary of names:");
         IO.writeLine();
         IO.writeLine(sb.toString());
       }
     } // main

   } // ShowGenericList


The following is the specification of useful members of a C# 2.0 generic list handling class useful in developing translators. A closely equivalent Java version appears earlier on this page. Note that this class is called List and not ArrayList.

The earlier versions of Java and C# did not have generic versions of these classes, but worked with elements of the Object class, which necessitated casting operations. For details of how to use these consult this page.

  using System.Collections.Generic;

  class List<E>
  // Class for constructing a list of elements of type E in C#

    public List<E>()
    // Empty list constructor

    public void Add(E element)
    // Appends element to end of list

    public void Insert(int index, E element)
    // Inserts element at position index

    public object this [int index] {set; get; }
    // Inserts or retrieves an element at position index
    // list[index] = element;  element = list[index];

    public void Clear()
    // Clears all elements from list

    public int Count { get; }
    // Returns number of elements in list

    public boolean Contains(E element)
    // Returns true if element is in the list

    public boolean IndexOf(E element)
    // Returns position of element in the list

    public void RemoveAt(int index)
    // Removes the element at position index

    public void Remove(E element)
    // Removes element from list

  } // List
Here is a sample program like the one given earlier showing the use of this library
   using Library;
   using System.Collections.Generic;
   using System.Text;

   class ShowGenericList {
   // Demonstrate simple application of the generic List class (C# version 2.0)

     class Entry {
       public string name;
       public int age;                                     // other fields could be added

       public Entry(string name, int age) {                // constructor
         this.name = name;
         this.age = age;
       }
     } // Entry

     static List<Entry> list = new List<Entry>();          // global for simplicity here!

     public static int Position(string name) {
     // Finds position of entry with search key name in list, or -1 if not found
       int i = list.Count - 1;                             // index of last entry
       while (i >= 0 &&                                    // short-circuit protection
              !name.Equals(list[i].name))                  // no need to cast before extracting field
         i--;                                              // will reach -1 if no match
       return i;
     } // Position

     public static void Main (string[] args) {
     // Build a list of people's names and ages
       IO.WriteLine("Supply a list of people's names and ages.  CTRL-Z to terminate");
       do {
         string name = IO.ReadWord();
         if (IO.EOF()) break;
         int age = IO.ReadInt();
         IO.ReadLn();
         list.Add(new Entry(name, age));                   // add to end of list
       } while (!IO.EOF());

       IO.WriteLine(list.Count + " items stored");         // report size of list

       Entry patEntry = new Entry("Pat", 63);              // that fellow again!
       list[0] = patEntry;                                 // insert him on position 0

       StringBuilder sb = new StringBuilder();             // demonstrate StringBuilder use
       for (int i = 0; i < list.Count; i++) {              // display each entry
         Entry e = list[i];                                // retrieve an item at position i
         IO.Write(e.name, -16); IO.WriteLine(e.age);       // -16 means "left justify"
         sb.Append(e.name + " ");                          // add the names to a StringBuffer object
       }
       IO.WriteLine();

       int where = Position("Peter");                      // find the silly fellow!
       if (where < 0) IO.WriteLine("Peter not found");
       else {
         Entry peter = list[where];                        // retrieve an item at position where
         IO.WriteLine("Peter found at position " + where + ". He is " + peter.age + " years old");
       }

       if (sb.Length > 0) {
         IO.WriteLine();
         IO.WriteLine("Summary of names:");
         IO.WriteLine();
         IO.WriteLine(sb.ToString());
       }
     } // Main

   } // ShowGenericList

Revised: 2014/03/13