1

I am looking to fill an array a with the numbers 1 through 10 and take a random number from that array and add it to an array b and remove that element from array a. I would like to know the most efficient way of doing this. EDIT: (The exercise requires that I not have repeated values in the arrays and that the permutation is random each time the method is called.) Here is my method so far:

public int[] nextPermutation() {
    int capOne = 10;
    int capTwo = 10;
    int aSize = 0;
    int bSize = 0;


    int[] a = new int[capOne];
    int[] b = new int[capTwo];

    int upperBound = 11;
    Random generator = new Random();

    //fill initial array with 1 - 10
    for (int i = aSize; i < 10; i++) {
        a[i] = i + 1;
        //companion variable for sizing array
        aSize++;
    }

    //Create a random integer and add it to array b
    //Remove same integer from array a
    //Repeat and remove another random integer from the remaining integers in array a and    add it to b


        permuted = b;
        return permuted;
        }

I may be approaching this in an inefficient if not completely incorrect manner. If so, I'm sure you won't hesitate to tell me. Any help on this is greatly appreciated.

8
  • you better use a list Commented Jul 2, 2013 at 4:52
  • Given that the array contains ten elements, I doubt you need to worry about efficiency too much. Focus on correctness. Commented Jul 2, 2013 at 4:53
  • Hi StinePike. As in List list = Arrays.asList() or an ArrayList? Commented Jul 2, 2013 at 4:54
  • can you have repeated values? Commented Jul 2, 2013 at 4:55
  • No I cannot have repeated values. I should have specified that. My apologies. Commented Jul 2, 2013 at 4:57

2 Answers 2

1

You can:

//randomly choose element
int index = (int) (Math.random() * aSize);
int dataFromA = a[index];

//"remove" it from A
aSize--;
for(int i = index; i<aSize; i++) {
    a[i] = a[i+1];
}

//"add" it to b
b[bSize] = dataFromA;
bSize++;

Only interesting part is removing from A, where you have to reduce the size before the cycle (or you can i < aSize-1, then decrement size)

I guess you have to use arrays since this is an excersice, but using List for this would be better.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is a program producing random permutations, using swap. Well, I'm not sure which can produce a better result, but swap should be faster than add/remove to/from an array:

public int[] nextPermutation() {
    int cap = 10;
    int[] a = new int[cap];
    Random generator = new Random();

    //fill initial array with 1 - 10
    for (int i = 0; i < cap; i++) {
        a[i] = i + 1;
    }

    for (int i = 0; i < cap; i++) {
        int j = generator.nextInt(cap);
        int x = a[j];
        a[j] = a[i];
        a[i] = x;
    }

    // You can reduce the size of the output array:
    // int output[] = new int[5];
    // System.arraycopy(a, 0, output, 0, 5);
    // return output;

    return a;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.