简体   繁体   中英

Java - Printing Array List of String Arrays

I am pretty new to Java and had a question. 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. 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. 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. 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

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]; which gives you nullbet for instance.

To solve it just use = instead of += .

Since you don't want an outright answer, look at the code that populates your String[]'s. Your printing's doing the right thing, but the Strings in the array actually are "nullbet," "nullraise," etc.

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.

You probably meant

chromosome[j] = choices[index];

And accidentally inserted the + because you use population.add() with your list below.

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). 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. You only needed to swap += with = in your original code, it seemed fine otherwise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM