我一直因为一个问题而陷入困境,该问题涉及将一个对象添加为另一个对象的子对象:我有一个包含对象Snapo的类“ Snapo”,它是一个包含第一个索引(m)的数组,该索引是一个整数,然后是列表Snapo-> [m(s1 s2 ... sn)]。现在,我编写了代码,以向主Snapo添加仅具有“ m”属性的子项,但是我不明白如何向主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);

最佳答案

也许这个例子可能有用

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

10-04 14:58