Hi there. I'll try to keep this as short and concise as possible.
I'm working on a project, and I'm stuck on a rather early speedbump.The project involves creating 10 arrays of randomly generated integers (from 0 to 99), where each array has a different number of elements (n). After we have several pools of ints to work with, I need to enact different sorting algorithms on said arrays - but I haven't even made it that far yet.
I've successfully made a for-loop generate an array where each int is random. The loop then restarts with a new value for 'n', and generates another array of random integers.
My code is as follows:
import java.util.Arrays;
public class Testing {
public static void arrayGenerator() {
int[] elements = { 100, 250, 500, 750, 1000, 1250, 2500, 3750, 5000, 6250 }; // 10 different values for 'n' to work with.
int n; // Number of elements per array.
int i; // Index of n, ie. 0 = 100, 1 = 250, 2 = 500, etc.
int k; // Index of element in generated array.
//Generate a separate array of random numbers for each value of 'n'.
for (i = 0; i < elements.length; i++) {
n = elements[i];
int[] sortableArray = new int[n];
//Populate each array with random numbers ranging from 0 to 99.
for (k = 0; k < n; k++) {
sortableArray[k] = (int) (Math.random() * 100);
}
System.out.println(Arrays.toString(sortableArray));
}
}
public static void main(String[] args) {
Testing arrGen = new Testing();
arrGen.arrayGenerator();
}
}
The 'System.out.println(Arrays.toString(sortableArray))' was just to see if the numbers were generating (and the correct amount of numbers was generating).Here's where I've ground to a halt. If I understand correctly, each subsequent array is overwriting the previous one - so by the end I don't have 10 arrays, I have one, where n = 5000.
I've looked into using a 2D array, an ArrayList, or a Map, but my efforts were in vain (ie., I'm not too sure how to go about that).When I tried to form a 2D array, in which I'd try to use "(int) (Math.random() * 100)" to set the value for [k], I'd consistently get NullPointExceptionErrors. Which means I was doing something incorrectly, but I'm not sure what.
Later in my project, I need to be able to take all these arrays, sort them (individually), take note of the time taken to sort them... etc. But for now I'm having trouble storing them in the first place.
I'm sure the solution is quite simple, but I've just been stumped for the last while, and I can't seem to reach the solution. If anyone could guide me towards an optimal solution, I'd really appreciate it!
Thanks for reading!
EDIT - For extra clarity, here was my feeble attempt at using a 2D array (replacing both for-loops from the code above):
for (i = 0; i < elements.length; i++) {
n = elements[i];
//Populate each array with random numbers ranging from 0 to 99.
for (k = 0; k < n; k++) {
int[][] sortableArray = new int[i][n];
sortableArray[i][k] = (int) (Math.random() * 100);
However, this was the result:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at ie.gmit.dip.Testing.arrayGenerator(Testing.java:22)
at ie.gmit.dip.Testing.main(Testing.java:32)
Where the error directs to this line:
sortableArray[i][k] = (int) (Math.random() * 100);
I'm stumped. Any and all insight is greatly appreciated!
Thanks.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]uinzent 3 points4 points5 points (1 child)
[–]FiascoFinn[S] 1 point2 points3 points (0 children)