1

I want to copy a larger array that some elements randomly removed by remove() method into the a small array.

I have used System.arraycopy() but this method copies elements respectively. Therefore some elements in the larger array don't be copied.

I want to copy the larger array's all non-removed elements into small array which has the length is equal to number of non-removed elements in larger array.

6
  • Why not randomly add elements from the large array into the small array? Commented Apr 5, 2017 at 18:02
  • Thanks for answering. This is really good idea but i have just tried and i could'nt do it. Could you show me how to do it please? Commented Apr 5, 2017 at 18:08
  • Welcome to Stack Overflow. Usually we require that the asker first shows an attempt, or at the very least makes a record of search and research done. It's not to be strict, it's because it's much easier to guide you when we have a clearer understanding of what challenges you. Commented Apr 5, 2017 at 18:19
  • One idea, use ArrayList instead of array, it shrinks when elements are temoved, and you will end up with a smaller ArrayList. Another idea, make a stream, filter out removed elements and collect the remainig elements into a new array. Commented Apr 5, 2017 at 18:22
  • I guess you were shuffling that "accepted" mark from one answer to the other ... accidentically? Commented Apr 5, 2017 at 18:31

2 Answers 2

1

In case you are allowed to use other "system libraries", the solution is super-simple:

  • Shuffle the large array (or a copy of it; if you have to preserve the large array as is)
  • use arraycopy() and copy smaller.length elements from larger to smaller

The point is: when you shuffle the large array, you put it into random order!

But in case you are not allowed to use "system" libraries; you "shuffle" yourself:

  • iterate larger; and for each iteration, compute a random int within larger.length
  • swap the element of the current iteration with that randomly selected index

Afterwards, you can again use arraycopy.

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

2 Comments

Could you show the code please. I couldn't get it properly.
I didn't show code on purpose. I assume this is some kind of homework; and you dont learn from other people doing your homework. But I also added another suggestion, that would boil down to two lines of code ...
0
import java.util.concurrent.ThreadLocalRandom;  //for shuffling array
import java.util.Random;                        //for shuffling solution

public class BigToSmallArray    {
    public static void main(String[] args)  {
        int[] bigArray = new int[]{0,1,2,3,4,5,6,7,8,9};
        int[] smallArray = new int[5];
        int[] shuffledBigArray = shuffleArray(bigArray);
        for(int i = 0; i < smallArray.length; i++)  {
            smallArray[i] = shuffledBigArray[i];
        }
    }

    public static int[] shuffleArray(int arr[])   {
        Random rnd = ThreadLocalRandom.current();
        for (int i = arr.length - 1; i > 0; i--) {
            int index = rnd.nextInt(i + 1);
            // Simple swap
            int a = arr[index];
            arr[index] = arr[i];
            arr[i] = a;
        }
        return arr;
    }
}

3 Comments

Thanks for your great helping. That's worked for me :)
@M.Aktas No problem. Good luck :)
@M.Aktas alternatively if you converted to using ArrayLists, theres a method that shuffles the elements in the ArrayList but i cant remember the exact method name, you can look it up on the API.

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.