Zachary Hull
Zachary Hull

Reputation: 1

How to change an element in a list of lists Java

This is a peice of my code, i am making a grid of 5x5 with random colors set to each section. I need to set the specified y_loc and x_loc in the list to the color randomly picked except i have not been able to find out how. It should be the second last line that is not operating as id like. I understand that i could do this in much much longer code but it would be nice to do it in less.

//making the map
    ArrayList<ArrayList<String>> fullmap = new ArrayList<ArrayList<String>>();
    ArrayList<String> y_row_0 = new ArrayList<String>();
    ArrayList<String> y_row_1 = new ArrayList<String>();
    ArrayList<String> y_row_2 = new ArrayList<String>();
    ArrayList<String> y_row_3 = new ArrayList<String>();
    ArrayList<String> y_row_4 = new ArrayList<String>();
//adding each row
    fullmap.add(y_row_0);
    fullmap.add(y_row_1);
    fullmap.add(y_row_2);
    fullmap.add(y_row_3);
    fullmap.add(y_row_4);
    Random rn = new Random();
    //loop to randomly pick colors then set them to their destined locations
    for (int y_loc = 0; y_loc < 6; y_loc++){
        for (int x_loc = 0; x_loc < 6; x_loc++){
            colorPicked = false;
            while (!colorPicked){
                int ranNum = rn.nextInt();
                if (ranNum ==0){
                    if (redTot < 5) {
                        redTot += 1;
                        fullmap.set(y_loc).set(x_loc, "Red"));
                        colorPicked = true;

Upvotes: 0

Views: 6782

Answers (4)

Frelling
Frelling

Reputation: 3507

Others have already discussed the indexing issue. Apart from that, I believe that your conditionals may not be executing as you expect. nextInt() will return a reasonable uniform random number in the range of -2147483648 to 2147483647. You have a 1/2^64 chance of getting a 0. Reduce the random number range to something more reasonable. For example, nextInt(10) will return a random number between 0 and 9.

Furthermore, if the probability is too low, you will not get 5 reds all the time. To guarantee 5 picks and for the sake of computational efficiency, it is better to randomly pick array indices and evaluate whether a color is set or not, such as the following pseudo-code

    int redTot = 0;
    while ( redTot < 5 ) {
        int r = rn.nextInt( 5 );
        int c = rn.nextInt( 5 );
        String color = fullmap.get( r ).get( c );
        if ( color == null ) {
            fullmap.get( r ).set( c, "Red" );
            redTot++;
        }
    }

Upvotes: 0

Darshan Mehta
Darshan Mehta

Reputation: 30819

You need to make a couple of changes:

  • While declaring the sub lists, you need to make sure they have 5 empty/null elements. Otherwise set will throw IndexOutOfBoundsException. E.g. you need to declare the lists like this:

    ArrayList<String> y_row_0 = Arrays.asList(new String[5]);//Assuming it will have 5 elements

  • While setting the element, you first need to get the corresponding sub list, e.g. the following needs to be changed from:

    fullmap.set(y_loc).set(x_loc, "Red"));

    to

    fullmap.get(y_loc).set(x_loc, "Red"));

Upvotes: 0

CodedStingray
CodedStingray

Reputation: 391

Since you have lists in list here, to set something at a specific location, you'll have to get the inner list and then perform the set on it. The following should work:

fullmap.get(y_loc).set(x_loc, "Red"));

Also, since you seem to always have a 5x5 matrix, I'd recommend using a double array instead. That'd make that line:

fullmap[x_loc][y_loc] = "Red";

Upvotes: 1

Alex
Alex

Reputation: 827

You should have something like this:

fullmap.get(y_loc).set(x_loc, "Red"));

Notice the "get". You are "getting" the list at the y location, which returns an array list, then calling "set" against that array list to set the actual value in the index of the "x_loc" value.

Upvotes: 0

Related Questions