在java中随机播放两个数组

时间:2014-02-26 08:08:09

标签: java arrays random

抱歉,我是java的初学者。我已经定义了两个数组。其中一种类型是字符串,另一种是整数。现在,我想把它们洗牌。假设id = {12,45,78,23}和name = {“math”,“physic”,“art”,“computer”}。例如,在洗牌后,数组将变为id = {78,45,23,12}和name = {“physic”,“art”,“math”,“computer”}。我写下面的代码不起作用。我该如何解决?

public class RandomNumber {

public static void main(String[] args) 
{
    long[] numbers = new long[4];
    Scanner input = new Scanner(System.in);
    Random id = new Random(4);
    String[] name = new String[4];

    for (int i=0; i<=numbers.length; i++)
    {
        System.out.print("Enter the numbers: ");
        numbers[i] = input.nextLong();
    }
    for (int i=0; i<=numbers.length; i++)
    {
        int randomPosition = id.nextInt(4);
        long temp = numbers[i];
        numbers[i] = randomPosition;
        numbers[randomPosition] = temp;
    }
    for (int i=0; i<name.length; i++)
    {
        System.out.println("Enter the name: ");
        name [i] = input.nextLine();
    }
    for (int i=0; i<name.length; i++)
    {
        int randomPosition = id.nextInt(4);
        String temp = name[i];
        name[i] = randomPosition;
        name [randomPosition] = temp;
    }
    for (int i=0; i<numbers.length; i++)
    {
        System.out.println(i + " ID = " + numbers[i] + " and name = " + name[i]);
    }
}  
}

5 个答案:

答案 0 :(得分:1)

可以获取这些数组并将它们添加到List中,然后使用Collections中的shuffle方法:

Collections.shuffle(List myList);

针对不同的问题,请参阅相同的答案: How can I make this into a loop?

答案 1 :(得分:1)

由于您有两个值信息,为什么不使用Map

    Map<Integer, String> toRandomize = new HashMap<Integer, String>();
    toRandomize.put(1, "One");
    toRandomize.put(2, "Two");
    toRandomize.put(3, "Etc");

    Random r = new Random();

    List<Integer> keys = new ArrayList<Integer>(toRandomize.keySet());
    while (!keys.isEmpty()) {
        Integer key = keys.remove(r.nextInt(keys.size()));
        String val = toRandomize.get(key);
        System.out.println("key=" + key + ", val=" + val);
    }

答案 2 :(得分:0)

你应该这样做:

    int randomPosition = id.nextInt(4);
    long temp = numbers[i];
    numbers[i] = numbers[randomPosition];
    numbers[randomPosition] = temp;

你的第3行不同。

numbers[i] = randomPosition;

这同样适用于名字。

你还有其他一些错误。

这是您修改的代码 与你的比较。
你会看到改变了什么。

import java.util.Random;
import java.util.Scanner;

public class RandomNumber {

    public static void main(String[] args) {
        long[] numbers = new long[4];
        Scanner input = new Scanner(System.in);
        Random id = new Random(4);
        String[] name = new String[4];

        System.out.print("Enter the numbers: ");

        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = input.nextLong();
        }

        for (int i = 0; i < numbers.length; i++) {
            int randomPosition = id.nextInt(4);
            long temp = numbers[i];
            numbers[i] = numbers[randomPosition];
            numbers[randomPosition] = temp;
        }

        System.out.println("Enter the names: ");

        for (int i = 0; i < name.length; i++) {
            name[i] = input.next();
        }

        for (int i = 0; i < name.length; i++) {
            int randomPosition = id.nextInt(4);
            String temp = name[i];
            name[i] = name[randomPosition];
            name[randomPosition] = temp;
        }
        for (int i = 0; i < numbers.length; i++) {
            System.out.println(i + " ID = " + numbers[i] + " and name = " + name[i]);
        }
    }
}

答案 3 :(得分:0)

Using Collections to shuffle an array of primitive types is a bit of an overkill...

It is simple enough to implement the function yourself, using for example the http://en.wikipedia.org/wiki/Fisher-Yates_shuffle

import java.util.*;

class Test
{
  public static void main(String args[])
  {
    int[] solutionArray = { 1, 2, 3, 4, 5, 6, 16, 15, 14, 13, 12, 11 };

    shuffleArray(solutionArray);
    for (int i = 0; i < solutionArray.length; i++)
    {
      System.out.print(solutionArray[i] + " ");
    }
    System.out.println();
  }

  // Implementing Fisher–Yates shuffle
  static void shuffleArray(int[] ar)
  {
    Random rnd = new Random();
    for (int i = ar.length - 1; i > 0; i--)
    {
      int index = rnd.nextInt(i + 1);
      // Simple swap
      int a = ar[index];
      ar[index] = ar[i];
      ar[i] = a;
    }
  }
}

答案 4 :(得分:0)

你需要的只是

Arrays.sort(numbers, randomComparator);

其中randomComparator是Comparator的一个实例,它将随机选择2个元素之间的顺序:

// assuming T is your array type:
int compare(T o1, T o2) {
  return (int)Math.signum(Math.random() * 2 - 1);
}

这是做什么的? Math.random() * 2 - 1将生成介于-1和1之间的数字。关键是它可以生成负数,正数或零。 Math.signum将其转换为-1,0,1。

这就是全部,享受!