gpt4 book ai didi

java - 从 arrayList() 中删除属性

转载 作者:行者123 更新时间:2023-11-30 03:11:06 25 4
gpt4 key购买 nike

如标题所述,如何从 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

最佳答案

根据 JavaDoc :

public boolean remove(Object o)

Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

这本质上意味着,当且仅当您有某个等于该字符串的 Borrower 对象时,borrowers.remove(libraryNumber); 才会起作用。除非您在 Borrower 类中添加了对 equals 方法的重写,否则这不太可能起作用。因此,您有两个选择:

  1. 重写 Borrower 类中的 equals(为了更好的实践,重写 hashcode())方法,以便两个借款人如果项目具有相同的libraryNumber,则被视为相等。

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

顺便说一句,虽然第一种方法对某些人来说可能更优雅,但必须小心,因为在您的特定场景中,Borrower 对象可能不会仅仅因为它们具有相同的 图书馆编号

关于java - 从 arrayList() 中删除属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33633404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com