我写了一个房屋出租系统,里面有房主的姓名,电话,地址,房号id和出租状态
请问一下,再新增房屋的时候,都先判断一下房屋够不够,如果不够,那就新增一个数组来进行扩容,但是扩容新增房屋的时候为什么我每次打印房屋列表的时候都说我越界了呢?我也没越界呀
在主函数里我创建了这几个对象,然后对象数组就设置为4个,再进行初始化
public class MainRent {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Hourse_s[] hh=new Hourse_s[4];
for(int i=0;i<hh.length;i++){
hh[i]=new Hourse_s(null,null,null,0,null,null);
}
Hourse h=new Hourse(0,4);
HourseMethod hm=new HourseMethod();
new Menu().menu(hh,h,hm);
}
}
这个是房屋Hourse_s hh以及房屋Hourse h的类信息
public class Hourse {
private int len;//数组长度
private int capacity;
public class Hourse_s {
private String name;//姓名
private String numberPhone;//电话
private String adress;//地址
private double rent;//租金
private String status;//状态
private String id;//编号
public Hourse_s(String name, String numberPhone,
String adress, double rent, String status, String id) {
this.name = name;
this.numberPhone = numberPhone;
this.adress = adress;
this.rent = rent;
this.status = status;
this.id = id;
}
每个对象我都已经将各个属性封装了,这里不展示
一个含有所有操作方法的类,在每次所有扩容的时候,都先进行数组的初始化,将新数组拷贝至旧数组(initialize方法)
public class HourseMethod {
public void AddHourse(Hourse_s[] hh, Hourse h){
//增加之前先判断长度,看是否需要扩容
Scanner scanner = new Scanner(System.in);
if(h.getLen()==h.getCapacity()-1){
//扩容操作
System.out.println("正在扩容。。。");
Hourse_s[] newhourse = new Hourse_s[h.getCapacity() * 2];
Initialize(hh,newhourse,h);
hh=newhourse;
h.setCapacity(h.getCapacity()*2);
System.out.println("成功扩容!");
}
//先确定编号
hh[h.getLen()].setId(h.getLen()+"");
//由用户输入信息
System.out.print("姓名:");
hh[h.getLen()].setName(scanner.next());
System.out.print("电话:");
hh[h.getLen()].setNumberPhone(scanner.next());
System.out.print("地址:");
hh[h.getLen()].setAdress(scanner.next());
System.out.print("月租:");
hh[h.getLen()].setRent(scanner.nextDouble());
System.out.print("状态:(未出租/已出租):");
hh[h.getLen()].setStatus(scanner.next());
System.out.println("----------------------添加完成--------------------\n");
//改变h的值
h.setLen(h.getLen()+1);
System.out.println("数组容量:"+hh.length+" h.len="+h.getLen()+" h.capacity="+h.getCapacity());
}
//初始化并且赋值数据
public void Initialize(Hourse_s[] hh,Hourse_s[] newHourse,Hourse h){
//将创建这些空间初始化
for(int i=0;i< newHourse.length;i++){
newHourse[i]=new Hourse_s(null,null,null,0,null,null);
}
//再将原数组拷贝到新数组
for(int i=0;i<h.getLen();i++){
newHourse[i]=hh[i];
}
}
但是到了打印的时候显示我的数组越界了,我很是疑惑,请教一下各位学者
public void Show(Hourse_s[] hh, Hourse h){
System.out.println("编号\t姓名\t\t电话\t\t地址\t\t月租\t\t状态(未出租,已出租)");
for(int i=0;i<h.getLen();i++){
System.out.println(hh[i].getId()+"\t\t"+hh[i].getName()+"\t\t"+hh[i].getNumberPhone()
+"\t\t"+hh[i].getAdress()+"\t\t"+hh[i].getRent()+"\t\t"+hh[i].getStatus());
}
System.out.println("----------------------房屋列表完成----------------------");
}
我已经执行了这一句话,但是为什么在show里面的方法,hh.length的大小又变回原来的样子了
菜单详情
public void menu(Hourse_s[] hh, Hourse h,HourseMethod hm) {
int choose;
boolean y=true;
//引入输入类
Scanner scanner = new Scanner(System.in);
do {
System.out.println("----------------------房屋出租系统--------------------\n" +
"\t\t\t1.新增房屋\n" +
"\t\t\t2.查找房屋\n" +
"\t\t\t3.删除房屋\n" +
"\t\t\t4.修改房屋信息\n" +
"\t\t\t5.房屋列表\n" +
"\t\t\t6.退出系统\n");
System.out.println("请选择(1-6)");
choose = scanner.nextInt();
switch (choose) {
case 1:
System.out.print("----------------------添加房屋--------------------\n");
hm.AddHourse(hh,h);
break;
case 2:
System.out.println("----------------------查找房屋--------------------");
hm.CheckHorese(hh,h);
break;
case 3:
System.out.println("----------------------删除房屋--------------------");
hm.DelHourse(hh, h);
break;
case 4:
System.out.println("----------------------修改房屋信息-----------------");
hm.SetHourse(hh, h);
break;
case 5:
System.out.println("----------------------房屋列表--------------------");
hm.Show(hh,h);
break;
case 6:
char s;
System.out.println("确认退出:(y)");
s=scanner.next().charAt(0);
if(s=='y'){
y=false;
break;
}
}
} while (y==true);
}