简体   繁体   English

我如何一次从二维数组返回一个字符串并且在我使用整个列表之前没有任何重复输出?

[英]How would I return one string at a time from a 2d array and not have any repeating ouputs until i have used the entire list?

I am trying to make a thing that selects one of five names from a 2d array of strings and return it.我正在尝试做一个从二维字符串数组中选择五个名称之一并将其返回的东西。 I only want to return one name at a time so I am using methods, but I also want the names to not repeat until all of them have been generated.我一次只想返回一个名字,所以我正在使用方法,但我也希望这些名字在生成所有名字之前不会重复。 I found something that was almost what I needed.我找到了几乎是我需要的东西。 However the output was still in a list and if I put it in a method it would just repeat.但是 output 仍在列表中,如果我将它放在一个方法中,它只会重复。

import java.util.Arrays;
  import java.util.Collections;
    import java.util.List;

public class Main {
  static void runArray() {
        String[] peoples = {"Person 1", "Person 2", "Person 3", "Person 4"};
    List<String> names = Arrays.asList(peoples);
    Collections.shuffle(names);
    for (String name : names) {
      System.out.println(name + " ");
    }
  }
  public static void main(String[] args) {
    runArray();
  }
}

One way of doing this is to randomly remove an item from the list each time you want to process a name, and continue to do so while there are more names left in the list.这样做的一种方法是每次要处理一个名称时从列表中随机删除一个项目,并在列表中还有更多名称时继续这样做。

eg例如

    private static final Random R = new Random(System.currentTimeMillis());

    private static String getRandomFromList(List<String> list) {
        final int index = r.nextInt(list.size());
        return list.remove(index);
    }

    private static void processName(String name) {
        // do stuff here
    }

    public static void main(String[] args) {
        final String[] peoples = {"Person 1", "Person 2", "Person 3", "Person 4"};
        final List<String> names = Arrays.stream(peoples).collect(Collectors.toList());


        while (!names.isEmpty()) {
            final String name = getRandomFromList(names);
            // Now do your processing for the name here e.g.
            processName(name);
        }
    }

So you can call getRandomFromList 4 times (in the above example), getting a random name each time which you can then process.因此,您可以调用getRandomFromList 4 次(在上面的示例中),每次都获得一个随机名称,然后您可以对其进行处理。 Rather than assuming a specific number of entries I have just put this in a while loop with a check on whether there are any names left.我没有假设特定数量的条目,而是将其放入一个 while 循环中,检查是否还有剩余的名称。

If you need to call it more sequentially you can do something like below.如果您需要按顺序调用它,您可以执行如下操作。 It's good to make sure there are actually names left in the list and if not you can write out an error and return early, or throw an exception.最好确保列表中确实有名字,如果没有,你可以写出错误并提前返回,或者抛出异常。

if (names.isEmpty()) {
    return; // or log error, or throw exception
}
String name = getRandomFromList(names);

// do stuff with first name

if (names.isEmpty()) {
    return; // or log error, or throw exception
}
name = getRandomFromList(names);

// do stuff with second name

// etc.

暂无
暂无

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

相关问题 我需要从这个 2D String 方法返回什么? - What would I need to return from this 2D String method? 我将如何从我的二维数组打印坐标? - How would I print coordinates from my 2d array? 如果我有一个方法名称以字符串形式存储在2D数组中,是否可以使用该字符串来调用所述方法? - If I have a method name stored in a 2D array as a String, is it possible for me use that String to call said method? 我有一个日期列表,我想成对使用,然后在pja查询中成对使用,直到所有日期都用完了为止 - i have a list of dates and i want to use then in pairs as from and to in a pja query until all dates have been used end in java 如何从一个自定义 object 列表中创建一个 HashMap,它将 String 作为键,值将是另一个 HashMap? - How to create a HashMap that would have String as key and the value would be another HashMap from one list of custom object? 我有一个2D的JButton数组,但似乎只有一个JButton被添加到我的面板中 - I have a 2D array of JButtons, but it seems only one of the JButton is getting added to my panel 我有一个文件编写器方法,只有在我关闭它时才会输出到文件 - I have a filewriter method that only ouputs to the file when i close it 我必须将 30*126 的二维数组放入 tflite model。 如何将二维浮点数组转换为 java 中的 ByteBuffer - I have to put a 2D array of 30*126 to tflite model. How to convert a 2D array of float to ByteBuffer in java Java-我有一个二维数组。 如何将它们的块加在一起? - Java - I have a 2D array. How can I add blocks of it together? 我将如何计算二维数组中的平均值? - How would I calculate the average in a 2D Array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM