2017-01-29 80 views
0

这里是java的新手,只是无法让我的头绕着数组。我需要从ArrayList中删除一个项目,我迷失在如何实际编码。我的程序读取一个.csv文件,从主类传递给第二个类一个字符串,一旦在第二个类中,我需要搜索数组列表并删除该字符串,如果它被发现并返回true表示它已被删除。任何帮助表示赞赏。从ArrayList中移除一个对象

二等

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; 
} 

编辑: Person类

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; 
} 
+0

我认为你在你实现 –

+1

例如什么是'person.equals(添加)'的意思的问题?比较'对象'和'布尔值'? –

+0

person.equals(add)假设返回true,如果它被添加 – Newb2Java

回答

1

试试这个:

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; 
} 
+0

这段代码有语法错误。你怎么知道'Person'类有'getLicense(String license)'方法? OP在他们的问题中没有提供'Person'类。 –

+0

遗憾是仓促..更新语法.. BTW无论是检索和比较许可的方法名。该算法将工作..即使它的平等。我假设OP是足够的智能来推断。 –

+0

'person'的类型是'ArrayList ',所以我们已经知道它没有'getLicense'方法。 –

0

如果你没有一个呢,上创建一个方法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 
} 

我假设每个人都有一个唯一的许可证号码和您不会两次添加相同的人到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

+0

为什么getLicense()是必需的,因为arraylist将包含该值,您可以将其删除? – Harry

+0

@Harry'ArrayList'包含'Person'对象而不是'String'对象。 –

+2

ArrayList的类型是人,而不是字符串的(如果是,我们可以只'返回person.remove(许可证);'我们可以通过每个人看起来就比较ArrayList的许可证,或创建一个新的人与该许可证并直接从ArrayList中删除,但由于op没有发布Person类,所以我不能假设哪一个最好。 –

0

你有在代码中的一些错误,我已经强调他们与下面的评论:

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)); 
    } 

} 
+0

我已经添加了人员课程中处理许可证号码 – Newb2Java

+0

@ Newb2Java的更新我的答案。 –

0

@ Newb2Java,该equals方法可以比较,在这种情况下person与任何对象,但如果一个参数(add这里)与调用者(person)的类型不同,结果是false。尽管您使用了单数名词person,但它的类型是复数,是Person s的集合。 ArrayList<Person>永远不会等于Boolean(也不是boolean)。 List方法add将返回trueremove将返回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()); 
    } 
} 
相关问题