Java-如何根据用户输入检查列表是否包含对象名称?

时间:2018-09-12 05:42:33

标签: class object

我正在尝试编写一些代码,该代码使用扫描器输入来询问LibraryPatron的名称,并使用contains()方法来检查LibraryPatron是否在列表中。我没有关于contains()方法如何工作的问题,而是关于如何使用它来检查对象名称的问题。预先感谢。

这是我的LinkedList类(在本例中为BagList),已经有一个contains()方法:

package libraryPatrons;

public final class BagList<T> implements BagInterface<T> {

    Node firstNode;
    int numPatrons;

    public BagList() {
        firstNode = null;
        numPatrons = 0;
    }

    public int getCurrentSize() { 
        return numPatrons; 
    }

    /** Sees whether this bag is empty.   
      * @return  True if the bag is empty, or false if not. */
    public boolean isEmpty() { 
        return firstNode == null; 
    }

    /** Adds a new entry to this bag.   
      * @param newEntry  The object to be added as a new entry.  
      * @return  True if the addition is successful, or false if not. */
    public boolean add(T newEntry) { 
        Node newNode = new Node (newEntry);
        newNode.next = firstNode;
        firstNode = newNode;
        ++numPatrons;
        return true;
    }

    /** Removes one unspecified entry from this bag, if possible.
      * @return  The removed entry if the removal was successful, or null. */
    public T remove() { 
        T result = null;
        if (firstNode != null) {
            result = firstNode.data;
            firstNode = firstNode.next;
            --numPatrons;
        }
        return result;
    }

    private Node getReferenceTo (T anEntry) {
        Node currentNode = firstNode;
        boolean found = false;
        while (!found && currentNode != null) {
            if (anEntry.equals(currentNode.data)) {
                found = true;         
            } else {
                currentNode = currentNode.next;
            }
        }
        return currentNode;
    }

    /** Removes one occurrence of a given entry from this bag.
      * @param anEntry  The entry to be removed.
      * @return  True if the removal was successful, or false if not. */
    public boolean remove(T anEntry) { 
        boolean result = false;
        Node currentNode = getReferenceTo (anEntry);
        if (currentNode != null) {
            currentNode.data = firstNode.data;
            firstNode = firstNode.next;
            --numPatrons;
            result = true;
        } 
        return result;
    }

    /** Removes all entries from this bag. */
    public void clear() {
        while (!isEmpty()) {
            remove();
        }
        numPatrons = 0;
    }

    /** Counts the number of times a given entry appears in this bag.   
      * @param anEntry  The entry to be counted.   
      * @return  The number of times anEntry appears in the bag. */
    public int getFrequencyOf(T anEntry) { 
        Node currentNode = firstNode;
        int frequency = 0;
        while (currentNode != null) {
            if (anEntry.equals(currentNode.data))
                ++frequency;
            currentNode = currentNode.next;
        }
        return frequency;
    }

    /** Tests whether this bag contains a given entry.   
      * @param anEntry  The entry to locate.   
      * @return  True if the bag contains anEntry, or false if not. */
    public boolean contains(T anEntry) { 
        Node currentNode = getReferenceTo (anEntry);
        return !(currentNode == null);
    }

    /** Retrieves all entries that are in this bag.   
      * @return  A newly allocated array of all the entries in the bag.
      * Note: If the bag is empty, the returned array is empty. */
    public T[] toArray() { 
        T[] newArray = (T[]) new Object[numPatrons];
        int index = 0;
        Node currentNode = firstNode;
        while (currentNode != null) {
            newArray[index] = currentNode.data;
            ++index;
            currentNode = currentNode.next;
        }
        return newArray;
    }

    private class Node {
        private T data;
        private Node next;

        private Node (T dataPortion) {
            this(dataPortion, null);
        }

        private Node (T dataPortion, Node nextNode) {
            this.data = dataPortion;
            this.next = nextNode;
        }
    }
}

这是我的主班:

package libraryPatrons;
import java.util.Scanner;

class Main {
    public static void main (String args[]) {               
        LibraryPatron patron1 = new LibraryPatron("Bob", "3814872910", "Adelaide Way", "Belmont", "94002");
        LibraryPatron patron2 = new LibraryPatron("Les", "3860165016", "Chevy St", "Belmont", "94002");
        LibraryPatron patron3 = new LibraryPatron("Anna", "7926391055", "Davey Glen Rd", "Belmont", "94002");
        LibraryPatron patron4 = new LibraryPatron("Amy", "7619356016", "Fernwood Way", "Belmont", "94002");
        LibraryPatron patron5 = new LibraryPatron("Tom", "1758563947", "Flasner Ln", "Belmont", "94002");
        LibraryPatron patron6 = new LibraryPatron("James", "4729573658", "Marsten Ave", "Belmont", "94002");
        LibraryPatron patron7 = new LibraryPatron("Jason", "3858239773", "Middlesex Rd", "Belmont", "94002");
        LibraryPatron patron8 = new LibraryPatron("Jess", "3866392656", "Oxford Ct", "Belmont", "94002");
        LibraryPatron patron9 = new LibraryPatron("Mike", "7836591904", "Sem Ln", "Belmont", "94002");
        LibraryPatron patron10 = new LibraryPatron("Abby", "1960265836", "Tioga Way", "Belmont", "94002");
        LibraryPatron patron11 = new LibraryPatron("Dom", "5917485910", "Village Dr", "Belmont", "94002");
        LibraryPatron patron12 = new LibraryPatron("Wes", "5810385736", "Willow Ln", "Belmont", "94002"); 

        BagList<LibraryPatron> listOfPatrons = new BagList<LibraryPatron>();

        listOfPatrons.add(patron1);
        listOfPatrons.add(patron2);
        listOfPatrons.add(patron3);
        listOfPatrons.add(patron4);
        listOfPatrons.add(patron5);
        listOfPatrons.add(patron6);
        listOfPatrons.add(patron7);
        listOfPatrons.add(patron8);
        listOfPatrons.add(patron9);
        listOfPatrons.add(patron10);
        listOfPatrons.add(patron11);
        listOfPatrons.add(patron12);

        Scanner patronInput = new Scanner(System.in);
        String name = patronInput.nextLine();
        System.out.println("Does this list of library patrons contain: " + name + "?");
        System.out.println(/* contains(objectName)? */);
    }
}

在main()函数中,我想询问LibraryPatron的名称,并使用contains()方法来检查LibraryPatron是否在列表中。

0 个答案:

没有答案