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.
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:
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.