如何在没有任何重复/重复字符串的情况下随机化数组?

时间:2017-04-17 15:09:48

标签: java android arrays random

我可以随机化我设置为android中每个按钮的字符串,但是有相同字母的重复项。如何在没有任何重复字母的情况下进行此操作?

    Random random = new Random();
    String[] letters = {"G","O","K","U","H","A","N","L","Z","M"};

    c1r1 = (Button) findViewById(R.id.btn1);
    c2r1 = (Button) findViewById(R.id.btn2);
    c3r1 = (Button) findViewById(R.id.btn3);
    c4r1 = (Button) findViewById(R.id.btn4);
    c5r1 = (Button) findViewById(R.id.btn5);
    c1r2 = (Button) findViewById(R.id.btn6);
    c2r2 = (Button) findViewById(R.id.btn7);
    c3r2 = (Button) findViewById(R.id.btn8);
    c4r2 = (Button) findViewById(R.id.btn9);
    c5r2 = (Button) findViewById(R.id.btn10);
    first = (Button) findViewById(R.id.first);
    second = (Button) findViewById(R.id.second);
    third = (Button) findViewById(R.id.third);
    fourth = (Button) findViewById(R.id.fourth);

    c1r1.setText("" + letters[random.nextInt(letters.length)]);
    c2r1.setText("" + letters[random.nextInt(letters.length)]);
    c3r1.setText("" + letters[random.nextInt(letters.length)]);
    c4r1.setText("" + letters[random.nextInt(letters.length)]);
    c5r1.setText("" + letters[random.nextInt(letters.length)]);
    c1r2.setText("" + letters[random.nextInt(letters.length)]);
    c2r2.setText("" + letters[random.nextInt(letters.length)]);
    c3r2.setText("" + letters[random.nextInt(letters.length)]);
    c4r2.setText("" + letters[random.nextInt(letters.length)]);
    c5r2.setText("" + letters[random.nextInt(letters.length)]);

3 个答案:

答案 0 :(得分:1)

private String[] letters = {"G","O","K","U","H","A","N","L","Z","M"};
private List<String> list = new ArrayList<String>(Arrays.asList(letters ));

private String getUniqueRandomString() {
      int i = ThreadLocalRandom.current().nextInt(0, letters.size() + 1);
      String s = letters.get(i);
      letters.remove(i);
      return s;
}
private void init() { 

    c1r1 = (Button) findViewById(R.id.btn1);
    c2r1 = (Button) findViewById(R.id.btn2);
    c3r1 = (Button) findViewById(R.id.btn3);
    c4r1 = (Button) findViewById(R.id.btn4);
    c5r1 = (Button) findViewById(R.id.btn5);
    c1r2 = (Button) findViewById(R.id.btn6);
    c2r2 = (Button) findViewById(R.id.btn7);
    c3r2 = (Button) findViewById(R.id.btn8);
    c4r2 = (Button) findViewById(R.id.btn9);
    c5r2 = (Button) findViewById(R.id.btn10);
    first = (Button) findViewById(R.id.first);
    second = (Button) findViewById(R.id.second);
    third = (Button) findViewById(R.id.third);
    fourth = (Button) findViewById(R.id.fourth);

    c1r1.setText(getUniqueRandomString());
    c2r1.setText(getUniqueRandomString());

// ... 

}

答案 1 :(得分:1)

你可以使用     Collections.shuffle(列表列表)

答案 2 :(得分:0)

public ArrayList<String> arrayList;

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    arrayList=new ArrayList<String>();

    String[] letters = {"G","O","K","U","H","A","N","L","Z","M"};
    for (String s:letters)
      arrayList.add(s);



    c1r1.setText("" + getRandom());
    c2r1.setText("" + getRandom());
    ...
    ...

}


public String getRandom(){

    String randomString=arrayList.get(random.nextInt(arrayList.size()));
    arrayList.remove(randomString);
    return randomString;

}