i want copy larger array elements randomly removed remove()
method small array.
i have used system.arraycopy()
method copies elements respectively. therefore elements in larger array don't copied.
i want copy larger array's non-removed elements small array has length equal number of non-removed elements in larger array.
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 = 0; < smallarray.length; i++) { smallarray[i] = shuffledbigarray[i]; } } public static int[] shufflearray(int arr[]) { random rnd = threadlocalrandom.current(); (int = arr.length - 1; > 0; i--) { int index = rnd.nextint(i + 1); // simple swap int = arr[index]; arr[index] = arr[i]; arr[i] = a; } return arr; } }
Comments
Post a Comment