0

I have very big multi dimension array for example 2d inside for loop. I would like to return one element from this array at each iteration and this element should not returned before. I mean return an element once in the iteration.

2
  • 1
    Please show some code for what you've tried and where you are stuck. Commented Jan 19, 2016 at 17:24
  • Will you eventually take all (or most) of the elements? I.e. is the number of iterations equal to (or close to) the number of elements in the array? Commented Jan 19, 2016 at 18:15

1 Answer 1

1

Without seeing any code, this is what I would try.

  • Make an identically sized 2D array with just Booleans all set to True (available) by default
  • When your code randomly generates an X,Y location in your 2D array, check the Availability array first:
    • If the value at that location is True (available), return that value in the other Array (whatever values are stored there) and then set that available value to False.
    • If the value at that location is False (not available), keep trying the next value in the array until you find one available. (Do this instead of hitting the random number generator again. The less elements available, the more you'd have to "re-roll" which would eventually become painfully slow.)

Make sense?

EDIT: I can think of at least 2 other ways of doing this that might be faster or more efficient, but this is the simple verson.

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

2 Comments

Many thanks could you please tell me if there is a function choose an element from 2d array
I think you just do array[x][y] and that returns the element at location x,y in your 'array' (or whatever you've named your array.)

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.