Java: create array with random int's (int's can only be used once)
问题 I have an array called arr , with place for 15 elements. I need to place the numbers 1 through 15 in a random order into that array. Here is what I have tried: int[] arr = new int[15]; int i,j,k,n; for (i = 0; i<15; i++) { for (j=0; j<15; j++) { n = (int)(Math.random() * 14 + 1); if (rij[j] != n) { rij[i] = n; break; } } } Thanks! :) 回答1: Use an ArrayList and fill it up with numbers 1 to 15. Shuffle the list. Convert it to an array. 回答2: This seems like homework (or an interview question?).