I've been working on learning about irregular arrays and thought making a generator such as this one would be a good exercise in applying what I have learned.
The following class generates an array containing \$n\$ arrays which themselves contain arrays of integers of decreasing length, of which the maximum length is \$n\$. They are also shuffled to be in random (or, pseudo-random, more accurately) order. The idea behind this is to be able to simply create an array and populate it with data that can be used for developing and testing.
A few general concerns:
This is the first
toString()
I have ever written; how can I improve the code, output, or both, to be more useful or "natural"?I'm still quite new at organizing workflow within a class. My constructor calls a method which chains a few other methods. Is this sensible or terrible design?
All other feedback welcomed!
Random2dIntArray.java
import java.util.Random;
/**
* Create an array of given length which is comprised of inner arrays of random integers.
*/
public class Random2dIntArray {
int numberOfInnerArrays;
private int[][] random2dIntArray;
private Random random = new Random();
/**
* Constructor.
* @param numberOfInnerArrays The number of inner arrays which will be seeded with random integers,
* as well as the length of the longest inner array of random integers.
*/
public Random2dIntArray(int numberOfInnerArrays) {
this.numberOfInnerArrays = numberOfInnerArrays;
this.random2dIntArray = new int[numberOfInnerArrays][];
generate();
}
/**
* Creates the Random2dIntArray.
* @return random2dIntArray A shuffled array of arrays of random integers.
*/
private int[][] generate() {
createInnerArrays(random2dIntArray);
shuffle(random2dIntArray);
addRandomInts(random2dIntArray);
return random2dIntArray;
}
/**
* Creates each inner array with a length based on the input parameter numberOfInnerArrays,
* which will also be the length of the longest inner array within the outer array.
* @param randomIrregularArray The outer array based on which the inner arrays will be created.
*/
private void createInnerArrays(int[][] randomIrregularArray) {
for (int i = randomIrregularArray.length, j = 0; i > 0; i--, j++) {
randomIrregularArray[j] = new int[i];
}
}
/**
* Shuffles the indexes of the array to change their order. Fisher–Yates shuffle array function.
* inspired by: http://stackoverflow.com/a/18456998/3626537
* @param sorted2dIntArray The array to be shuffled.
*/
private void shuffle(int[][] sorted2dIntArray) {
// Fisher–Yates shuffle array
// Inspired by: http://stackoverflow.com/a/18456998/3626537
int randomIndex;
int[] temp;
for (int index = sorted2dIntArray.length - 1; index > 0; index--) {
randomIndex = random.nextInt(index + 1);
// Swap index values:
temp = sorted2dIntArray[randomIndex];
sorted2dIntArray[randomIndex] = sorted2dIntArray[index];
sorted2dIntArray[index] = temp;
}
}
/**
* Seeds the inner arrays with random integers.
* @param randomIrregularArray Iterates over the inner arrays and seeds random integers as values for each index.
*/
private void addRandomInts(int[][] randomIrregularArray) {
for (int i = 0; i < randomIrregularArray.length; i++) {
for (int j = 0; j < randomIrregularArray[i].length; j++) {
randomIrregularArray[i][j] = random.nextInt();
}
}
}
/**
* Renders Random2dIntArray to a String type.
* @return a String representation of the Random2dIntArray
*/
public String toString() {
String arrayAsString = "";
for (int outerIdx = 0; outerIdx < random2dIntArray.length; outerIdx++) {
arrayAsString = arrayAsString.concat(String.format("outer[%d]: ", outerIdx));
for (int innerIdx = 0; innerIdx < random2dIntArray[outerIdx].length; innerIdx++) {
arrayAsString = arrayAsString.concat(String.format("[%d]: %d ",
innerIdx, random2dIntArray[outerIdx][innerIdx]));
}
arrayAsString = arrayAsString.concat(System.lineSeparator());
}
return arrayAsString;
}
}
A test call, including printing to console:
public class TestRandom2dIntArray {
public static void main(String[] args) {
Random2dIntArray jaggedArray = new Random2dIntArray(5);
System.out.print(jaggedArray.toString());
}
}
Sample toString()
output:
outer[0]: [0]: 1497180700 [1]: -1267723849 [2]: -1402772085 [3]: -1853139189 [4]: -1840423978 outer[1]: [0]: 1616095695 [1]: 861133708 [2]: -2108469735 outer[2]: [0]: -1782952896 [1]: -1979814925 outer[3]: [0]: 1525848498 [1]: 733271498 [2]: -1194054116 [3]: 553156500 outer[4]: [0]: 972788909
Another sample toString()
to show how the content varies:
outer[0]: [0]: -1537688595 [1]: -1356001667 [2]: 876061606 [3]: -196204872 outer[1]: [0]: -814279607 outer[2]: [0]: -933087370 [1]: 1582817382 outer[3]: [0]: -497198575 [1]: 1270590328 [2]: 553862753 outer[4]: [0]: -1766682724 [1]: 520775777 [2]: -279377700 [3]: 162768788 [4]: 423268324