随机化返回和加倍数组大小

时间:2011-06-27 11:19:26

标签: java

我目前有这段代码。

目前发生的事情是两个阵列被接纳,并且阵列A的索引的所有可能的顺序组合被存储为单独的阵列的列表,每个阵列与阵列B的大小相同。目前要做这个sizeA必须小于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;

}

}

我想知道,无论sizeA和sizeB如何,我如何修改它来创建double sizeB的数组并复制每个索引值。所以如果我们有:     [0,1,1,2] 这会变成:     [0,0,1,1,1,1,2,2] 即复制每个值并将其放在旁边。

另外,我如何消除这种递归,以便不是产生所有可能的组合,而是在每次调用时产生随机的单个数组而不是数组列表。

谢谢。

2 个答案:

答案 0 :(得分:1)

  

所以如果我们有:[0,1,1,2]这将成为:[0,0,1,1,1,1,2,2],即重复每个值并将其放在旁边。< / p>

public int[] getArray(int originSize) {
    // Create a array double the size of originSize
    int[] result = new int[originSize * 2];

    // Iterate through 0 to originSize - 1 (This are your indicies)
    for (int i = 0, j = 0; i < originSize; ++i, j+=2)
    {
        // i is the index to insert into the new array.
        // j holds the current position in the new array.

        // On the first iteration i = 0 is written onto the 
        // position 0 and 1 in the new array
        // after that j is incremented by 2
        // to step over the written values.
        result[j] = i;
        result[j+1] = i;
    }   

    return result;
}

答案 1 :(得分:0)

int[] sizeB_double = new int[sizeB.length()*2];

for(int i = 0; i<sizeB_double; i+=2)
{
   sizeB_double[i] = sizeB[i/2];
   if(sizeB_double.length > (i+1))
       sizeB_double[i+1] = sizeB[i/2];
}
相关问题