如何创建 class 的实例并将它们存储在数组或数组列表中? (加工)
How to create an instance of a class and store them in an array or arraylist? (processing)
我有这个程序,我需要来自外部文件的 50 个随机单词出现,每个单词随机移动到一个随机位置...我已经能够让每个单词彼此分开移动到随机地方,但问题是只有外部文件中的第一个单词出现了 50 次,这是唯一出现的单词……而不是 50 个随机单词!只有 50 个完全相同的词...所以我尝试将 int index = int(random(allWords.length));
放在 draw
下和 for
内,但这可能会导致它会发生 50 次,每秒 60 次,并且那不是我想要发生的......有人建议相反,我可能只想在 setup()
函数中生成一次随机词,我可以通过创建 class
I 的实例来做到这一点创建并将它们存储在数组或 ArrayList 中。问题是我仍然不太熟悉,所以有人对我如何做有提示吗?或者 link 我可以在那里获得如何做的指南?
如果有人想看看我的问题是什么,这是我的代码...
String [] allWords;
int x = 120;
int y = 130;
int index = 0 ;
word [] words;
void setup () {
size (500, 500);
background (255); //background : white
String [] lines = loadStrings ("alice_just_text.txt");
String text = join(lines, " "); //make into one long string
allWords = splitTokens (text, ",.?!:-;:()03 "); //splits it by word
words = new word [allWords.length];
for (int i = 0; i < 50; i++) {
words[i] = new word (x, y);
}
}
void draw() {
background (255);
for (int i = 0; i < 50; i++) { //produces 50 words
words[i].display();
words[i].move();
words[i].avgOverlap();
}
}
class word {
float x;
float y;
word(float x, float y) {
this.x = x;
this.y = y;
}
void move() {
x = x + random(-3, 3); //variables sets random positions
y = y + random(-3, 3); //variables sets random positions
}
void display() {
fill (0); //font color: black
textAlign (CENTER, CENTER);
text (allWords[index], x, y, width/2, height/2 );
}
void ran () {
textSize (random(10, 80)); //random font size
}
}
您已经创建了 class 的实例并将其存储在这个 for
循环中的数组中:
for (int i = 0; i < 50; i++) {
words[i] = new word (x, y);
}
问题是您只有一个 index
变量,因此 Word
class 的每个实例都使用相同的 index
值!
您可能希望将单独的索引传递到您正在创建的 Word
的每个实例中:
for (int i = 0; i < 50; i++) {
words[i] = new word (x, y, i);
}
或者您可以传递希望每个特定实例使用的 String
值:
for (int i = 0; i < 50; i++) {
words[i] = new word (x, y, allWords[i]);
}
然后您需要修改 Word
构造函数以获取额外参数,以及 display()
函数以使用该参数。
请注意classes应该以大写字母开头,所以应该是Word
而不是word
。
此外,请尝试将您的问题隔离到 MCVE,我们可以自己复制并粘贴到 运行。这将使您的生活更轻松,也将使我们更容易为您提供帮助。从空白草图开始,只添加足够的代码,以便我们可以看到您的问题。例如,使用 String
值的硬编码数组而不是文件。
我有这个程序,我需要来自外部文件的 50 个随机单词出现,每个单词随机移动到一个随机位置...我已经能够让每个单词彼此分开移动到随机地方,但问题是只有外部文件中的第一个单词出现了 50 次,这是唯一出现的单词……而不是 50 个随机单词!只有 50 个完全相同的词...所以我尝试将 int index = int(random(allWords.length));
放在 draw
下和 for
内,但这可能会导致它会发生 50 次,每秒 60 次,并且那不是我想要发生的......有人建议相反,我可能只想在 setup()
函数中生成一次随机词,我可以通过创建 class
I 的实例来做到这一点创建并将它们存储在数组或 ArrayList 中。问题是我仍然不太熟悉,所以有人对我如何做有提示吗?或者 link 我可以在那里获得如何做的指南?
如果有人想看看我的问题是什么,这是我的代码...
String [] allWords;
int x = 120;
int y = 130;
int index = 0 ;
word [] words;
void setup () {
size (500, 500);
background (255); //background : white
String [] lines = loadStrings ("alice_just_text.txt");
String text = join(lines, " "); //make into one long string
allWords = splitTokens (text, ",.?!:-;:()03 "); //splits it by word
words = new word [allWords.length];
for (int i = 0; i < 50; i++) {
words[i] = new word (x, y);
}
}
void draw() {
background (255);
for (int i = 0; i < 50; i++) { //produces 50 words
words[i].display();
words[i].move();
words[i].avgOverlap();
}
}
class word {
float x;
float y;
word(float x, float y) {
this.x = x;
this.y = y;
}
void move() {
x = x + random(-3, 3); //variables sets random positions
y = y + random(-3, 3); //variables sets random positions
}
void display() {
fill (0); //font color: black
textAlign (CENTER, CENTER);
text (allWords[index], x, y, width/2, height/2 );
}
void ran () {
textSize (random(10, 80)); //random font size
}
}
您已经创建了 class 的实例并将其存储在这个 for
循环中的数组中:
for (int i = 0; i < 50; i++) {
words[i] = new word (x, y);
}
问题是您只有一个 index
变量,因此 Word
class 的每个实例都使用相同的 index
值!
您可能希望将单独的索引传递到您正在创建的 Word
的每个实例中:
for (int i = 0; i < 50; i++) {
words[i] = new word (x, y, i);
}
或者您可以传递希望每个特定实例使用的 String
值:
for (int i = 0; i < 50; i++) {
words[i] = new word (x, y, allWords[i]);
}
然后您需要修改 Word
构造函数以获取额外参数,以及 display()
函数以使用该参数。
请注意classes应该以大写字母开头,所以应该是Word
而不是word
。
此外,请尝试将您的问题隔离到 MCVE,我们可以自己复制并粘贴到 运行。这将使您的生活更轻松,也将使我们更容易为您提供帮助。从空白草图开始,只添加足够的代码,以便我们可以看到您的问题。例如,使用 String
值的硬编码数组而不是文件。