如何将一个对象作为另一个对象的子对象添加(Java)

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

How to add an object as a child of an object java

问题

我被卡在一个关于将一个对象作为另一个对象的子对象添加的问题上,已经好几个小时了:我有这个名为 "Snapo" 的类,它包含了 Snapo 对象,它是一个包含第一个索引 (m) 的数组,这个索引是一个整数,紧接着是一个 Snapo 列表 --> [m (s1 s2 ... sn)]。现在我编写了代码,将一个只有 "m" 属性的子对象添加到主要的 Snapo 对象中,但是我无法理解如何将一个 Snapo 对象添加到主要的 Snapo 对象中,就像这样:

[8 ([5 ([7] [6])] [1 ([4] [3] [2])])] --> 其中 "8"、"5" 和 "1" 是 "m",而其他数字是 Snapo 列表

变量的声明:

private final static int SIZE = 100;
private int m;
public int[] arr = new int[SIZE];
private int top=0;

Snapo (int m) {
this.m = m;
arr[0] = this.m;
}

这是类的构造函数 ^

public void addChild (Snapo s) {
    top++;
    for (int i = top; i > 1; i--) {
        arr[i] = arr[i - 1];
    }
    arr[1] = s.arr[0];
}

这是添加单个数字到主要的 Snapo 对象的 addChild 方法 ^(top 被初始化为私有 int = 0)

我的程序可以处理像这样的输入:

Snapo s1 = new Snapo(1);

s1.addChild(new Snapo(2));
s1.addChild(new Snapo(3));

但很显然不能处理像这样的输入:

Snapo s3 = new Snapo(8);
s3.addChild(s1);
s3.addChild(s2);
英文:

i'm stuck since hrs to a problem that regards adding an object as a child of another object: i have this class "Snapo" which contains the object Snapo, it is an array that contains the first index (m) which is an integer, and it's followed by a list Snapo --> [m (s1 s2 ... sn)]. Now i wrote the code to add a child which has only the "m" attribute to the main Snapo, but i can't understand how to add a Snapo to the main Snapo, like:
[8 ([5 ([7] [6])] [1 ([4] [3] [2])])] --> where "8" , "5" and "1" are the "m" and the other numbers are the list of Snapo

declaration of variables:

private final static int SIZE = 100;
private int m;
public int[] arr = new int[SIZE];
private int top=0;

Snapo (int m) {
    this.m = m;
    arr[0] = this.m;
}

This is the constructor of the class ^

public void addChild (Snapo s) {
    top++;
    for (int i = top; i > 1; i--) {
        arr[i] = arr[i - 1];
    }
    arr[1] = s.arr[0];
}

This is the addChild method that adds a single number to the main Snapo ^ (top is initialized as a private int = 0)

My program works with inputs like:

Snapo s1 = new Snapo(1);

s1.addChild(new Snapo(2));
s1.addChild(new Snapo(3));

But it obviously doesn't with inputs like:

Snapo s3 = new Snapo(8);
s3.addChild(s1);
s3.addChild(s2);

答案1

得分: 0

或许这个示例可能会有用<br>

import java.util.ArrayList;
import java.util.List;
class TestAddC
{
    public static void main(String args[])
    {
        TestAddC t= new TestAddC();
        Snapo s1 = t.new Snapo("s1");
        s1.addChild(t.new Snapo("s2"));
        Snapo s3 = t.new Snapo("s3");
        s1.addChild(s3);
        Snapo s4 = t.new Snapo("s4");
        s3.addChild(s4);
        
        System.out.println(s1);
        System.out.println(s3);
    }
    class Snapo
    {
        List<Snapo> listC= new ArrayList<Snapo>();
        String name;
        Snapo(String name)
        {
            this.name = name;
        }
        public void addChild(Snapo obj)
        {
            listC.add(obj);
        }
        public String toString()
        {
            StringBuffer sb = new StringBuffer();
            sb.append("parent="+this.name+"\n");
            //only first level
            listC.forEach(t->sb.append("child="+t.name+"\n"));
            return sb.toString();
        }
    }
}

输出:

parent=s1
child=s2
child=s3

parent=s3
child=s4

版本2:使用数组

class TestAddC
{
    public static void main(String args[])
    {
        TestAddC t= new TestAddC();
        Snapo s1 = t.new Snapo("s1");
        s1.addChild(t.new Snapo("s2"));
        Snapo s3 = t.new Snapo("s3");
        s1.addChild(s3);
        Snapo s4 = t.new Snapo("s4");
        s3.addChild(s4);
        s3.addChild(s4);
        s3.addChild(s4);
        s3.addChild(s4);
        s3.addChild(s1);
        
        System.out.println(s1);
        System.out.println(s3);
    }
    class Snapo
    {
        
        //add here up to how many children
        Snapo[] listArr = new Snapo[3];
        int counter = 0;
        String name;
        Snapo(String name)
        {
            this.name = name;
        }
        private int getNext()
        {
            System.out.println(counter);
            return (counter<listArr.length)? counter++ :(listArr.length -1);
        }
        public void addChild(Snapo obj)
        {
            listArr[getNext()] = obj;
        }
        public String toString()
        {
            StringBuffer sb = new StringBuffer();
            sb.append("parent="+this.name+"\n");
            //only first level
            for(int i=0;i<listArr.length;i++)
            {
                if(listArr[i] instanceof Snapo )
                {
                    sb.append("child="+listArr[i].name+"\n");
                }
            }
            return sb.toString();
        }
    }
}

输出:

parent=s1
child=s2
child=s3
//only 3 since arrsize is 3 (added 4, always last one have the last child)
parent=s3
child=s4
child=s4
child=s1
英文:

Maybe this example could be useful <br>

import java.util.ArrayList;
import java.util.List;
class TestAddC
{
public static void main(String args[])
{
TestAddC t= new TestAddC();
Snapo s1 = t.new Snapo(&quot;s1&quot;);
s1.addChild(t.new Snapo(&quot;s2&quot;));
Snapo s3 = t.new Snapo(&quot;s3&quot;);
s1.addChild(s3);
Snapo s4 = t.new Snapo(&quot;s4&quot;);
s3.addChild(s4);
System.out.println(s1);
System.out.println(s3);
}
class Snapo
{
List&lt;Snapo&gt; listC= new ArrayList&lt;Snapo&gt;();
String name;
Snapo(String name)
{
this.name = name;
}
public void addChild(Snapo obj)
{
listC.add(obj);
}
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append(&quot;parent=&quot;+this.name+&quot;\n&quot;);
//only first level
listC.forEach(t-&gt;sb.append(&quot;child=&quot;+t.name+&quot;\n&quot;));
return sb.toString();
}
}
}

Output

parent=s1
child=s2
child=s3

parent=s3
child=s4

Version 2 : with arrays

class TestAddC
{
public static void main(String args[])
{
TestAddC t= new TestAddC();
Snapo s1 = t.new Snapo(&quot;s1&quot;);
s1.addChild(t.new Snapo(&quot;s2&quot;));
Snapo s3 = t.new Snapo(&quot;s3&quot;);
s1.addChild(s3);
Snapo s4 = t.new Snapo(&quot;s4&quot;);
s3.addChild(s4);
s3.addChild(s4);
s3.addChild(s4);
s3.addChild(s4);
s3.addChild(s1);
System.out.println(s1);
System.out.println(s3);
}
class Snapo
{
//add here up to how many children
Snapo[] listArr = new Snapo[3];
int counter = 0;
String name;
Snapo(String name)
{
this.name = name;
}
private int getNext()
{
System.out.println(counter);
return (counter&lt;listArr.length)? counter++ :(listArr.length -1);
}
public void addChild(Snapo obj)
{
listArr[getNext()] = obj;
}
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append(&quot;parent=&quot;+this.name+&quot;\n&quot;);
//only first level
for(int i=0;i&lt;listArr.length;i++)
{
if(listArr[i] instanceof Snapo )
{
sb.append(&quot;child=&quot;+listArr[i].name+&quot;\n&quot;);
}
}
return sb.toString();
}
}
}

Output

parent=s1
child=s2
child=s3
//only 3 since arrsize is 3 (added 4, always last one have the last child)
parent=s3
child=s4
child=s4
child=s1

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

发表评论

匿名网友

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

确定