Java 在ArrayList上,比较许可证,或使用该许可证创建一个新人,然后直接从ArrayList中删除。但是由于op没有发布Person类,我不能假设哪一个是最好的。你试过这个代码吗?我不确定,但我认为在迭代时从列表中删除它会引发异常(我认为Concurren
Java 在ArrayList上,比较许可证,或使用该许可证创建一个新人,然后直接从ArrayList中删除。但是由于op没有发布Person类,我不能假设哪一个是最好的。你试过这个代码吗?我不确定,但我认为在迭代时从列表中删除它会引发异常(我认为Concurren,java,arrays,string,oop,arraylist,Java,Arrays,String,Oop,Arraylist,在ArrayList上,比较许可证,或使用该许可证创建一个新人,然后直接从ArrayList中删除。但是由于op没有发布Person类,我不能假设哪一个是最好的。你试过这个代码吗?我不确定,但我认为在迭代时从列表中删除它会引发异常(我认为ConcurrentModificationException但我不确定),因为for-each循环在后台使用迭代器。我已经尝试过,效果很好。可能是因为一旦我们找到了拥有该许可证的人,我们将删除该人并立即关闭该方法,这样它就不会进入下一次迭代 public cl
在ArrayList上,比较许可证,或使用该许可证创建一个新人,然后直接从ArrayList中删除。但是由于op没有发布Person类,我不能假设哪一个是最好的。你试过这个代码吗?我不确定,但我认为在迭代时从
列表中删除它会引发异常(我认为ConcurrentModificationException
但我不确定),因为for-each循环在后台使用迭代器。我已经尝试过,效果很好。可能是因为一旦我们找到了拥有该许可证的人,我们将删除该人并立即关闭该方法,这样它就不会进入下一次迭代
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;
}
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
}
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
}
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));
}
}
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());
}
}