我想你能做到這一點是這樣的:
import java.awt.Rectangle;
import java.awt.Shape;
public Shape[] ArrayRandomizer(int size) {
List<Shape> list = new ArrayList<Shape>();
if (size < 10 && size%10 != 0) {
return null; // array must be divided by 10 without modulo
}
else {
Random random = new Random();
Shape[] result = new Shape[size];
for (int r=0; r<4*(size/10); r++) {
Shape rectangle = new Rectangle(random.nextInt(), random.nextInt(), random.nextInt(), random.nextInt()); // standart awt constructor
list.add(rectangle);
}
for (int cir=0; cir<3*(size/10); cir++) {
Shape circle = new Circle(random.nextInt(), random.nextInt(), random.nextInt()); // your constructor of circle like Circle(int x, int y, int radius)
list.add(circle);
}
for (int cil=0; cil<3*(size/10); cil++) {
Shape cilinder = new Cilinder(random.nextInt(), random.nextInt(), random.nextInt(), random.nextInt()); // your constructor of cilinder like Cilinder (int x, int y, int radius, int height)
list.add(cilinder);
}
}
Shape[] result = list.toArray(new Shape[list.size()]);
return result;
}
是的,我有想法,但它需要你的代碼中可以看出 –
這有點曖昧,你想* *確切40%充滿'Rectangle's還是你希望這是預期的價值? – arshajii
陣列長度是否已知? – akaHuman