Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Doubling The Array Size And Randomizing Array Return

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    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.
    Last edited by Pingu00; June 27th, 2011 at 08:46 AM. Reason: Made the problem more clearer?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Doubling The Array Size And Randomizing Array Return

    I'm having trouble incorporating it into the original code.
    Please explain the problem.

    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.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Doubling The Array Size And Randomizing Array Return

    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?
    I'm trying to modify this so that ... I can create arrays which are double sizeB and duplicate each index value.

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

  7. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Doubling The Array Size And Randomizing Array Return

    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.

  9. #9
    Junior Member
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

  11. #11
    Junior Member
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Doubling The Array Size And Randomizing Array Return

    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.

    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?

  13. #13
    Junior Member
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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].

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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]
    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, ???

  15. #15
    Junior Member
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Doubling The Array Size And Randomizing Array Return

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

  17. #17
    Junior Member
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Doubling The Array Size And Randomizing Array Return

    They are being used when we find the size of the arrays

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

  19. #19
    Junior Member
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Doubling The Array Size And Randomizing Array Return

    You can do it that way too

Similar Threads

  1. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  2. Search for number in array and return index
    By Kevinius in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2011, 12:00 AM
  3. Replies: 1
    Last Post: December 4th, 2010, 05:26 PM
  4. [SOLVED] Problem accessing specific data in an array and getting it to return properly
    By Universalsoldja in forum Collections and Generics
    Replies: 3
    Last Post: February 4th, 2010, 04:26 PM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM