java 如何使用 Rectangle 类创建矩形数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15777064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:49:35  来源:igfitidea点击:

How to create an array of Rectangles with a Rectangle class?

javaarrays

提问by dmetal23

I'm working on an assignment that is supposed to return an array of 10 rectangles with a random height, random width, and random color selected from a string.

我正在处理一项任务,该任务应该返回一个由 10 个矩形组成的数组,这些矩形具有从字符串中选择的随机高度、随机宽度和随机颜色。

The program works fine to return the objects for ONE rectangle, but how would I implement this to create an array of 10 rectangles and THEN return each one in a loop?

该程序可以很好地返回一个矩形的对象,但是我将如何实现它以创建一个包含 10 个矩形的数组,然后在循环中返回每个矩形?

Here's my class file with my objects:

这是我的类文件和我的对象:

import java.util.*;

public class Rectangle 
{
    private double width;
    private double height;
    public static String color = "White";
    private Date date;

Rectangle() {
    width = 1;
    height = 1;
    date = new Date(); 
    }

Rectangle (double w, double h) {
    width = w;
    height = h;
    date = new Date();
    }

public double getHeight() {
    return height;
    }

public void setHeight(double h) {
    height = h;
    }

public double getWidth() {
    return width;
    }

public void setWidth(double w) {
    width = w;
    }

public static String getColor() {
    return color;
    }

public static void setColor(String c) {
    color = c;
    }

public Date getDate() {
    return date;
    }

public void setDate (Date d) {
    date = d; 
    }

public double getArea() {
    return width * height;
    }

public double getPerimeter() {
    return 2  * (width + height);
    }

public String toString() {
    String S;
    S = "Rectangle with width of " + width;
    S = S + " and height of " + height;
    S = S + " was created on " + date.toString();
    return S;
    }

}

}

Here is my client program so far. I am setting a random height and a random width and selecting a random color from the colors String.

到目前为止,这是我的客户端程序。我正在设置一个随机高度和一个随机宽度,并从颜色字符串中选择一个随机颜色。

I would like to be able to do this for an array of 10 different rectangles:

我希望能够对 10 个不同矩形的数组执行此操作:

import java.util.*;

public class ClientRectangle 
{
    public static void main(String[] args)
    {
        String[] colors = {"White","Blue","Yellow","Red","Green"};

        Rectangle r = new Rectangle();
        int k;
        for(int i = 0; i < 10; i++)
        {
            r.setWidth((Math.random()*40)+10); 
            r.setHeight((Math.random()*40)+10);
            System.out.println(r.toString() + " has area of " + r.getArea() + " and perimeter of " + r.getPerimeter());
            k = (int)(Math.random()*4)+1;
            System.out.println(colors[k]);

        }



    }
}       

Thanks!

谢谢!

回答by Jeroen Vannevel

Create an array of rectangles and add a rectangle to each index.

创建一个矩形数组并向每个索引添加一个矩形。

Rectangle[] arr = new Rectangle[10];

for(int i = 0; i < 10; i++)
{
 Rectangle r = new Rectangle();
 r.setWidth((Math.random()*40)+10); 
 r.setHeight((Math.random()*40)+10);

 arr[i] = r;
}

回答by Pradeep Pati

Move the Rectangle r = new Rectangle();inside the for loop. Initialise a Array(List) outside the loop, and keep adding the Rectangles in the loop to the array.

移动Rectangle r = new Rectangle();for 循环内部。在循环外初始化一个 Array(List),并将循环中的 Rectangles 添加到数组中。

回答by Code-Apprentice

You already seem to know how to declare an array of objects because you did so with an array of Strings. The declartion for an array of Rectangles is very similar:

您似乎已经知道如何声明对象数组,因为您使用的是Strings数组。Rectangles数组的声明非常相似:

Rectangle[] rects;

Note that you mustalso initialize the array. Since arrays are themselves objects you use the newoperator, somewhat like when you initialize a reference variable to a single Rectangle:

请注意,您还必须初始化数组。由于数组本身就是对象,因此您使用new运算符,有点像将引用变量初始化为单个Rectangle

Rectangle[] rects = new Rectangle[SIZE];

The only difference, as you can see, is that you put the size of the array inside the []s. This only creates an array of referencesto Rectangleobjects. The references are all automatically set to null. This means that you need to create the Rectanglesthemselves. You can do this in for loop:

如您所见,唯一的区别是将数组的大小放在[]s 中。这只会创建对对象的引用数组Rectangle。引用都自动设置为null. 这意味着您需要创建Rectangles它们自己。您可以在 for 循环中执行此操作:

for (int i = 0; i <= SIZE; ++i) {
    rects[i] = new Rectangle();
}

You can also set the width and height of each Rectangleas you want. (I will leave the exact details about how to do this to the reader.)

您还可以根据需要设置每个的宽度和高度Rectangle。(我会将有关如何执行此操作的确切详细信息留给读者。)

Finally, you need to put this all in a method other thanmain()so that you can return the whole array (You don't return one Rectangleat a time) as the instructions state:

最后,您需要将所有这些都放在一个方法中,而不是main()这样您就可以返回整个数组(您不会一次返回一个Rectangle)作为指令状态:

return rects;