[英]Java - Printing Array List of String Arrays
I am pretty new to Java and had a question. 我是Java的新手并且有一个问题。 This is homework, so I would not like any outright answers.
这是家庭作业,所以我不想要任何直接的答案。 Thanks!
谢谢!
I'm working on a genetic algorithm for playing poker. 我正在研究一种用于玩扑克的遗传算法。 I wanted to create an arrayList of string arrays.
我想创建一个字符串数组的arrayList。 The string arrays would hold moves for each possible hand during a round.
字符串数组将在一轮中保持每个可能的手的移动。 What I want to do right now to make sure it is working is run my method and print out the results.
我现在要做的是确保它正常工作是运行我的方法并打印出结果。 Here are my practice classes (this is only a small part of the assignment):::
这是我的练习课(这只是作业的一小部分):::
import java.util.ArrayList;
import java.util.Arrays;
public class Play
{
GeneticAlg start;
ArrayList<Object> pop;
public static void main(String[] args)
{
GeneticAlg start = new GeneticAlg();
ArrayList<String[]> pop = start.initializePopulation();
for(String[] arr: pop)
{
System.out.println(Arrays.toString(arr));
}
}
}
import java.util.ArrayList;
import java.util.Random;
public class GeneticAlg
{
ArrayList<String[]> population;
int[] populationScores;
String[] chromosome;
int generation;
int index;
public GeneticAlg()
{
}
public ArrayList<String[]> initializePopulation()
{
ArrayList<String[]> population = new ArrayList<String[]>();
for (int i = 0; i < 20; i++)
{
Random generator = new Random();
String[] choices = {"bet","raise","call","check"};
chromosome = new String[33];
for (int j = 0; j < 33; j++)
{
if (j < 6) //first and second round possible hands)
{
index = generator.nextInt((choices.length)-1);
chromosome[j] += choices[index];
}
else //third, fourth, and fifth round possible hands)
{
index = generator.nextInt(choices.length);
chromosome[j] += choices[index];
}
}
population.add(chromosome);
}
return population;
}
}
Right now, it's printing out the array, but each entry looks like this: 现在,它打印出数组,但每个条目看起来像这样:
[nullcall, nullraise, nullbet, nullraise, nullcall, nullraise,....
I want it to just return the move without null on the front. 我希望它只返回前面没有null的移动。 Any advice is appreciated.
任何建议表示赞赏。 Thanks!
谢谢!
ETA: I fixed the two lines with the concatenation error, but it is still printing "null" in front of each command. ETA:我用连接错误修复了两行,但它仍然在每个命令前打印“null”。 Any advice?
有什么建议?
Code after repairing the error: 修复错误后的代码:
import java.util.ArrayList;
import java.util.Arrays;
public class Play
{
GeneticAlg start;
ArrayList<Object> pop;
public static void main(String[] args)
{
GeneticAlg start = new GeneticAlg();
ArrayList<String[]> pop = start.initializePopulation();
for(String[] arr: pop)
{
System.out.println(Arrays.toString(arr));
}
}
}
import java.util.ArrayList;
import java.util.Random;
public class GeneticAlg
{
ArrayList<Object> population;
String[] choices;
int[] populationScores;
String[] chromosome;
int generation;
int index;
public GeneticAlg()
{
}
public ArrayList<Object> initializePopulation()
{
population = new ArrayList<Object>();
for (int i = 0; i < 20; i++)
{
Random generator = new Random();
for (int j = 0; j < 24; j++)
{
if (j < 6) //first and second round possible hands)
{
choices[0]= "bet";
choices[1]= "raise";
choices[3]= "call";
index = generator.nextInt(choices.length);
chromosome[j] = choices[index];
}
else //third, fourth, and fifth round possible hands)
{
choices[4] = "check";
index = generator.nextInt(choices.length);
chromosome[j] = choices[index];
}
}
population.add(chromosome);
}
return population;
}
}
Arrays of objects (like Stirng
s) are filled at start with null
values, so doing 对象数组(如
Stirng
s)在开始时用null
值填充,这样做
chromosome[j] += choices[index];
is the same as 是相同的
chromosome[j] = chromosome[j] + choices[index];
which is the same as 这是一样的
chromosome[j] = null + choices[index];
So you are concatenating null
with choices[index];
所以你将
null
与choices[index];
连接起来choices[index];
which gives you nullbet
for instance. 例如,它为你提供了
nullbet
。
To solve it just use =
instead of +=
. 要解决它,只需使用
=
而不是+=
。
Since you don't want an outright answer, look at the code that populates your String[]'s. 由于您不想要直接回答,请查看填充String []的代码。 Your printing's doing the right thing, but the Strings in the array actually are "nullbet," "nullraise," etc.
您的打印的做正确的事,但字符串数组中实际上是“nullbet”,“nullraise”等
The error comes from this line here: 错误来自这一行:
chromosome[j] += choices[index];
The += operator, when used with a Strings, will concatenate the right-hand string to the end of the left-hand string. + =运算符与字符串一起使用时,会将右侧字符串连接到左侧字符串的末尾。 In this case, it tacks on choices[index] to the existing contents of chromosome[j], which will null by default if chromosome is declared as an array of Objects.
在这种情况下,它决定了染色体[j]的现有内容的选择[索引],如果将染色体声明为对象数组,则默认为null。
You probably meant 你可能意味着
chromosome[j] = choices[index];
And accidentally inserted the + because you use population.add() with your list below. 并且意外地插入了+因为您使用了population.add()和下面的列表。
Your "fixed" code looks like it doesn't compile - you're probably still running the previous version. 你的“修复”代码看起来好像没有编译 - 你可能仍在运行以前的版本。 You don't initialize choices ("choices = new String[4]") and you're confusing its indices (0, 1, 3 and 4).
你没有初始化选择(“choices = new String [4]”)并且你混淆了它的索引(0,1,3和4)。 Also, if you're trying to add a fourth element to the array later, don't, you can't do that with arrays.
此外,如果您稍后尝试向阵列添加第四个元素,请不要,您不能使用数组。 And you're assigning an ArrayList to an ArrayList without a cast.
并且您在没有强制转换的情况下将ArrayList分配给ArrayList。 You only needed to swap += with = in your original code, it seemed fine otherwise.
您只需要在原始代码中交换+ = with =,否则它似乎没问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.