有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何实现ArrayList以将“Room”对象添加到“House”对象列表中?

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

Implement a method to read in room information in the House class from the keyboard — this should call the Room constructor as required. Create a zero-argument House constructor to call this new method.

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

如何将RoomArrayList应用于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';
 }   
}

共 (6) 个答案

  1. # 1 楼答案

    将列表专门化为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++;
    }
    
  2. # 2 楼答案

    更改此循环:

    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++;
    }
    
  3. # 3 楼答案

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

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

    要将文件室添加到文件室ArrayList,只需执行类似操作(不要忘记检查ArrayListAPI,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);
    }
    
  4. # 4 楼答案

    因为这是家庭作业,我不会完全解决它,但给你一个工作的基础。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(...) {
        ...
      }
    }
    

    有问题吗

  5. # 5 楼答案

    ArrayList<Room> rooms = new ArrayList<Room>();
    rooms.add(new Room());
    
  6. # 6 楼答案

    你检查过ArrayList的API了吗

    你需要像这样的东西:

    rooms.add(new Room());
    

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

    private ArrayList<Room> rooms;
    

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