| | |
Anonymous class
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 62
Reputation:
Solved Threads: 1
I have this code, but instead of implementing class enumeration I want to create an anonymous class in the method getEnumeration that does the job right in the return, hwo do I do that?
Java Syntax (Toggle Plain Text)
// File: TestList.java (Module 10) // // Author: Rahul Simha // Created: Nov 2, 1998 // // Starting point for Ex. 10.1: an anonymous class // implementation of an Enumeration. // The current file has the list implement the enumeration. import java.util.*; abstract class ComparableObject { public abstract String toString (); public abstract int compare (ComparableObject c); } class ListItem { ComparableObject data = null; ListItem next = null; // Constructor. public ListItem (ComparableObject obj) { data = obj; next = null; } // Accessor. public ComparableObject getData () { return data; } } // LinkedList now implements Enumeration itself. class LinkedList implements Enumeration { ListItem front = null; ListItem rear = null; int numItems = 0; // Current number of items. // Instance method to add a data item. public void addData (ComparableObject obj) { if (front == null) { front = new ListItem (obj); rear = front; } else { // Find the right place. ListItem tempPtr=front, prevPtr=front; boolean found = false; while ( (!found) && (tempPtr != null) ) { if (tempPtr.data.compare(obj) > 0) { found = true; break; } prevPtr = tempPtr; tempPtr = tempPtr.next; } // Now insert. if (!found) { // Insert at rear. rear.next = new ListItem (obj); rear = rear.next; } else if (tempPtr == front) { // Insert in front. ListItem Lptr = new ListItem (obj); Lptr.next = front; front = Lptr; } else { // Insert in the middle. ListItem Lptr = new ListItem (obj); prevPtr.next = Lptr; Lptr.next = tempPtr; } } numItems++; } public void printList () { ListItem listPtr = front; System.out.println ("List: (" + numItems + " items)"); int i = 1; while (listPtr != null) { System.out.println ("Item# " + i + ": " + listPtr.getData()); // Must implement toString() i++; listPtr = listPtr.next; } } ListItem enumPtr; // Must implement this method. public boolean hasMoreElements () { if (enumPtr == null) return false; else return true; } // Must implement this method. public Object nextElement() { Object obj = enumPtr.data; enumPtr = enumPtr.next; return obj; } // This is needed to return an Enumeration // instance to the user. public Enumeration getEnumeration () { enumPtr = front; return this; // Using the "this" reserved word. } } // End of class "LinkedList" // An object to use in the list: class Person extends ComparableObject { String name; String ssn; // Constructor. public Person (String nameIn, String ssnIn) { name = nameIn; ssn = ssnIn; } // Override toString() public String toString () { return "Person: name=" + name + ", ssn=" + ssn; } // Must implement compare public int compare (ComparableObject obj) { Person p = (Person) obj; return name.compareTo (p.name); } } // End of class "Person" // Test class. public class TestList { public static void main (String[] argv) { // Create a new list object. LinkedList L = new LinkedList (); // Create a Person instance and add it to list. L.addData (new Person ("Terminator", "444-43-4343")); L.addData (new Person ("James Bond", "666-65-6565")); L.addData (new Person ("Rambo", "555-54-5454")); L.addData (new Person ("Bruce Lee", "777-76-7676")); // Print contents via an Enumeration. Enumeration e = L.getEnumeration(); while (e.hasMoreElements()) { Person p = (Person) e.nextElement(); System.out.println (p); } } } // End of class "TestList"
0
#2 Nov 2nd, 2009
Just like any anonymous implemenation Creating a small private inner class for the enumeration and returning an instance of that would be better though. You can see that idiom used quite a lot in the JDK classes.
Also keep in mind that Iterator is generally preferred to Enumeration these days.
Java Syntax (Toggle Plain Text)
return new Enumeration() { public boolean hasMoreElements() { throw new UnsupportedOperationException("Not supported yet."); } public Object nextElement() { throw new UnsupportedOperationException("Not supported yet."); } };
Also keep in mind that Iterator is generally preferred to Enumeration these days.
![]() |
Similar Threads
- Java Problem with running program (Java)
- Applet / Array help (Java)
- Anonymous Objects (C++)
- Code Snippet: Anonymous Objects (C++)
- Help with Java programming for lottery (Java)
- Help with program...Please...... (Java)
- DrawHouse code help... (Java)
- DrawHouse Code Problem (Java)
Other Threads in the Java Forum
- Previous Thread: hangman on the phone problem Java
- Next Thread: netbeans troubles
Views: 296 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener android api apple applet application arguments array arrays automation binary bluetooth character chat class classes client code component consumer database desktop detection draw eclipse encode error event exception file fractal ftp game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javac javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie number object online oracle os print printf problem program programming project properties recursion researchinmotion rotatetext rsa scanner score screen server set size sms socket sort sql string swing template test threads time transfer tree update windows working xstream






