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.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
M. Aktas
  • 131
  • 1
  • 3
  • 14
  • Why not randomly *add* elements from the large array into the small array? – CraigR8806 Apr 05 '17 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? – M. Aktas Apr 05 '17 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. – Ole V.V. Apr 05 '17 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. – Ole V.V. Apr 05 '17 at 18:22
  • I guess you were shuffling that "accepted" mark from one answer to the other ... accidentically? – GhostCat Apr 05 '17 at 18:31
  • I've created array list. But i never thought it has a such feature. It's easier to use Array list. Thansks. – M. Aktas Apr 05 '17 at 18:44

2 Answers2

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.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Could you show the code please. I couldn't get it properly. – M. Aktas Apr 05 '17 at 18:13
  • 1
    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 ... – GhostCat Apr 05 '17 at 18:29
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;
    }
}
noobcoder
  • 323
  • 1
  • 3
  • 12