Java: create array with random int's (int'

2020-07-28 08:55发布

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! :)

5条回答
孤傲高冷的网名
2楼-- · 2020-07-28 09:01

Use an ArrayList and fill it up with numbers 1 to 15.

Shuffle the list.

Convert it to an array.

查看更多
▲ chillily
3楼-- · 2020-07-28 09:04

This will leave the elements randomly shuffled in a Integer[], if that's fine with you:

List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 15; i++)
    list.add(i + 1);

Collections.shuffle(list);
Integer[] arr = list.toArray(new Integer[0]);
查看更多
做自己的国王
4楼-- · 2020-07-28 09:08

Do it like this

// Create an ordered list
List<Integer> list = new ArrayList<Integer>();
for (int i = 1; i < 16; i++) {
    list.add(i);
}

// Shuffle it
Collections.shuffle(list);

// Get an Integer[] array
Integer[] array1 = list.toArray(new Integer[list.size()]);

// Get an int[] array
int[] array2 = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
    array2[i] = list.get(i);
}
查看更多
Juvenile、少年°
5楼-- · 2020-07-28 09:09

I will do something like this:

First create a temporary arraylist filled with numbers from start to end, then using random select a number, copy it into array and remove it from the temp arraylist, repeat until the arraylist is empty...

    ArrayList<Integer> arr = new ArrayList<Integer>();
    int[] arr2 = new int[15];
    int i,j,k,n;

    for (i=0;i<15;i++) arr.add(i+1);
    i=0;
    while(arr.size()>0){                    
         n = (int)(Math.random() * (14 + 1 - i));
         arr2[i]=arr.get(n);
         arr.remove(n);
         i++;
    }   
查看更多
神经病院院长
6楼-- · 2020-07-28 09:26

This seems like homework (or an interview question?). If that's the case and you are required to use arrays rather than the built in methods with the Java Collection Objects, (or even if not, really), the answer is the Fisher-Yates Shuffle algorithm

The modern in-place shuffle is:

To shuffle an array a of n elements (indexes 0..n-1):
for i from n − 1 downto 1 do
   j ← random integer with 0 ≤ j ≤ i
   exchange a[j] and a[i]

(I'd have to check, but I suspect this is what Java uses under the hood for its shuffle() methods).

Edit because it's fun to implement algorithms:

In java, this would be:

public static void main(String[] args) {

    int[] a = new int[15];
    for (int i = 1; i <= 15; i++)
    {
        a[i-1] = i;
    }

    Random rg = new Random();
    int tmp;
    for (int i = 14; i > 0; i--)
    {
        int r = rg.nextInt(i+1);
        tmp = a[r];
        a[r] = a[i];
        a[i] = tmp;
    }

    for (int i = 0; i < 15; i++)
        System.out.print(a[i] + " ");

    System.out.println();


}

And ... this can be further optimized using the inside-out version of the algo since you're wanting to insert a known series of numbers in random order. The following is the best way to achieve what you stated as wanting to do as there are no extra copies being made such as when creating an ArrayList and having it copy back out to an array.

a = new int[15];
Random rg = new Random();
for (int i = 0; i < 15; i++)
{
    int r = rg.nextInt(i+1);
    a[i] = a[r];
    a[r] = i+1;
}
查看更多
登录 后发表回答