Marco98Lorenzo
Marco98Lorenzo

Reputation: 3

Initialize a random matrix with a fixed number of variables in Java

I'm writing the constructor for a fixed size matrix of objects with a String type attribute. There is a fixed pool of types and a fixed number of times each type can be used.

An example:

public class MyObject {

    private String type;
    ...
}

...

private MyObject[][] myMatrix = new MyObject[3][3];

...

int number_of_red = 2;        //Number of times I can assign the String "Red" in MyMatrix;
int number_of_blue = 5;        //Number of times I can assign the String "Blue" in MyMatrix;
int number_of_green = 2;        //Number of times I can assign the String "Green" in MyMatrix;

I want MyMatrix to be a 3x3 matrix of MyObject where each object has a String type attribute and the number of times each String can be used is fixed by a given variable. The output Matrix should be randomized. I'm fairly new to Java (and OOP in general), so I was thinking about nested for loops as I would do in C. Is there a more object oriented way to do this?

Upvotes: 0

Views: 93

Answers (2)

user4910279
user4910279

Reputation:

Try this.

public class MyObject {
    private String type;
    MyObject(String type) { this.type = type; }
    @Override public String toString() { return type; }
}

And

MyObject[][] myMatrix = new MyObject[3][3];
int number_of_red = 2;        //Number of times I can assign the String "Red" in MyMatrix;
int number_of_blue = 5;        //Number of times I can assign the String "Blue" in MyMatrix;
int number_of_green = 2;        //Number of times I can assign the String "Green" in MyMatrix;
String[] colors = new String[9];
int start = 0;
Arrays.fill(colors, start, start += number_of_red, "Red");
Arrays.fill(colors, start, start += number_of_blue, "Blue");
Arrays.fill(colors, start, start += number_of_green, "Green");
Collections.shuffle(Arrays.asList(colors));
for (int i = 0, c = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j, ++c)
        myMatrix[i][j] = new MyObject(colors[c]);
for (MyObject[] row : myMatrix)
    System.out.println(Arrays.toString(row));

output

[Green, Blue, Red]
[Red, Blue, Blue]
[Blue, Green, Blue]

Upvotes: 0

IntoVoid
IntoVoid

Reputation: 964

If you want to loop through all the entries of your matrix using only one foor loop, you could always do:

for (int i = 0; i < 9; i++) {
    int y = i / 3, x = i % 3;
    // Access myMatrix[y][x] and change stuff
}

If you want to also distribute MyObject, then do:

// This would be in your constructor after initializing the myMatrix object
int i = 0, c = 0, y, x;
for (c += number_of_red; i < c; i++) {
    y = i / 3; x = i % 3;
    myMatrix[y][x] = new MyObject("type red");
}
for (c += number_of_blue; i < c; i++) {
    y = i / 3; x = i % 3;
    myMatrix[y][x] = new MyObject("type blue");
}
for (c += number_of_green; i < c; i++) {
    y = i / 3; x = i % 3;
    myMatrix[y][x] = new MyObject("type green");
}


// And if you want to output the entire matrix do (if a 'getType()' method exists for 'MyObject'):
for (i = 0; i < 9; i++) {
    y = i / 3; x = i % 3;
    System.out.println("[ " + x + " | " + y + " ] = " + myMatrix[y][x].getType());
}

This should also populate the matrix

Upvotes: 1

Related Questions