Java 如何在找到可用对象之前尝试某项操作

Java 如何在找到可用对象之前尝试某项操作,java,Java,标题可能有点混乱,但我真的不知道该如何解释。我有一个对象列表,在本例中是位置,这些位置可以被玩家占据。如果所选位置已被占用,我如何尝试查找新位置,并继续此操作,直到找到未占用的位置 我已经知道有20个位置,我可以手动检查每个位置,看看是否有人占用,但是有更好的方法吗 下面是我的代码片段 List<Location> spawnList = arena.getManager().getRandomSpawns(); // Returns a list of possible locat

标题可能有点混乱,但我真的不知道该如何解释。我有一个对象列表,在本例中是位置,这些位置可以被玩家占据。如果所选位置已被占用,我如何尝试查找新位置,并继续此操作,直到找到未占用的位置

我已经知道有20个位置,我可以手动检查每个位置,看看是否有人占用,但是有更好的方法吗

下面是我的代码片段

List<Location> spawnList = arena.getManager().getRandomSpawns(); // Returns a list of possible locations
Location random = spawnList.get(new Random().nextInt(spawnList.size())); // Selects a random location from the list

if (random.isOccupied()) {
    /* Location is occupied, find another one from the list, and continue doing this until non-occupied location is found */
}
List spawnList=arena.getManager().getrandompawns();//返回可能位置的列表
Location random=spawnList.get(new random().nextInt(spawnList.size());//从列表中选择一个随机位置
if(random.isocupied()){
/*位置已被占用,请从列表中找到另一个位置,然后继续执行此操作,直到找到未占用的位置*/
}
抱歉,如果您不明白,我不知道如何解释这一点。

List-spawnList=arena.getManager().getrandomsawans();
List<Location> spawnList = arena.getManager().getRandomSpawns();
Location random;
Random r = new Random();

do {
  random = spawnList.get(r.nextInt(spawnList.size()))
} while(random.isOccupied());
位置随机性; 随机r=新随机(); 做{ random=spawnList.get(r.nextInt(spawnList.size())) }while(random.isocupied());

如果所有位置都被占用,则此操作将失败,您应该在之前检查此操作。

您可以选择以下两种方式之一:

  • 推送-当某个位置可用时,通知该位置现在可用。(例如,通过调用方法)

  • 投票:就像你现在做的那样。可以保存可用位置的集合,当某个位置可用时,它将添加到集合中。您可以等待列表具有值。我建议:


  • 您可以声明一个标志来检查是否找到了候选位置,并使用while-loop生成随机位置,如

        Location random = null;
    
    boolean foundLocation = false;
    while(!foundLocation)
    {
        random = spawnList.get(new Random().nextInt(spawnList.size()));
        if(!random.isOccupied())
        {
            foundLocation = true;
        }
    }
    

    注意:此处假设位置列表中至少有一个位置未被占用。如果所有位置都被占用。则不能使用上述代码。它将处于无限循环中。我们最好先检查列表中是否至少有一个位置未被占用。

    简单的方法是在循环中随机化一个位置,直到找到一个:

    List<Location> spawnList = arena.getManager().getRandomSpawns(); // Returns a list of possible locations
    Location random = spawnList.get(new Random().nextInt(spawnList.size())); // Selects a random location from the list
    
    while (random.isOccupied()) {
        random = spawnList.get(new Random().nextInt(spawnList.size()));
    }
    
    List spawnList=arena.getManager().getrandompawns();//返回可能位置的列表
    Location random=spawnList.get(new random().nextInt(spawnList.size());//从列表中选择一个随机位置
    while(random.isocupied()){
    random=spawnList.get(new random().nextInt(spawnList.size());
    }
    
    这里的问题是,如果大部分位置已经被占用,这可能需要很长时间

    一种“更安全”的方法,无论预占用位置的百分比如何,都能保证相同的性能顺序,它可以是洗牌位置列表,然后简单地迭代:

    List<Location> spawnList = new LinkedList<Location>(arena.getManager().getRandomSpawns());
    Location random = null;
    
    for (Location loc : spawnList) {
        if (!loc.isOccupied()) {
            random = loc;
        }
    }
    
    List-spawnList=newlinkedlist(arena.getManager().getrandomsawns());
    位置随机=空;
    对于(位置loc:SPOWNLIST){
    如果(!loc.isocupied()){
    随机=loc;
    }
    }
    
    不要在到达一个空白点之前进行随机探测,你应该

  • 首先收集所有可用位置,然后
  • 选择一个随机的空闲位置

  • List freeLocations=new ArrayList();
    对于(int i=0;i
    spawnList的大小是多少。它是否太大而无法循环?20个位置对于迭代来说不是一个大数字。你真的需要提高效率吗?@jazz11在这种情况下没有,但我想知道如果我将来需要做类似的事情,是否有办法做到这一点。
    List<Integer> freeLocations = new ArrayList<>();
    for (int i = 0; i < spawnList.size(); i++)
      if (!spawnList.get(i).isOccupied) freeLocations.add(i);
    
    Location random = 
       spawnList.get(freeLocations.get(rnd.nextInt(freeLocations.size()));