这里是Java新手,只是无法理解数组。我需要从ArrayList
中删除一个项目,我迷失了如何实际编码。我的程序读取一个.csv
文件,从主类传递给第二个类一个字符串,一旦在第二个类中我需要搜索数组列表并删除该字符串,如果找到它并返回true表示它已被删除。任何帮助表示赞赏。
二等
public class PersonLogImpl {
private boolean remove;
private boolean isLicenseUnique;
private boolean add;
private final ArrayList<Person> person;
public PersonLogImpl() {
this.person = new ArrayList<>();
}
public ArrayList<Person> getPersonLog(){
return Person;
}
public boolean add(Person obj){ //add person object to ordered list
person.add(obj);
return add;
}
public boolean remove (String license){ //remove person with specific license from list
if(person.remove(person.equals(add))){
remove = true;
}
return remove;
}
编辑:人类
public class Person{
private String licenseNumber;
public Person(){
}
public Person(String licenseNumber){
this.licenseNumber = licenseNumber;
}
public String getLicenseNumber(){
return licenseNumber;
}
public void setLicenseNumber(String licenseNumber){
this.licenseNumber = licenseNumber;
}
@Override
public int hashCode(){
int hash = 7;
return hash;
}
@Override
public boolean equals(Object obj){
if (this == obj){
return true;
}
if (obj == null){
return false;
}
if (getClass() != obj.getClass()){
return false;
}
final Person other = (Person) obj;
if (!Objects.equals(this.licenseNumber, other.licenseNumber)){
return false;
}
return true;
}
@Override
public String toString(){
return "Person{" + "licenseNumber=" + licenseNumber + '}';
}
public boolean validateLicense(){
boolean retValue = false;
if ((this.licenseNumber.matches("^[a-zA-Z]{2}\\d{7}$")))
retValue = true;
return retValue;
}
试试这个:
public boolean remove (String license){ //remove person with specific license from list
Iterator<Person> personIterator = person.iterator();
boolean remove = false;
while(personIterator.hasNext(){
Person personInstance = personIterator.next();
if(personInstance.getLicense().equals(license))){
personIterator.remove();
remove = true;
break;
}
}
return remove;
}
@ newb2Java,equals
方法可以比较,在这种情况下person
与任何对象,但如果参数(add
在这里)是与调用者(person
)不同的类型,结果是false
。虽然你使用了单数名词person
,但它的类型是复数,Person
s的集合。 ArrayList<Person>
永远不会等于Boolean
(也不是boolean
)。 List
方法add
将返回true
。 remove
将返回true
,如果它实际上删除了参数,必须是Person
类型。首先,您必须找到列表中的哪个元素具有指示的属性值。所以你必须遍历列表,使用迭代器是最好的方法,直到找到具有给定License
的元素的每个实例。像这样的东西:
package practice;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class Personator {
private final List<Person> persons = new ArrayList<>();
public List<Person> getPersons() {
return new ArrayList<>(persons);
}
public boolean add(Person person) {
return persons.add(person);
}
public boolean remove(String license) {
return remove(new Person(license));
}
public boolean remove(Person removal) {
boolean wasRemoved = false;
for (ListIterator<Person> iterator = persons.listIterator(persons.size());
iterator.hasPrevious();
) {
final Person person = iterator.previous();
if (removal.equals(person)) {
iterator.remove();
wasRemoved = true;
}
}
return wasRemoved;
}
}
有了这个:
public class Person {
private final String license;
public Person(String license) {
if (license == null) {
throw new IllegalArgumentException("null license not allowed");
}
this.license = license;
}
public String getLicense() {
return license;
}
@Override
public String toString() {
return "Person " + getLicense();
}
@Override
public int hashCode() {
return getLicense().hashCode();
}
@Override public boolean equals(Object oth) {
if (this == oth) {
return true;
}
if (! (oth instanceof Person)) {
return false;
}
Person other = (Person) oth;
return this.getLicense().equals(other.getLicense());
}
}
你的代码中有一些错误,我用下面的注释突出显示了它们:
public class PersonLogImpl {
private boolean remove;
private boolean isLicenseUnique;
private boolean add;
private final ArrayList<Person> person;
public PersonLogImpl() {
// YOU SHOULD ALSO ASSIGN THE PRIVATE BOOLEANS HERE
this.person = new ArrayList<>();
}
public ArrayList<Person> getPersonLog() {
return Person; // What's going on here?
// You're returning the actual Person class when you should be returning
// an ArrayList of type Person (I think you meant for the P to be lowercase)
}
public boolean add(Person obj){ //add person object to ordered list
person.add(obj);
return add; // You never assign true or false to 'add' (I believe
// it defaults to true) so this method will always return true (maybe
// false, not sure). you should instead use 'return person.add(obj);'
}
public boolean remove (String license){ //remove person with specific license from list
// The code below makes no sense (no offence).
if(person.remove(person.equals(add))){
remove = true;
}
return remove;
// Since I don't know the make up of the 'Person' class I can't
// tell you how this method should function. I'll make a guess though:
for(int i = 0; i < person.size(); ++i) {
if(person.get(i).getLicenseNumber().equals(license)) {
return person.remove(i);
}
}
// OR a shorter alternative
return person.remove(new Person(license));
}
}
如果还没有,请在getLicense()
类上创建一个方法Person
。该方法应返回人员执照,然后您可以使用下面的remove
方法;
编辑:我现在看到你的Person类有一个getLicenseNumber()
方法。
public boolean remove(String license) {
for (Person individual : person) { // go through each Person on the ArrayList
if (individual.getLicenseNumber().equals(license)) // check if that Person's license is equal to the license we're looking for
return person.remove(individual); // if so, remove that person and return true (remove will return true if the element is found)
}
return false; // if we never removed any element we never found a person with that license. In that case, return false
}
我假设每个人都有一个唯一的许可证号,并且你不会将相同的Person两次添加到ArrayList(或者会阻止用户尝试)。如果您希望删除任何具有该许可证的人员,您可以使用以下方法:
public boolean remove(String license) {
int listsize = person.size(); // size before removing any
for(int i=0; i<person.size(); i++) {
if (person.get(i).getLicenseNumber().equals(license))
person.remove(i--); // i-- is required because the list size will change if we remove from it, so we need to decrement i
}
return !(listsize == person.size()); // if the size before is equal to the size now, we never removed any and thus return false
}
如果你想要一个更简单的解决方案,你可以查看method removeAll from ArrayList class