1 Replies - 6071 Views - Last Post: 27 June 2011 - 05:30 PM Rate Topic: -----

#1 Puddle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-June 11

Doubling the array size and returning a random array

Posted 27 June 2011 - 04:56 AM

Hi, I've written the following 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:

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 to make sure the sizes doesn't matter.

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Doubling the array size and returning a random array

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Doubling the array size and returning a random array

Posted 27 June 2011 - 05:30 PM

What about ?
int[] doubleArrayFromArrayList(ArrayList<Integer> al) {
    int[] result = new int[al.size() * 2);
    int k = 0;
    for(int i : al) {
       result[k++] = i;
       result[k++] = i;
    }
    return result;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1