java基础问题

m0_54814086 2021-05-09 06:31:57
请问为什么执行删除房屋步骤时会报错,应该如何修改呢?

import java.util.*;

public class House {
public String name = " ";
public String telephone = " ";
public String adress = " ";
public String sent = " ";
public String state = " ";
public int id = 0;

boolean loop = true;
Scanner input = new Scanner(System.in);
String choice = " ";
String key = " ";
Collection<House> list = new ArrayList<>();

public House(int id, String name, String telephone, String adress, String sent, String state) {
this.id = id;
this.name = name;
this.telephone = telephone;
this.adress = adress;
this.sent = sent;
this.state = state;
}

public House() {

}

public String getName() {
return name;
}

public String getTelephone() {
return telephone;
}

public String getAdress() {
return adress;
}

public String getSent() {
return sent;
}

public String getState() {
return state;
}

public int getId() {
return id;
}

public void setName(String name) {
this.name = name;
}

public void setTelephone(String telephone) {
this.telephone = telephone;
}

public void setAdress(String adress) {
this.adress = adress;
}

public void setSent(String sent) {
this.sent = sent;
}

public void setState(String state) {
this.state = state;
}

public void setId(int id) {
this.id = id;
}

public void HouseMenu( ) {
do{
System.out.println("-----------------------房屋出租系统---------------------");
System.out.println("\t\t 1 新增房源");
System.out.println("\t\t 2 查找房屋");
System.out.println("\t\t 3 删除房屋");
System.out.println("\t\t 4 修改房屋信息");
System.out.println("\t\t 5 房屋列表");
System.out.println("\t\t 6 退 出");
System.out.print("请选择(1-6)");
key = input.next( );

switch(key){
case"1" : this.addHouse( );
break;
case"2" : this.searchHouse( );
break;
case"3" : this.deleteHouse( );
break;
case"4" : this.setHouse( );
break;
case"5" : this.HouseList( );
break;
case"6" : this.exit( );
break;
default :
System.out.println("输入有误,重新选择。");
}
}while(loop);
}

public void addHouse() {
System.out.println("------------添加房屋--------------");
System.out.print("编号: ");
id = input.nextInt();
System.out.print("姓名: ");
name = input.next();
System.out.print("电话: ");
telephone = input.next();
System.out.print("地址: ");
adress = input.next();
System.out.print("月租: ");
sent = input.next();
System.out.print("状态(未出租/已出租): ");
state = input.next();
House house = new House(id, name , telephone , adress, sent, state);
list.add(house);
System.out.println("-------------添加完成--------------");
}

public void searchHouse() {
System.out.println("--------------查找房屋--------------");
System.out.print("请输入你要查找的id: ");
int n = input.nextInt();
for(House house : list) {
if(house.getId() == n) {
System.out.println(house.getId()+ "\t" + house.getName() + "\t" + house.getTelephone() + "\t"
+ house.getAdress() + "\t" + house.getSent() + "\t" + house.getState());
}
}
}

public void deleteHouse() {
System.out.println("-----------删除房屋------------");
int n;

while(true) {
System.out.print("请选择待删除房屋编号(-1退出): ");
n = input.nextInt();
if(n == -1)
break;
System.out.println("确认是否删除(Y/N): 请小心选择: ");
System.out.print("请输入你的选择(Y/N): ");
choice = input.next();
if("N".equals(choice) || "Y".equals(choice)) {
break;
}
}

if(choice.equals("Y")){
for(House house : list ) {
if(house.getId() == n) {
list.remove(house);
}
}
System.out.println("-----------删除完成-----------");
}
}

public void setHouse() {
System.out.println("------------修改房屋-------------");
int m;
while(true) {
System.out.print("请选择待修改房屋编号(-1退出): ");
m = input.nextInt();
if(m == -1)
break;
for(House house : list) {
if(house.getId() == m) {
System.out.print("姓名(" + house.getName() + ")");
String name = input.next();
house.setName(name);

System.out.print("电话(" + house.getTelephone() + ")");
String telephone = input.next();
house.setTelephone(telephone);

System.out.print("地址(" + house.getAdress() + ")");
String adress = input.next();
house.setAdress(adress);

System.out.print("租金(" + house.getSent() + ")");
String sent = input.next();
house.setSent(sent);

System.out.print("状态(" + house.getState() + ")");
String state = input.next();
house.setState(state);
}
}
System.out.println("------------修改完成------------");
break;
}
}

public void HouseList() {
System.out.println("--------------房屋列表--------------");
System.out.println("编号\t房主\t电话\t地址\t月租\t状态(未出租/已出租)");
for(House house : list) {
System.out.println(house.getId()+ "\t" + house.getName() + "\t" + house.getTelephone() + "\t"
+ house.getAdress() + "\t" + house.getSent() + "\t" + house.getState());
}
System.out.println("--------------房屋列表完成--------------");
}

public void exit() {
while(true) {
System.out.print("请输入你的选择(Y/N)");
choice = input.next();
if("Y".equals(choice) || "N".equals(choice)){
break;
}
}
if(choice.equals("Y")) {
loop = false;
System.out.println("你退出了程序~~");
}
}

public static void main(String args[]) {
House newHouse = new House();
newHouse.HouseMenu();
}
}
...全文
183 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
m0_54814086 2021-05-11
  • 打赏
  • 举报
回复
我的问题解决了,非常感谢各位的耐心解答。
nayi_224 2021-05-10
  • 打赏
  • 举报
回复
引用 1 楼 狂奔的蜗牛已被占用 的回复:
这是一个数组越界问题:
catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
你再看看你的House 里面有值吗?没有值直接删除会报错的
是个鬼的数组越界,罚你重看一遍源码。 这是迭代时修改数组的异常,一般使用重建数组或者for循环替代
m0_54814086 2021-05-10
  • 打赏
  • 举报
回复
引用 1 楼 狂奔的蜗牛已被占用的回复:
这是一个数组越界问题:
catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
你再看看你的House 里面有值吗?没有值直接删除会报错的
house是有值的,执行了第一步新增房源之后再进行接下来的步骤。可以执行查找房屋,修改房屋信息,房屋列表,退出的步骤,但是却执行删除房屋步骤会报错。
=PNZ=BeijingL 2021-05-10
  • 打赏
  • 举报
回复
for(House house : list ) {
if(house.getId() == n) {
list.remove(house); //这里虽然 remove 了,但是你的循环还是继续遍历下一个, 但是你的list中已经少了1个元素
}
}

list.remove 处理不好经常会出现这类越界问题, 一般建议修改成迭代器来处理remove元素操作
  • 打赏
  • 举报
回复
引用 2 楼 m0_54814086 的回复:
[quote=引用 1 楼 狂奔的蜗牛已被占用的回复:]这是一个数组越界问题:
catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
你再看看你的House 里面有值吗?没有值直接删除会报错的
house是有值的,执行了第一步新增房源之后再进行接下来的步骤。可以执行查找房屋,修改房屋信息,房屋列表,退出的步骤,但是却执行删除房屋步骤会报错。[/quote] 虽然直接加break可以解决麻烦但还是不太建议这样使用吧,用for就可以随便删除;如果想详细了解for-each的运行机制,可以在这几个地方下断点:iterator() ;这是在使用for-each时首先会创建的对象,会调用:hasNext(),next()其中报错就是在next()中,有个校验:modCount != expectedModCount;具体的你可以debug跟进看看;也可以看看我下面回复一个老哥的答案。
  • 打赏
  • 举报
回复
引用 2 楼 m0_54814086 的回复:
[quote=引用 1 楼 狂奔的蜗牛已被占用的回复:]这是一个数组越界问题:
catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
你再看看你的House 里面有值吗?没有值直接删除会报错的
house是有值的,执行了第一步新增房源之后再进行接下来的步骤。可以执行查找房屋,修改房屋信息,房屋列表,退出的步骤,但是却执行删除房屋步骤会报错。[/quote] 在remove后面加一句:break;就可以了;
  • 打赏
  • 举报
回复
引用 3 楼 nayi_224 的回复:
[quote=引用 1 楼 狂奔的蜗牛已被占用 的回复:]这是一个数组越界问题:
catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
你再看看你的House 里面有值吗?没有值直接删除会报错的
是个鬼的数组越界,罚你重看一遍源码。 这是迭代时修改数组的异常,一般使用重建数组或者for循环替代[/quote] 仔细阅读了源码,发现我问题确实太大,之前一直没怎么关注过这个问题,只是知道不要在迭代中做删除操作。做了实验,主要差别还是2种for循环方式不同;for,for-each;使用for循环删除没问题; 在使用for-each的时候在遍历list的时候会使用到迭代器Iter,在循环的时候首先会new Iter();这里面有一个很重要的属性:expectedModCount;这个在for-each循环时候,会调用到的Iter对象的2个方法:hasNext(),next();这2个方法都不涉及到对expectedModCount的改变;因此,expectedModCount的值是不变的; 这里面使用for-each删除元素的时候: 方法调用顺序: 1.hasNext();确定list中是否还有值; 2.next();检查modCount != expectedModCount(报错的根源:这个判断是用来检查list是否被修改) 3.list.remove();删除元素:modCount ++;(在ArrayList被修改时:remove,add,都会modCount++) 每一次循环都会按照这个顺序调用方法;因此可以看到,在第一次调用remove时,不会报错的;因为在第一次调用remove之前,ArrayList没有被修改所以:modCount== expectedModCount;第一次的删除会成功的; 那为什么会报错呢? 在第一次删除成功之后,如果不停止循环继续 遍历,此时modCount =modCount +1;与expectedModCount已经不相等了; 这个时候按照顺序,会在remove之前先调用:next();在这个时候判断modCount 与expectedModCount时候是否相等?肯定不相等,这个时候就会报错了;因此在使用for-each循环删除数据时,删除完成之后就得break;不进入下一次的循环就没问题; 最后感谢老哥指正。。之前实在是误人子弟啊
  • 打赏
  • 举报
回复
这是一个数组越界问题:
catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
你再看看你的House 里面有值吗?没有值直接删除会报错的

62,630

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧