【问题标题】:How do I implement an ArrayList to add "Room" objects to the "House" objects list? [closed]如何实现 ArrayList 以将“房间”对象添加到“房屋”对象列表中? [关闭]
【发布时间】:2013-03-12 17:47:54
【问题描述】:

我目前正在做作业,我有两个班级房间和房子。作为作业的一部分,我需要启用房屋类将房间存储在ArrayList:

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

如果这含糊不清或已在其他地方得到回答,但我试图理解其他类似查询的解释,我不知道如何将它们应用于我的情况,我深表歉意。

我如何将Room 的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;
 }
 }

Room:

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相关的具体问题。

标签: java generics arraylist


【解决方案1】:

您检查过API 的ArrayList 吗?

你需要这样的东西:

rooms.add(new Room());

此外,我将使用 Java 泛型并这样定义列表:

private ArrayList<Room> rooms;

这利用了generics,并确保这只能是房间列表,而不是杂项对象。它会为您省去一些麻烦,因为您不会无意中将房屋添加到您的房间列表中(通过编程错误)

【讨论】:

    【解决方案2】:

    由于这是作业,我不会全部解决,但会给你一个工作的基础......

    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(...) {
        ...
      }
    }
    

    问题?

    【讨论】:

    • 重新审视了我的问题,这是完美的!谢谢!
    【解决方案3】:

    使用特化列表到Room 类型

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

    【讨论】:

    • 感谢大家!这些答案中的任何一个都可以为我解决这个问题!我只是希望我在大约 5 小时前寻求帮助。谢谢阿甘。
    【解决方案4】:
    ArrayList<Room> rooms = new ArrayList<Room>();
    rooms.add(new Room());
    

    【讨论】:

      【解决方案5】:

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

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

      要将房间添加到您的房间ArrayList,只需执行类似操作(不要忘记检查ArrayList API、http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html)。

      ArrayList<Room> rooms = new ArrayList<Room>();
      rooms.add(new Room(...));
      // Add here all the rooms you need.
      
      House house = new House(rooms);
      
      // You can also create a method into the House class to allow adding new rooms once you have created a House object.
      public void addRoom(Room room) {
      
       this.rooms.add(room);
      }
      

      【讨论】:

        【解决方案6】:

        改变这个循环:

        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 < numRooms) 
        {
        Room newRoom = new Room();
        rooms.add(newRoom);
        i++;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-07-09
          • 2020-07-08
          • 1970-01-01
          • 1970-01-01
          • 2019-05-29
          • 1970-01-01
          • 2023-03-22
          • 1970-01-01
          相关资源
          最近更新 更多