从arrayList()中删除属性

时间:2015-11-10 15:21:46

标签: java arraylist

如标题中所述,如何从arrayList中删除属性, 我的方法是这样的:

public boolean removeBorrower(String libraryNumber)

我基本上必须检查此方法中的参数是否等于属性,如果是,则根据该删除借用者。

这是我的代码:

import java.util.*;
/**
* Write a description of class BorrowerList here.
* 
* @author xxxxxx 
* @version 08/11/2015
*/
public class BorrowerList
{
    private ArrayList<Borrower> borrowers;

    public BorrowerList()
    {
        borrowers = new ArrayList<Borrower>();
    }

    public void addBorrowers(Borrower borrower)
    {
        borrowers.add(borrower);
    }

    public void getAllBorrowers()
    {   
        for(Borrower borrower : borrowers)
        {
            borrower.printBorrowerDetails();
        }
    } 

    public void getBorrower(int borrowerEntry)
    {
        if(borrowerEntry < 0)
        {
            System.out.println("Negative entry :" + borrowerEntry);
        }
        else if(borrowerEntry < getNumberOfBorrowers())
        {
            Borrower borrower = borrowers.get(borrowerEntry);
            borrower.printBorrowerDetails();
        }
        else
        {
            System.out.println("No such entry :" + borrowerEntry);
        }
    } 

    public int getNumberOfBorrowers()
    {
        return borrowers.size();
    }

    public void removeBorrower(int borrowerEntry)
    {
        if(borrowerEntry < 0)
        {
            System.out.println("Negative entry :" + borrowerEntry);
        }
        else if(borrowerEntry < getNumberOfBorrowers())
        {
            borrowers.remove(borrowerEntry);
        }
        else
        {
            System.out.println("No such entry :" + borrowerEntry);
        }
    }

    public boolean removeBorrower(String libraryNumber)
    {
        borrowers.remove(libraryNumber);
    }

    public int search(String libraryNumber)
    {
        int index = 0;
        boolean found = false;
        while(index < borrowers.size() && !found)
        {
            Borrower borrower = borrowers.get(index);
            if(borrower.getLibraryNumber().equals(libraryNumber))
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        if (index < borrowers.size())
        {
            return index;
        }
        else
        {
            return -1;
        }
    }
}

编辑:

以下是借款人类本身的代码

/**
* Write a description of class Borrower here.
* 
* @author xxxxxx
* @version 08/11/2015
*/
public class Borrower
{
    private String firstName;
    private String lastName;
    private String libraryNumber;
    private int noOfBooks;
    private Address address;

    /**
     * Constructor for objects of class Borrower.
     * The number of books should be set to 1.
     * 
     * @param firstName The Borrower's first name 
     * @param lastName The Borrower's last name
     * @param lNumber The Borrower's library number
     * @param street The Borrower's street
     * @param town The Borrower's town
     * @param postcode The Borrower's postcode
     */
     public Borrower(String fName, String lName, String lNumber, 
                String street, String town, String postcode)
    {
        firstName = fName;
        lastName = lName;
        libraryNumber = lNumber;
        noOfBooks = 1;        
        address = new Address(street, town, postcode);
    }

    /**
     * Constructor for objects of class Borrower.
     * The number of books on loan should should be set to
     * the supplied vale.
     * 
     * @param fName The Borrower's first name 
     * @param lName The Borrower's last name
     * @param lNumber The Borrower's library number
     * @param numberOfBooks The initial book borrow
     * @param street The Borrower's street
     * @param town The Borrower's town
     * @param postcode The Borrower's postcode
     */
     public Borrower(String fName, String lName, String lNumber, int       numberOfBooks, 
                String street, String town, String postcode)
     {
        firstName = fName;
        lastName = lName;
        libraryNumber = lNumber;
        noOfBooks = numberOfBooks;         
        address = new Address(street, town, postcode);
     }


    /**
     * Get the Borrower's first name
     * 
     * @return the Borrower's first name
     */
    public String getFirstName()
    {
        return firstName;
    }

    /**
     * Get the Borrower's last name
     * 
     * @return the Borrower's last name
     */
     public String getLastName()
     {
        return lastName;
     }

    /**
     * Get the Borrower's library Number
     * 
     * @return the Borrower's library number
     */
     public String getLibraryNumber()
    {
        return libraryNumber;
    }

    /**
     * Get the number of books on loan
     * 
     * @return the number of books on loan
     */
    public int getNoOfBooks()
    {
        return noOfBooks;
    }

    /**
     * Print out the Borrower's details to the console window
     * 
     */
    public void printBorrowerDetails()
    {
        System.out.println( firstName + " " + lastName 
                       + "\n" + address.getFullAddress()
                       + "\nLibrary Number: " + libraryNumber
                       + "\nNumber of loans: " + noOfBooks);
    }     



    /**
     * Increase the bumber of books on loan by 1
     * 
     */
    public void borrowBook()
    {
        noOfBooks = noOfBooks + 1;
        System.out.println("Books on loan: " + noOfBooks);        
    }

    /**
     * Increase the bumber of books on loan by a given number
     * 
     * @param number of new loans to add to total
     */
    public void borrowBooks(int number)
    {
        noOfBooks = noOfBooks + number;
        System.out.println("Books on loan: " + noOfBooks);        
    }

    /**
     * Return a book
     * 
     */
    public void returnBook ()
    {
        noOfBooks = noOfBooks - 1 ;
        System.out.println("Books on loan: " + noOfBooks);        
    }

    /**
     * Return the Borrower's address
     * 
     * @return the Borrower's address
     */
    public String getAddress()
    {
        return address.getFullAddress();
    }

    /**
     * Change the Borrower's address
     * 
     * @param street the street
     * @param town the town
     * @param postcode the postcode
     */
    public void setAddress(String street, String town, String postcode)
    {
        address.setFullAddress(street, town, postcode);
    }

    /**
     * Print out Borrower's address
     */
    public void printAddress()
    {
        address.printAddress();
    }

} // end class

2 个答案:

答案 0 :(得分:2)

根据JavaDoc

  

public boolean remove(Object o)

     

从此列表中删除第一次出现的指定元素,   如果它存在。如果列表不包含该元素,则为   不变。更正式地,删除具有最低索引i的元素   这样(o == null?get(i)== null:o.equals(get(i)))(如果是这样的话)   元素存在)。如果此列表包含指定的,则返回true   element(或等效地,如果此列表由于更改而更改)   呼叫)。

当且仅当您有一些等于该字符串的borrowers.remove(libraryNumber);对象时,这基本上意味着Borrower将起作用。除非您在equals课程中为Borrower方法添加了覆盖,否则它不太可能有效。因此,您有两种选择:

  1. 覆盖equals类中的hashcode()(以及良好做法,Borrower)方法,以使两个借用项目具有相同libraryNumber iterator 1}}。

  2. 第二个选项是使用Borrower浏览borrower中存储的remove项,并使用迭代器的Borrower方法删除具有相同属性值的对象。

  3. 作为旁注,虽然第一种方法可能对某些人来说更优雅,但必须谨慎,因为在您的特定情况下,libraryNumber对象可能不相同只是因为它们具有相同的document.getElementsByClassName("point")[a].style.top;

答案 1 :(得分:0)

public boolean removeBorrower(String libraryNumber)
{
    boolean retVal = false;
    if(borrowers.contains(libraryNumber))
    {
       retVal = borrowers.remove(libraryNumber);
    } 
    return retVal;
}

也许这样的事情?我知道这是一个长期的做事方式,因为最简单的方法是

public boolean removeBorrower(String libraryNumber)
{
       return borrowers.remove(libraryNumber);
}

但也许我不太明白你遇到的问题是什么?