如何在不创建额外实例变量的情况下创建对象数组?
How can I create an array of objects without creating extra instance variables?
我正在尝试解决以下问题:
Implement a class called TriangleArray which has the following:
No other instance variables or constants allowed.
- An instance variable called list which is an array of Triangle objects.
- No other instance variables or constants allowed.
- Only one constructor which has two integer parameters. The first parameter called number is
the length of list. The second parameter called maxSize is the maximum size allowed for the
- The constructor initializes list to an array of Triangle objects.
- The sides of each Triangle object are randomly generated integers in the range from 1 to
maxSize.
- Getter and setter methods for list.
- A largest method with no parameters. The method returns the largest Triangle in list.
- A toString method which returns a description of each Triangle in list.
如何在不使用其他实例变量的情况下填充充满随机对象的列表?
这是我目前的代码:
import java.util.Random;
public class TriangleArray
{
private Triangle[ ] list;
// list is an array of "number" Triangle objects.
// The maximum size of each Triangle edge is "maxSize".
public TriangleArray (int number, int maxSize);
{
list = new Triangle[number];
您可以使用函数中的局部变量来做到这一点。
public TriangleArray (int number, int maxSize) {
list = new Triangle[number];
Random random = new Random();
for(int i = 0; i < number; list[i] = makeRandomTriangle(random, maxSize);
}
private function makeRandomTriangle(Random random, int maxSize) {
int a = random.nextInt(maxSize) + 1,
b = random.nextInt(maxSize) + 1;
// |a - b| <= c <= (a + b) because of the triangle inequality
int min = Math.max(a - b, b - a),
max = Math.min(maxSize, a + b);
int c = rand.nextInt((max - min) + 1) + min;
new Triangle(a, b, x);
}
实例变量或字段与局部变量不同。
参见:What is the difference between a local variable, an instance field, an input parameter, and a class field?
您可以很好地使用局部变量,例如:
public TriangleArray (int number, int maxSize) {
list = new Triangle[number];
for(int i = 0; i < number; i++) {
list[i] = makeTriangle(maxSize);
}
}
private makeTriangle(maxSize) {
Random r = new Random();
do {
int first = r.nextInt(maxSize - 1) + 1;
int second = r.nextInt(maxSize - 1) + 1;
int third = r.nextInt(maxSize - 1) + 1;
int max = Math.max(first, Math.max(second, third));
} while ((2 * max) - first - second - third) > 0);
return new Triangle(first, second, third);
}
在此代码中,您永远不会创建新的实例变量。
您不需要使用实例变量来填充随机数。您可以使用 java.util.Random
或 Math.random
:
list = new Triangle[number];
for (int i = 0; i < list.length; i++) {
int a = (int)(Math.random() * (maxSize + 1));
int b = (int)(Math.random() * (maxSize + 1));
int c = (int)(Math.random() * (maxSize + 1));
//handle the triangle inequality:
while (c > a + b || c < Math.abs(a - b)) {
c = (int)(Math.random() * (maxSize + 1));
}
list[i] = new Triangle(a, b, c);
}
假设 Triangle
对象的构造函数如下所示:
public Triangle(int a, int b, int c) { ...
我们初始化数组:
Random r = new Random(); // random number gen
for ( int i = 0; i < number; i++ ) {
int a = r.nextInt(maxSize-1) + 1; // randomly generate each side
int b = r.nextInt(maxSize-1) + 1;
int c = r.nextInt(maxSize-1) + 1; // this won't necessarily make a proper triangle
list[i] = new Triangle(a, b, c);
}
我们必须创建一个随机数生成器,然后我们必须遍历整个未创建的三角形数组并创建它们。迭代时,我们随机生成每条边 a、b、c,从 1 到 maxSize 的数字,然后在当前索引处创建 Triangle 对象。
list
是您的实例变量,同时变量 a、b、c 和 r 都是构造函数或您放入的任何函数的局部变量。
我正在尝试解决以下问题:
Implement a class called TriangleArray which has the following: No other instance variables or constants allowed.
- An instance variable called list which is an array of Triangle objects.
- No other instance variables or constants allowed.
- Only one constructor which has two integer parameters. The first parameter called number is the length of list. The second parameter called maxSize is the maximum size allowed for the
- The constructor initializes list to an array of Triangle objects.
- The sides of each Triangle object are randomly generated integers in the range from 1 to maxSize.
- Getter and setter methods for list.
- A largest method with no parameters. The method returns the largest Triangle in list.
- A toString method which returns a description of each Triangle in list.
如何在不使用其他实例变量的情况下填充充满随机对象的列表?
这是我目前的代码:
import java.util.Random;
public class TriangleArray
{
private Triangle[ ] list;
// list is an array of "number" Triangle objects.
// The maximum size of each Triangle edge is "maxSize".
public TriangleArray (int number, int maxSize);
{
list = new Triangle[number];
您可以使用函数中的局部变量来做到这一点。
public TriangleArray (int number, int maxSize) {
list = new Triangle[number];
Random random = new Random();
for(int i = 0; i < number; list[i] = makeRandomTriangle(random, maxSize);
}
private function makeRandomTriangle(Random random, int maxSize) {
int a = random.nextInt(maxSize) + 1,
b = random.nextInt(maxSize) + 1;
// |a - b| <= c <= (a + b) because of the triangle inequality
int min = Math.max(a - b, b - a),
max = Math.min(maxSize, a + b);
int c = rand.nextInt((max - min) + 1) + min;
new Triangle(a, b, x);
}
实例变量或字段与局部变量不同。
参见:What is the difference between a local variable, an instance field, an input parameter, and a class field?
您可以很好地使用局部变量,例如:
public TriangleArray (int number, int maxSize) {
list = new Triangle[number];
for(int i = 0; i < number; i++) {
list[i] = makeTriangle(maxSize);
}
}
private makeTriangle(maxSize) {
Random r = new Random();
do {
int first = r.nextInt(maxSize - 1) + 1;
int second = r.nextInt(maxSize - 1) + 1;
int third = r.nextInt(maxSize - 1) + 1;
int max = Math.max(first, Math.max(second, third));
} while ((2 * max) - first - second - third) > 0);
return new Triangle(first, second, third);
}
在此代码中,您永远不会创建新的实例变量。
您不需要使用实例变量来填充随机数。您可以使用 java.util.Random
或 Math.random
:
list = new Triangle[number];
for (int i = 0; i < list.length; i++) {
int a = (int)(Math.random() * (maxSize + 1));
int b = (int)(Math.random() * (maxSize + 1));
int c = (int)(Math.random() * (maxSize + 1));
//handle the triangle inequality:
while (c > a + b || c < Math.abs(a - b)) {
c = (int)(Math.random() * (maxSize + 1));
}
list[i] = new Triangle(a, b, c);
}
假设 Triangle
对象的构造函数如下所示:
public Triangle(int a, int b, int c) { ...
我们初始化数组:
Random r = new Random(); // random number gen
for ( int i = 0; i < number; i++ ) {
int a = r.nextInt(maxSize-1) + 1; // randomly generate each side
int b = r.nextInt(maxSize-1) + 1;
int c = r.nextInt(maxSize-1) + 1; // this won't necessarily make a proper triangle
list[i] = new Triangle(a, b, c);
}
我们必须创建一个随机数生成器,然后我们必须遍历整个未创建的三角形数组并创建它们。迭代时,我们随机生成每条边 a、b、c,从 1 到 maxSize 的数字,然后在当前索引处创建 Triangle 对象。
list
是您的实例变量,同时变量 a、b、c 和 r 都是构造函数或您放入的任何函数的局部变量。