简体   繁体   English

在Java中访问深度嵌套的对象Box

[英]Accessing a deeply nested object Box in Java

Let's say you have a box that could contain a maximum of 50 objects. 假设您有一个盒子,最多可以包含50个对象。 Weirdly, the box could also contain another box (which could also contain a maximum of 50 objects), and that this "nested" box does not occupy any space in the outer box. 奇怪的是,该框还可以包含另一个框(最多也可以包含50个对象),并且此“嵌套”框不占用外部框的任何空间。

The "nested" box could also contain another box, and so on... ad infinitum. “嵌套”框还可以包含另一个框,依此类推……无限。

Thus, I wrote a service class Box as follows: 因此,我编写了一个服务类Box,如下所示:

class Box {

  private Box nestedBox;

  public Box getBox() {
    return nestedBox;
  }

  //other code
}

The user is allowed to create as many boxes as he likes. 允许用户创建任意数量的框。 But each box he creates will be nested inside the "deepest" box. 但是他创建的每个框都将嵌套在“最深”框内。

So, for example, if he wishes to create three boxes: the first box will be created first, then the second box will be created inside the first box, then finally, the third and last box will be created inside the second box. 因此,例如,如果他希望创建三个框:将首先创建第一个框,然后在第一个框内创建第二个框,然后最后在第二个框内创建第三个和最后一个框。

How do I go about writing a program that does this creation? 我该如何编写一个可以完成此创建的程序? I was told to use something like recursion . 有人告诉我要使用递归之类的方法

Here is my attempt: 这是我的尝试:

public static void main(String[] args) {

  int numOfBoxes;
  Scanner sc = new Scanner(System.in);

  numOfBoxes = sc.nextInt();

  if (numOfBoxes == 1) {
    Box b = new Box();
  } else {
    Box b = new Box();
    for (int i = 1; i < numOfBoxes; i++) {
      b.getBox() = new Box();
    }
  }

}

I really appreciate it if someone could help me with this. 如果有人可以帮助我,我真的很感激。 Feel free to change the class Box and the main method. 随时更改Box的类和主要方法。 I was told to use recursion, but I couldn't figure this out. 有人告诉我使用递归,但我无法弄清楚。

As you've described it (with each box containing a max of 1 box) what you have is effectively a linked list. 正如您所描述的(每个框最多包含1个框),您所拥有的实际上是一个链表。 You don't need recursion for that, just walk the list keeping the current box in a variable. 您不需要递归,只需遍历列表即可将当前框保留在变量中。

But possibly what you're trying to solve for (where a box can contain multiple boxes) is a tree, and what you need is a search function for that tree. 但是可能您要解决的问题(一个盒子可以包含多个盒子)是一棵树,而您需要的是该树的搜索功能。 Without knowing anything of the expected node distribution in your tree, I would highly recommend doing a DFS ( https://en.wikipedia.org/wiki/Depth-first_search ), as this is easy to implement and is performant on a broad array of tree types. 在不了解树中预期的节点分布的情况下,我极力建议您进行DFS( https://en.wikipedia.org/wiki/Depth-first_search ),因为它易于实现并且在广泛的范围内都表现出色树类型。

One way is to hold a reference to the most out box. 一种方法是保留对最外框的引用。

class Box{
    private Box nestedBox;
    public Box(){

    }
    public Box(Box nestedBox){
        this.nestedBox = nestedBox;
    }
}
public class Main {
    public static void main(String[] args) {
        int numOfBoxes = 4;
        Box nestedBox = null;
        for(int i = 0; i < numOfBoxes; i++){
            nestedBox = new Box(nestedBox);
        }
    }
}

Alternatively, if you really require building the boxes w/ a recursive solution, you can do it within the constructor. 或者,如果您确实需要使用递归解决方案来构建框,则可以在构造函数中进行操作。 Example: 例:

import java.util.Scanner;

class Box{

    private Box nestedBox;

    public Box(int numOfInnerBoxes){
        if (numOfInnerBoxes > 0) {
            this.nestedBox = new Box(numOfInnerBoxes - 1);
        }
        System.out.println("This box "+ this +" contains box "+ this.nestedBox +".");
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int numOfBoxes = sc.nextInt();
        Box outerBox = new Box(numOfBoxes-1);
        System.out.println("The outermost box is "+ outerBox +".");
    }

}

Example output: 输出示例:

$ java Box
4
This box Box@55f96302 contains box null.
This box Box@3d4eac69 contains box Box@55f96302.
This box Box@42a57993 contains box Box@3d4eac69.
This box Box@75b84c92 contains box Box@42a57993.
The outermost box is Box@75b84c92.

Note that every constructor decrements the number of inner boxes to create until zero is reached. 请注意,每个构造函数都会减少要创建的内部框的数量,直到达到零为止。 This is the essence of a recursive solution. 这是递归解决方案的本质。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM