Java 如何实现ArrayList以添加;“房间”;反对;“房子”;物品清单?

Java 如何实现ArrayList以添加;“房间”;反对;“房子”;物品清单?,java,generics,arraylist,Java,Generics,Arraylist,我目前正在做一项作业,我有两门课,一间教室,一间房子。作为任务的一部分,我需要启用house类将房间存储在阵列列表中: 实现从键盘读取House类中的室内信息的方法-这应根据需要调用room构造函数。创建一个零参数House构造函数来调用这个新方法 我很抱歉,如果这是含糊不清的,或者已经在其他地方得到了回答,但在试图理解其他类似问题的解释后,我不知道如何将它们应用到我的情况中 如何将房间的ArrayList应用于house类 House: import java.util.ArrayList;

我目前正在做一项作业,我有两门课,一间教室,一间房子。作为任务的一部分,我需要启用house类将房间存储在
阵列列表中:

实现从键盘读取
House
类中的室内信息的方法-这应根据需要调用
room
构造函数。创建一个零参数House构造函数来调用这个新方法

我很抱歉,如果这是含糊不清的,或者已经在其他地方得到了回答,但在试图理解其他类似问题的解释后,我不知道如何将它们应用到我的情况中

如何将
房间的
ArrayList
应用于house类

House

import java.util.ArrayList;
import java.util.Scanner;

public class House {

private int idNum;
private static int internalCount = 0; 
private ArrayList rooms = new ArrayList();
private String address;
private int numRooms;
private String houseType; 

public House (String address, int numRooms, String houseType) {
idNum = internalCount++;

this.address = address;
this.numRooms = numRooms;
this.houseType = houseType;     
}


public House () {
int i = 0;  
idNum = ++internalCount;

Scanner scan = new Scanner(System.in);
scan.useDelimiter("\n");

System.out.println("Enter address of house:");   
address = scan.next();

System.out.println("Enter number of rooms:");   //Number of rooms in the House
numRooms = scan.nextInt();

 while (i < numRooms)  //This will loop until all rooms have been described
 {
 add.room = new Room[100]; //I understand this is incorrect but I don't know what  I should have in here
 i++;
 }

 System.out.println("Enter type of house:");
 houseType = scan.next();
 }
 public void addroom(String description, double length, double width)
 {

 }

 int getIdNum() {
 return idNum;
 }


 @Override

 public String toString()
 {
  String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  report += "Address: " + address + "\n";
  report += "No. of Rooms: " + numRooms + "\n";
  report += "House Type: " + houseType+ "\n";
  report += "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";

  return report;
 }
 }
import java.util.Scanner;

public class Room {

private String description;
private double length;
private double width;

public Room (String description, double length, double width) {
this.description = description;
this.length = length;
this.width = width;     
}

public Room () {
Scanner scan = new Scanner(System.in);
scan.useDelimiter("\n");

System.out.println("Enter description of room:");
description = scan.next();

System.out.println("Enter length of room:");
length = scan.nextDouble();

 System.out.println("Enter width of room:");
 width = scan.nextDouble();
 }

 /*
 * Calculates and returns area of Room
 */
 public double getArea () {
 return length*width;
 }


 @Override
 public String toString() {
 return description + "- Length: " + length + "m; Width: " + width + 'm';
 }   
}
你查过ArrayList了吗

你需要像这样的东西:

rooms.add(new Room());
此外,我将使用Java泛型并定义列表:

private ArrayList<Room> rooms;
私人ArrayList房间;
这将使用并确保这只能是房间列表,而不是其他对象。这将为您省去一些痛苦,因为您不能无意中将房屋添加到房间列表中(由于编程错误)

您是否已检查了ArrayList

你需要像这样的东西:

rooms.add(new Room());
此外,我将使用Java泛型并定义列表:

private ArrayList<Room> rooms;
私人ArrayList房间;

这将使用并确保这只能是房间列表,而不是其他对象。这将为您节省一些痛苦,因为您不能无意中将房屋添加到房间列表中(通过编程错误)

使用
房间类型的列表

List<Room> rooms = new ArrayList<Room>();

while (i < numRooms)  //This will loop until all rooms have been described
{
  rooms.add(new Room()); //this is correct 
  i++;
}
List rooms=new ArrayList();
while(i
使用列表到
房间
类型

List<Room> rooms = new ArrayList<Room>();

while (i < numRooms)  //This will loop until all rooms have been described
{
  rooms.add(new Room()); //this is correct 
  i++;
}
List rooms=new ArrayList();
while(i因为这是家庭作业,我不会完全解决它,但给你一个基础工作……< /P>
public class House {
  private List<Room> rooms;

  public House() {
    rooms = new ArrayList<Room>();
  }

  public void addRoom(...) {
    Room room = new Room(...);
    rooms.add(room);
  }

  public int numRooms() {
    return rooms.size();
  }

  public Room getRoom(int roomNumber) {
    return rooms.get(roomNumber);
  }
}

public class Room {
  public Room(...) {
    ...
  }
}
公共类房屋{
私人名单室;
酒店(){
房间=新的阵列列表();
}
公共房间(…){
房间=新房间(…);
房间。添加(房间);
}
公共int numRooms(){
返回房间。大小();
}
公共房间getRoom(内部房间号){
返回房间。获取(房间号);
}
}
公共教室{
公共房间(……){
...
}
}

问题?

< P>因为这是家庭作业,我不完全解决它,但给你一个工作的基础从…

public class House {
  private List<Room> rooms;

  public House() {
    rooms = new ArrayList<Room>();
  }

  public void addRoom(...) {
    Room room = new Room(...);
    rooms.add(room);
  }

  public int numRooms() {
    return rooms.size();
  }

  public Room getRoom(int roomNumber) {
    return rooms.get(roomNumber);
  }
}

public class Room {
  public Room(...) {
    ...
  }
}
ArrayList<Room> rooms = new ArrayList<Room>();
rooms.add(new Room());
公共类房屋{
私人名单室;
酒店(){
房间=新的阵列列表();
}
公共房间(…){
房间=新房间(…);
房间。添加(房间);
}
公共int numRooms(){
返回房间。大小();
}
公共房间getRoom(内部房间号){
返回房间。获取(房间号);
}
}
公共教室{
公共房间(……){
...
}
}
问题?

ArrayList rooms=new ArrayList();
ArrayList<Room> rooms = new ArrayList<Room>();
rooms.add(new Room());
房间。添加(新房间());
ArrayList房间=新建ArrayList();
房间。添加(新房间());

更改构造器方法添加一些有关房间的代码
ArrayList

public House(..., ArrayList aRooms) {

 ...
 rooms = aRooms;
}

public House() {

 ...
 // It's better to instantiate the rooms array here.
 rooms = new ArrayList<Room>();
}

更改构造函数方法,添加一些有关房间的代码
ArrayList

public House(..., ArrayList aRooms) {

 ...
 rooms = aRooms;
}

public House() {

 ...
 // It's better to instantiate the rooms array here.
 rooms = new ArrayList<Room>();
}
更改此循环:

while (i < numRooms)  //This will loop until all rooms have been described
{
  add.room = new Room[100]; //I understand this is incorrect but I don't know what I  should have in here
 i++;
}
while(i
致:

while(i
更改此循环:

while (i < numRooms)  //This will loop until all rooms have been described
{
  add.room = new Room[100]; //I understand this is incorrect but I don't know what I  should have in here
 i++;
}
while(i
致:

while(i
提出与arraylist相关的特定问题。提出与arraylist相关的特定问题。谢谢大家!这些答案中的任何一个都可以帮我澄清这一点!我真希望我5小时前就请求帮助。再次谢谢你们,谢谢你们!这些答案中的任何一个都可以帮我澄清这一点!我真希望我5小时前就请求帮助。再次感谢你。重新审视我的问题,这是完美的!非常感谢。重新审视我的问题,这是完美的!非常感谢。