The following is the specification of useful members of a Java 1.4 (non generic) list handling class useful in developing translators. The closely equivalent C# 1.0 (non-generic) version appears lower on this page.
The later versions of Java and C# introduced generic versions of these classes. For details of how to use these consult this page
import java.util.*; class ArrayList // Class for constructing a list of objects public ArrayList() // Empty list constructor public void add(Object o) // Appends o to end of list public void add(int index, Object o) // Inserts o at position index public Object get(int index) // Retrieves an object from position index public Object set(int index, Object o) // Inserts Object O 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(Object o) // Returns true if o is in the list public boolean indexOf(Object o) // Returns position of o in the list public Object remove(int index) // Removes the object at position index } // ArrayListHere is a sample program showing the use of this library
import library.*; import java.util.*; class ShowArrayList { // Demonstrate simple application of the non 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 list = new ArrayList(); // 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(((Entry)list.get(i)).name)) // must 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", 61); // 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 = (Entry) list.get(i); // retrieve (via a cast) 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); 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 } // ShowArrayList
The following is the specification of useful members of a C# list handling class useful in developing translators. A closely equivalent Java version appears earlier on this page.
The later versions of Java and C# introduced generic versions of these classes. For details of how to use these consult this page
using System.Collections; class ArrayList // Class for constructing a list of objects public ArrayList() // Empty list constructor public int Add(object o) // Appends o to end of list public object this [int index] {set; get; } // Inserts or retrieves an object in position index // list[index] = object; object = list[index] public void Clear() // Clears all elements from list public int Count { get; } // Returns number of elements in list public boolean Contains(object o) // Returns true if o is in the list public boolean IndexOf(object o) // Returns position of o in the list public void Remove(object o) // Removes object o from list public void RemoveAt(int index) // Removes the object at position index } // ArrayListHere is a sample program like the one given earlier showing the use of this library
using Library; using System.Collections; using System.Text; class ShowArrayList { // Demonstrate simple application of the non-generic ArrayList class in C# 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 list = new ArrayList(); // 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(((Entry)list[i]).name)) // must 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"); // interrogate size of list Entry patEntry = new Entry("Pat", 61); // 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 = (Entry) list[i]; // retrieve (via a cast) 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[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 } // ShowArrayList
Revised: 2009/10/12