Java 8 - "Element" ob'ektini ro'yxat elementidan oling

Menda quyidagi kod bor:

public class A {
   private String name;
   private List bs;

   public A(String name) {
       this.name = name;
       this.bs = new ArrayList<>();
   }

   public void addB(B b) {
       bs.add(b);
   }
   public List getBs() {
       return bs;
   }    
}

public class B {
    private String name;

    public B(String name) {
        this.name = name;
    }

    public String toString() {
        return name;
    }   
}

Va quyidagi asosiy:

public static void main(String[] args) {
        A a1 = new A("A1");
        A a2 = new A("A2");

        B b1 = new B("B1");
        B b2 = new B("B2");
        B b3 = new B("B3");

        a1.addB(b1);
        a2.addB(b2);
        a1.addB(b3);

        System.out.println(a1.getBs());//[B1, B3]

        b3.getA();//How can I do this? => Should return A1

}

Endi A ning B ni olishim kerak. Qaysi biri mukammal ishlaydi.

Lekin, shuningdek, B ning A ni olish kerak.

Har B yagona. Shuning uchun har B faqat A ning a'zosi bo'lishi mumkin.

Buning eng yaxshi yo'li qanday?

3
B (string name, parent) bilan ikkita mustaqil o'zgaruvchiga ega B constructor package B A /code>. Keyin A : umumiy B yaratish
(string name)
uchun ushbu usulni yangi B (ismi, bu); ga qaytarish uchun usulni qo'shing. B
orqali B yaratishingiz mumkin .Agar B kodi A bo'lmasa, "yashamaydi" ichki kod sifatida A .
qo'shib qo'ydi muallif DVarga, manba
"Har bir B noyobdir, shuning uchun har bir B faqat A ning a'zosi bo'lishi mumkin" <- So'ngra siz oldin ma'lumotlar tuzilmangizni shu tarzda modellashtirishingiz kerak. Siz kiritgan kodda a1.addB (b1) ni bajarishni to'xtatadigan hech narsa yo'q; a2.addB (b1);
qo'shib qo'ydi muallif OH GOD SPIDERS, manba

6 javoblar

You can add A member to B

public class B {
    private String name;
    private A a;

    public B(String name) {
        this.name = name;
    }

    public String toString() {
        return name;
    }

    public void setA(A a) {
        this.a = a;
    }

    public A getA() {
        return this.a;
    }
}

And set it when adding B to A

public void addB(B b) {
    bs.add(b);
    b.setA(this);
}
6
qo'shib qo'ydi

Agar B dan A ni olish uchun A manziliga biror joy kerak.

  • each B can have a reference to the A
  • you can have a collection of all the As and search them to find which contains the B

B asoschisida A ni o'rnatishdan iborat bo'lgan narsaga erishishning bir usuli bu A a'zosi

class A {
    private final String name;
    private final List bs = new ArrayList<>();

    public A(String name) {
        this.name = name;
    }

    public B addB(String name) {
        bs.add(new B(this, name));
    }
}

class B {
    final A parent;
    final String name;

    B(A parent, String name) {
        this.parent = parent;
        this.name = name;
    }

    public A getA() { return parent; }

Bu bilan B faqat bitta A ga qo'shilishi mumkin va B kimligini biladi.

public static void main(String[] args) {
    A a1 = new A("A1");
    A a2 = new A("A2");

    B b1 = a1.addB("B1");
    B b2 = a2.addB("B2");
    B b3 = a2.addB("B3");

    System.out.println(a1.getBs());//[B1, B3]

    assert a2 == b3.getA();
3
qo'shib qo'ydi

@PeterLawrey aytganidek, men shunday qilaman:

public class A {
   private String name;
   private List bs;

   public A(String name) {
       this.name = name;
       this.bs = new ArrayList<>();
   }

   public void addB(B b) {
       bs.add(b, this);
   }
   public List getBs() {
       return bs;
   }    
}

public class B {
    private String name;
    private A referenceToA;

    public B(String name, A referenceToA) {
        this.name = name;
        this.referenceToA = referenceToA;
    }

    public String toString() {
        return name;
    }

    public A getReferenceToA()
    {
        return referenceToA;
    }
}
0
qo'shib qo'ydi

b3 has no idea (and it shouldnt, because of circular references) about which collection is holding it.

you have to instead do it the opposite: find if a1, a2 or a3 has b3

0
qo'shib qo'ydi

B ro'yxati A ga qo'shsangiz, Bga o'zingizning ota-onangiz nimani anglatishini ham ayting.

class A {
    private List children;

    public void addChild(B b) {
        if (b != null) {
            this.children.add(b);
            b.setParent(this);
        }
    }
}

class B {
    private A parent;

    public void setParent(A a) {
        if (a != null) {
            this.parent = a;
        }
    }
}

Dairesel mos yozuvlar va xotira xatolaridan ehtiyot bo'ling. Siz B tashkil qilasiz, bular A ro'yxati, va hokazo.

0
qo'shib qo'ydi
Bu misol. Men OSh qanday haqiqiy narsani kodlashini bilmayman. Bu faqatgina A ning B iborasi va B ning A ga ega bo'lgan vaziyatlari haqida fikr yuritish. Muammo bo'lishi mumkin.
qo'shib qo'ydi muallif duffymo, manba
Nima uchun aylana mos yozuvlar muammosi? bu xotiradan ko'proq foydalanmaydi, siz ushbu ogohlantirish orqali nimani nazarda tutasiz?
qo'shib qo'ydi muallif Peter Lawrey, manba

Ichki sinfdan foydalaning va nizolarni o'zgarmas holatda saqlang, shunday qilib siz tutarsızlıklar xavfi yo'q:

class A {

    private final String name;
    private final List bs;

    public A(String name) {
        this.name = name;
        bs = new ArrayList<>();
    }

    public B addB(String name) {
        B b = new B(name);
        bs.add(b);
        return b;
    }

    public List getBs() {
        return Collections.unmodifiableList(bs);
    }


    class B {

        private final String name;

        public B(String name) {
            this.name = name;
        }

        public A getA() {
            return A.this;
        }

    }

}
0
qo'shib qo'ydi