使用数组列表指针而不是多个循环?

huangapple go评论60阅读模式
英文:

Using arrayList pointers instead of multiple loops?

问题

我正在尝试编写一个包含许多类的程序,在一个名为"Dorm"的类中,我有一个Blocks的ArrayList,在"Block"类中,我有一个Rooms的ArrayList,在"Room"类中,我有一个"Students"的ArrayList。

我正在尝试通过另一个名为"Manager class"的类来访问可用房间的数量(至少有一个空位的房间)。我被告知可以在管理器类中创建另一个ArrayList,将其用作指针,并搜索整个宿舍的空房间。

我的问题是,这将如何工作?

我的代码尚不完整,所以我不确定它是否有效...

public static void availableRooms() { //显示宿舍中可用的房间。
    
    Dorms dormitory = new Dorms();
    Room room1 = new Room();
    
    for (int i = 0; i < dormitory.getBlocks().size(); i++)
        for (int j = 0; j < Block.getRoomList().size(); j++) {
            if (!(room1.getStudentList().get(room1.getRoomCapacity()).equals(null)))
                System.out.print("\t" + room1.getStudentList().get(i) + "\t");
        }
}

请注意,您的代码可能有一些错误,例如room1.getStudentList().get(room1.getRoomCapacity()).equals(null)这一行可能会导致空指针异常。您可能需要进一步完善代码以实现您的目标。

英文:

I am trying to write a program that contains many classes and in one class called "Dorm",I have an arrayList of Blocks,and in the "Block" class,I have an arrayList of Rooms,and in the "Room" class,I have an arrayList of "Students".
I am trying to access the number of available rooms(the rooms that at least have one empty space) through another class called the "Manager class". I have been told that I can just create another arrayList in the manager class to be used as a pointer and search up the empty rooms of the whole dormitory.

My question is,how is this going to work?

ps:This is what I wrote:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

public static void availableRooms() { //Shows the available rooms in the dormitory.
		
		Dorms dormitory = new Dorms();
    	Room room1 = new Room();
		
		for(int i=0;i&lt;dormitory.getBlocks().size();i++)
			for(int j=0;j&lt;Block.getRoomList().size();j++) {
				if(!(room1.getStudentList().get(room1.getRoomCapacity()).equals(null)))
					System.out.print(&quot;/t&quot; + room1.getStudentList().get(i) + &quot;/t&quot;);
				}
		
		
		
  	}

<!-- end snippet -->

My code isn't complete yet,so I'm not sure if it works...

答案1

得分: 1

以下是翻译好的内容:

你能分享你的代码/暂定代码吗?并清楚地指出有什么问题?

话虽如此,除非受到特定约束,否则应该利用封装和单一职责原则(参见维基上的SOLID)通过保持实现细节私有化并将任务委托给更相关的类来操作。

你可能会有类似这样的代码:

class Dorm {
  private List<Block> blocks = ...
  ...
  public int getAvailableRooms() {
      int total = 0;
      for (Block b : blocks) {
        total += b.getAvailableRooms();
      }
      return total;
   }
 }

class Block {
  private List<Room> rooms = ....
  ...
  public int getAvailableRooms() {
    int total = 0;
    for (Room r : rooms) {
      if (! r.isFull()) {
         total++;
      }
    }
}

class Room {
   private int capacity = ...
   private List<Student> students = ..
   ...
   public boolean isFull() {
       return capacity == students.size();
   }
}

在这里,Manager类持有Dorm的实例,并且仅使用getAvailableRooms()方法,该方法在背后委托给底层的Blocks并聚合结果...以此类推。

英文:

Could you share your code/tentative? and clearly specify what's not working?

This being said, unless tied to specific constraints, one should make use of encapsulation and single responsibility principle (see SOLID on wiki) by keeping implementation details private and delegating tasks to the more relevant classes.

You may have something like:

class Dorm {
  private List&lt;Block&gt; blocks = ...
  ...
  public int getAvailableRooms() {
      int total = 0;
      for (Block b : blocks) {
        total += b.getAvailableRooms();
      }
      return total;
   }
 }

class Block {
  private List&lt;Room&gt; rooms = ....
  ...
  public int getAvailableRooms() {
    int total = 0;
    for (Room r : rooms) {
      if (! r.isFull()) {
         total++;
      }
    }
}

class Room {
   private int capacity = ...
   private List&lt;Student&gt; students = ..
   ...
   public boolean isFull() {
       return capacity == students.size();
   }
}

Where the Manager class, holding (an) instance(s) of Dorm, just make use of the getAvailableRooms() method which behind the scene delegate to the underlining Blocks and aggregate result... and so on.

huangapple
  • 本文由 发表于 2020年5月5日 21:02:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/61613832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定