简体   繁体   English

返回我的字符串列表的随机元素,在 Android 项目中不重复

[英]Return random elements of my String List without duplicates in Android project

I have an ArrayList of 5 Strings in my Android Eclipse project as folowing:我的 Android Eclipse 项目中有一个包含 5 个字符串的 ArrayList,如下所示:

//ArrayList<String> Declaration
    ArrayList<String> al= new ArrayList<String>();
    //add method for String ArrayList
    al.add("1st_string");
    al.add("2nd_string");
    al.add("3rd_string");
    al.add("4th_string");
    al.add("5th_string");

And I want to randomly select strings without duplicates according to the following plan :我想根据以下计划随机选择没有重复的字符串:

I will have a variable named string_selection and a Textview to save me results named displayed_string我将有一个变量命名string_selection和一个TextView救我产生一个名为displayed_string

1) for string_selection == 0 ---> display the 1st element in the TextView displayed_string (and next time choose from the remaining 4 strings ) 1) for string_selection == 0 ---> 在 TextView 中显示第一个元素string_selection == 0 (下次从剩余的 4 个字符串中选择)

2) for string_selection == 1 ---> display the 2nd element in the TextView displayed_string (and next time choose from the remaining 3 strings ) 2) for string_selection == 1 ---> 在 TextView 中显示第二个元素string_selection == 1 (下次从剩余的 3 个字符串中选择)

and finally最后

3)for string_selection == 2 ---> display the 3rd element in the TextView displayed_string( and that's it.Just set string_selection == 0 ) 3)对于string_selection == 2 ---> 在 TextView 中显示第三个元素display_string (就是这样。只需设置string_selection == 0 )

How can I do it please?请问我该怎么做?

Remove items when randomly selected (store your strings in a separated array if you need to keep the original one):随机选择时删除项目(如果需要保留原始字符串,请将字符串存储在单独的数组中):

items = 1, 2, 3, 4, 5项目 = 1, 2, 3, 4, 5

Random select: 2随机选择:2

Use items[2] and remove.使用items[2]并删除。 items = 1, 2, 4, 5项目 = 1, 2, 4, 5

Random select: 1随机选择:1

Use items[1] and remove.使用items[1]并删除。 items = 1, 4, 5项目 = 1, 4, 5

So on...很快...

private List<SameType> randomList (List<Type> targetList){

    List<TypeAsTarget> newUniqueList = new ArrayList<>(targetList.size());

    newUniqueList.add(0, targetList.get(new Random().nextInt(targetList.size())));
    newUniqueList.add(1, targetList.get(new Random().nextInt(targetList.size())));
    newUniqueList.add(2, targetList.get(new Random().nextInt(targetList.size())));
    newUniqueList.add(3, targetList.get(new Random().nextInt(targetList.size())));
    newUniqueList.add(4, targetList.get(new Random().nextInt(targetList.size())));

    while (newUniqueList.get(1) == newUniqueList.get(0))
        newUniqueList.set(1, targetList.get(new Random().nextInt(targetList.size())));

    while (newUniqueList.get(2) == newUniqueList.get(0) || newUniqueList.get(2) == newUniqueList.get(1))
        newUniqueList.set(2, targetList.get(new Random().nextInt(targetList.size())));

    while (newUniqueList.get(3) == newUniqueList.get(0) || newUniqueList.get(3) == newUniqueList.get(1) || newUniqueList.get(3) == newUniqueList.get(2))
        newUniqueList.set(3, targetList.get(new Random().nextInt(targetList.size())));

    while (newUniqueList.get(4) == newUniqueList.get(0) || newUniqueList.get(4) == newUniqueList.get(1) || newUniqueList.get(4) == newUniqueList.get(2) || newUniqueList.get(4) == newUniqueList.get(3))
        newUniqueList.set(4, targetList.get(new Random().nextInt(targetList.size())));

    return newUniqueList;
}

You can either randomly remove strings from your array if you don't need them or use some set to keep track of already used random indices.如果您不需要它们,您可以从数组中随机删除字符串,或者使用一些集合来跟踪已使用的随机索引。

1) Remove 1) 删除

String getRandom(List<String> strings) {
    return strings.remove(new Random().nextInt(strings.size()));
}

2) Track used indices: 2) 跟踪使用的索引:

String getRandom(List<String> strings, Set<Integer> used) {
    int next;
    do {
        next = new Random().nextInt(strings.size());
    } while (used.contains(next));
    used.add(next);
    return strings.get(next);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM