在Java中从ArrayList打印随机输入的值

在Java中从ArrayList打印随机输入的值,java,sorting,search,random,arraylist,Java,Sorting,Search,Random,Arraylist,我以随机顺序处理ArrayList的元素,通常是通过打印出来。我想检测随机选择的索引是0还是1,以便对这些情况执行特殊处理,其中索引0的处理部分取决于之前是否处理过索引1。具体地说,在处理索引1时不会立即打印任何内容,但如果处理了索引1,则在随后处理索引0时,会同时打印索引1和索引0值。在任何情况下,循环在十次迭代后或在处理索引0后退出,以先到者为准 我尝试使用if语句来实现这一点,但该语句存在明显的缺陷。我到处找例子,但一个也没找到。我已经开始考虑使用排序算法或线程来保存找到的第一个值,然后继

我以随机顺序处理ArrayList的元素,通常是通过打印出来。我想检测随机选择的索引是0还是1,以便对这些情况执行特殊处理,其中索引0的处理部分取决于之前是否处理过索引1。具体地说,在处理索引1时不会立即打印任何内容,但如果处理了索引1,则在随后处理索引0时,会同时打印索引1和索引0值。在任何情况下,循环在十次迭代后或在处理索引0后退出,以先到者为准

我尝试使用if语句来实现这一点,但该语句存在明显的缺陷。我到处找例子,但一个也没找到。我已经开始考虑使用排序算法或线程来保存找到的第一个值,然后继续循环,直到它看到第二个打印出来。如果有任何可能的帮助,我将不胜感激

这是我的密码:

public static void random_sortType(){ 

    types = new ArrayList<String>();
    types.add("Start");
    types.add("Starting");
    types.add("Load");
    types.add("Loading");
    types.add("End");

    ran = new Random();
    int listSize = types.size();
    String tempEventType;//the temp variable intended to hold temporary values
    for(int i = 0; i < 10; i++){ //the loop goes round the ArrayList 10 times 
        int index = ran.nextInt(listSize);//this produces the random selection of the elements within the list
        if(index == 0){
            out.println(types.get(index));
            out.println();
            break;
        }
        if(index == 1){
            tempEventType = types.get(index);
            if(index == 0){
                tempEventType = types.get(0) + " " + types.get(1);
                out.println(tempEventType);             
                break;
            }
        }/*if(index == 0){
            tempEventType = types.get(0) + " " + types.get(1);
            out.println(tempEventType);             
            break;
        }*/

        //out.print("Index is " + index + ": ");
        //out.println(types.get(index));

    }
}

您需要将随机生成的索引存储到全局变量中,并在每次生成随机数时更新它。应该是这样的

    public static void random_sortType(){ 

    types = new ArrayList<String>();
    types.add("Start");
    types.add("Starting");
    types.add("Load");
    types.add("Loading");
    types.add("End");
`   int previousIndex;
    ran = new Random();
    int listSize = types.size();
    String tempEventType;//the temp variable intended to hold temporary values
    for(int i = 0; i < 10; i++){ //the loop goes round the ArrayList 10 times 
        int index = ran.nextInt(listSize);//this produces the random selection of the elements within the list
        previous_index =index;
        if(index == 0){


    out.println(types.get(index));
        out.println();
        break;
    }
    if(index == 1){
        tempEventType = types.get(index);
        if(previousIndex == 0){
            temp EventType = types.get(0) + " " + types.get(1);
                out.println(tempEventType);             
                break;
            }

根据您的描述,以下是应用程序的基本要求:

按随机顺序处理ArrayList 处理=在随机选择的索引处打印值 尝试处理列表中的项目10次。 检测随机索引是1还是0。 如果1 不处理,但将其标记为选中。 如果标记了0&&1 进程0和1 出口 如果这些要求正确,下面是我提出的实现:

import java.util.ArrayList;
import java.util.Random;
import static java.lang.System.*;

public class RandomSort {
    private static final int MAX_ATTEMPTS = 10;
    private static boolean wasOneSelected = false;

    public static void main(String[] args) {
        ArrayList<String> types = new ArrayList<>(5);
        types.add("Start");
        types.add("Starting");
        types.add("Load");
        types.add("Loading");
        types.add("End");
        random_sortType(types);
    }

    public static void random_sortType(ArrayList<String> types) {
        Random ran = new Random();
        int lastIndex = types.size() - 1; // index range is from 0 to 4
        for (int i = 0; i < MAX_ATTEMPTS; i++) {
            int index = ran.nextInt(lastIndex);
            if ( (index == 0) && wasOneSelected) {
                process(types.get(index) + " " + types.get(index + 1));
                break;
            } else if (index == 1) {
                wasOneSelected = true;
            } else {
                process(types.get(index));
            }
        }
    }

    public static void process(String str) {
        out.println("Processing: " + str);

    }
}
这里的关键是包含初始化为false的布尔值WASONESELECT。一旦设置为true,在应用程序期间它将不再为false。if-else块处理循环中的所有分支,出于可读性的目的,我倾向于将println调用包装到名为process的方法中,以使其与您的描述更加一致


感谢反馈:

如果索引==1,则您的if index==0。怎么会这样?@Ryan…哈哈,我看到了,但留下来是为了让大家了解我的错误,也可能有助于理解我在尝试做什么?我需要的是使用索引值,当索引为0时打印值并停止,如果索引为1,等待索引为0,然后在[0,1]处打印两个值并停止,如果索引为2,则等待索引为1,然后在[1,2]处打印两个值并停止;如果索引为3,则等待索引为2,然后在[2,3]处打印两个值并停止;如果索引为4,则等待索引为3,然后在[3,4]处打印两个值然后,我只是浏览了一下我上面的问题,发现它在语法上被编辑得更好了,但完全改变了我的问题!我现在理解了给出的答案。@Tyrone…..它只在索引为1时起作用,之后它将其他值打印为单个字符串,我需要的是使用索引值。当索引为0时,打印值并停止,如果索引为1,则等待索引为0,然后在[0,1]处打印两个值并停止,如果索引为2,则等待索引为1,然后在[1,2]处打印两个值并停止;如果索引为3,则等待索引为2,然后在[2,3]处打印两个值并停止;如果索引为4,则等待索引为3,然后在[3,4]处打印两个值。。。。。我明白你的意思了:-@kk1992…..我尝试了你的解决方案,虽然它不起作用,但我想尝试一种情况,如果previousIndex&index=0,当index为1时打印值,将其存储在previousIndex中,直到index为0,然后按[0,1]顺序打印值。我认为这将解决问题,因为我将复制其余部分的逻辑,当索引为2时,存储在先前的索引中,直到索引为1,然后按[1,2]的顺序打印值