Doubling The Array Size And Randomizing Array Return
Hi, I've written the following code:
Currently what happens is that two arrays are being taken in, and all possible sequential combinations of the indices of Array A are being stored as a list of seperate arrays, each of which are the same size as array B. Currently to do this sizeA has to be smaller than sizeB.
Code :
import java.util.*;
public class Main {
public static void main(final String[] args) throws FileNotFoundException {
ArrayList<String> storeB= new ArrayList();
ArrayList<String> storeA = new ArrayList();
Scanner scannerB = new Scanner(new File("fileB"));
Scanner scannerA = new Scanner(new File("fileA"));
while(scannerB.hasNext()) {
String b = scannerB.next();{
storeB.add(b);
}
}
while(scannerA.hasNext()) {
String A = scannerA.next();{
storeA.add(A);
}
}
final int sizeA = storeA.size();
final int sizeB = storeB.size();
final List<int[]> combinations = getOrderings(sizeA-1, sizeB);
for(final int[] combo : combinations) {
for(final int value : combo) {
System.out.print(value + " ");
}
System.out.println();
}
}
private static List<int[]> getOrderings(final int maxIndex, final int size) {
final List<int[]> result = new ArrayList<int[]>();
if(maxIndex == 0) {
final int[] array = new int[size];
Arrays.fill(array, maxIndex);
result.add(array);
return result;
}
// creating an array for each occurence of maxIndex, and generating each head
//recursively
for(int i = 1; i < size - maxIndex + 1; ++i) {
//Generating every possible head for the array
final List<int[]> heads = getOrderings(maxIndex - 1, size - i);
//Combining every head with the tail
for(final int[] head : heads) {
final int[] array = new int[size];
System.arraycopy(head, 0, array, 0, head.length);
//Filling the tail of the array with i maxIndex values
for(int j = 1; j <= i; ++j)
array[size - j] = maxIndex;
result.add(array);
}
}
return result;
}
}
I'm trying to modify this so that, regardless of sizeA and sizeB (currently sizeB has to be bigger then sizeA), I can create arrays which are double sizeB and duplicate each index value. So if we had: [0,1,1,2] this would become: [0,0,1,1,1,1,2,2] i.e duplicating each value and placing it next to it.
I currently have code that looks somewhat like this to do this:
Code :
public int[] getArray(int originSize) {
int[] result = new int[originSize * 2];
for (int i = 0, j = 0; i < originSize; ++i, j+=2)
{
result[j] = i;
result[j+1] = i;
}
return result;
}
But I'm having trouble incorporating it into the original code.
Also how would I modify what I have so that rather than producing all possible combinations, on each call, a single array at random is produced rather than a list of arrays?
Thanks a lot.
Re: Doubling The Array Size And Randomizing Array Return
Quote:
I'm having trouble incorporating it into the original code.
Please explain the problem.
Quote:
so that rather than producing all possible combinations, on each call, a single array at random is produced rather than a list of arrays?
No idea what you are asking here. Can you explain perhaps with an example.
Re: Doubling The Array Size And Randomizing Array Return
What I mean is I don't see how to use that method I have created within my code. At the moment if you run the code, it produces a list of all possible combinations of arrays, which are of sizeB and contain a sequential ordering of the indices of array A. So if sizeB= 5 and A contains 3 elements then possible combinations are:
[0,1,1,2,2] or [0,1,1,1,2]. So rather than calculating every possible combination I want it to return a combination at random
Re: Doubling The Array Size And Randomizing Array Return
Quote:
I don't see how to use that method I have created within my code.
I'm confused. What is the purpose of the getArray method? Why did you write it if it is not related to the program task?
Quote:
I'm trying to modify this so that ... I can create arrays which are double sizeB and duplicate each index value.
Re: Doubling The Array Size And Randomizing Array Return
the getArray() represents what I'm trying to achieve even though it doesn't work in the main code, which is to duplicate each index value and place it next to it, which consequently means the size of the array doubles
Re: Doubling The Array Size And Randomizing Array Return
Sorry, I'm confused why you wrote getArray() with this program. What does it have to do with this program?
Re: Doubling The Array Size And Randomizing Array Return
getArray() is a representation of what I'm trying to achieve, it does not have anything directly to do with the program, what it does is it fills in the array two values which are the same next to each other and increments. This is what I what essentially I am trying to achieve, I want to duplicate each index value and place it alongside, as given in the example above. I'm not sure how to make this any more clearer.
Re: Doubling The Array Size And Randomizing Array Return
Quote:
This is what I what essentially I am trying to achieve
If you have a method that does what you want done, why don't you call that method?
Do you need to have two (or more) methods that do the same thing but using different techniques?
You currently have one and are now trying to write another one.
Re: Doubling The Array Size And Randomizing Array Return
This method increments values in the for loop and fills it in, what I want is for it to use the indices stored in the array, and I'm not sure exactly where to call the method
Re: Doubling The Array Size And Randomizing Array Return
Still confused. You wrote a method to solve a problem.
Now you've forgotten where the problem is so you can call the method at that location.
In that case why not throw out the method if it is of no use to you?
Re: Doubling The Array Size And Randomizing Array Return
Ok, forgetting the method. How would I solve the initial problem of duplicating each value in the arrays and placing it next to it, and instead of recursively calculating all the combinations how do I randomly calculate one?
Re: Doubling The Array Size And Randomizing Array Return
Quote:
How would I solve the initial problem of duplicating each value in the arrays and placing it next to it
I'd do it in a loop similiar to how your getArray() method does it.
Quote:
instead of recursively calculating all the combinations how do I randomly calculate one?
Can you give an example of the input and output you want?
Re: Doubling The Array Size And Randomizing Array Return
The input will be two arrays so something like A: [gs,a,jj] B:[fg,p,f,on] and possible combinations are [0,1,1,2], [0,0,1,2] (since there are 3 index values for A and the size of B is 4), all these combinations are currently what the program returns but I want to eliminate the recursion so that it returns one combination at random, so just [0,1,1,2].
Re: Doubling The Array Size And Randomizing Array Return
Quote:
The input will be two arrays so something like A: [gs,a,jj] B:[fg,p,f,on] and possible combinations are [0,1,1,2], [0,0,1,2]
Sorry, I have no idea how you go from the arrays A and B to the combination arrays.
One has letters, the other numbers. There are no elements in arrays A and B that are in common.
Given arrays A and B, how do you generate the two combination arrays you show?
This looks like an algorithm problem, not a java language programming problem.
If you have a problem with how to do a step in java, ask the question.
If its an algorithm problem, ???
Re: Doubling The Array Size And Randomizing Array Return
Maybe running the program will help. What happens is that the program fills in the indices A[0] = gs, A[1] = a A[2] = jj, into a new array that is the SIZE of B, and generates every possible sequential combination (so a list of arrays). At the moment this does it by recursion which is inefficient, I want to eliminate recursion, so that a single random combination is calculated and returned, so a single array.
Re: Doubling The Array Size And Randomizing Array Return
Quote:
Maybe running the program will help
I doubt it will explain what the algorithm is that you are trying to implement.
Besides the code is not setup for testing. I had to change these lines for easy testing:
Code :
Scanner scannerB = new Scanner("A B C D\n"); //new File("fileB"));
Scanner scannerA = new Scanner("D E F\n"); //new File("fileA"));
Also I don't see where the contents of storeA and storeB are used.
Re: Doubling The Array Size And Randomizing Array Return
They are being used when we find the size of the arrays
Re: Doubling The Array Size And Randomizing Array Return
So the contents of the array are used. So why do you read in the arrays? Couldn't you just read in two numbers that represent the sizes of the arrays?
Re: Doubling The Array Size And Randomizing Array Return
You can do it that way too