ArrayList<X> 是聚合还是组合?

Is ArrayList<X> an aggregation or composition?

我正在准备我的编程考试并且遇到了这个问题,我知道在聚合中对象是借来的,而在组合中对象是拥有的。答案是作文吗?

ArrayList<X>X 的聚合还是 X 的组合?

ArrayList<Point> pts = new ArrayList<Point>();
Point p = new Point(0., 0., 0.);
pts.add(p);
p.setX( 10.0 );
System.out.println(p);
System.out.println(pts.get(0));

由于点在数组之外是真实存在的,所以它是一个聚合。

https://www.visual-paradigm.com/guide/uml-unified-modeling-language/uml-aggregation-vs-composition/

如所述:

Aggregation implies a relationship where the child can exist independently of the parent. Example: Class (parent) and Student (child). Delete the Class and the Students still exist.

Composition implies a relationship where the child cannot exist independent of the parent. Example: House (parent) and Room (child). Rooms don't exist separate to a House.

来自here

Simple rules:

  1. A "owns" B = Composition : B has no meaning or purpose in the system without A
  2. A "uses" B = Aggregation : B exists independently (conceptually) from A

因此,这实际上取决于您的型号。列表中的元素是否可以在没有列表的情况下存在。元素是不是一定要放到列表中才有意义?

ArrayList<Point>的情况下,我认为是聚合。