Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将hashmap中小于x值的键放入arraylist,使其在每次键小于x值时都重新生成x值_Java_Arraylist_Hashmap_Bukkit - Fatal编程技术网

Java 如何将hashmap中小于x值的键放入arraylist,使其在每次键小于x值时都重新生成x值

Java 如何将hashmap中小于x值的键放入arraylist,使其在每次键小于x值时都重新生成x值,java,arraylist,hashmap,bukkit,Java,Arraylist,Hashmap,Bukkit,我正在制作一个Bukkit插件,玩家可以使用法力和法术。每个法术都有自己的法力消耗,玩家从20法力开始 我有一个ArrayList存储法力低于20的玩家,还有一个HashMap存储玩家当前的法力。我需要知道如何制作一种方法,在玩家的魔法值低于20的情况下,每秒增加1点魔法值 每次玩家进入ArrayList时,我都需要这样做,当他们达到20法力值时,我会将他们从列表中删除 这是我目前的代码: public HashMap<String, Integer> mana = new Hash

我正在制作一个Bukkit插件,玩家可以使用法力和法术。每个法术都有自己的法力消耗,玩家从20法力开始

我有一个
ArrayList
存储法力低于20的玩家,还有一个
HashMap
存储玩家当前的法力。我需要知道如何制作一种方法,在玩家的魔法值低于20的情况下,每秒增加1点魔法值

每次玩家进入
ArrayList
时,我都需要这样做,当他们达到20法力值时,我会将他们从列表中删除

这是我目前的代码:

public HashMap<String, Integer> mana = new HashMap<String, Integer>();
public ArrayList<String> changedMana = new ArrayList<String>();

public void onManaChange(){
  //code goes here
}
public HashMap mana=new HashMap();
public ArrayList changedMana=new ArrayList();
公共管理变更(){
//代码在这里
}

将其更改为一个
树状图,并使用。

您可以每秒运行一次任务计时器(这应该放在您的
onEnable()方法中):

并向
阵列列表中的玩家添加法力:

List<String> toRemove = new ArrayList<String>();

for(String player : changedMana){ //loop through all of the players in the ArrayList
   int current = mana.get(player); //get the player's current mana
   if(current < 20){ //if the player's mana is less than 20
       current++; //add to the player's mana
       mana.put(player, current); //update the player's mana
   }
   else if(current == 20){ //if the player's mana is greater than or equal to 20
       toRemove.add(player); //add the player to the toRemove list, in order to remove the player from the ArrayList later safely
   }
   else{
     //do something if the current mana is > 20, if you don't want to, just remove this
   }
}

changedMana.removeAll(toRemove); //remove players from the changed mana array

你能简单地解释一下你的问题吗。我认为这些
法力
&
法术
的措辞让问题变得有点复杂。好吧,有些法术需要消耗法力,施放法术需要法力…为什么?有什么区别吗?树状图有保证的顺序,而散列图没有t@L3n因为
Treemap
具有
headMap()
方法,而
HashMap
则没有。“Iterator.remove”是在迭代过程中修改集合的唯一安全方法;如果在迭代过程中以任何其他方式修改基础集合,则该行为未指定。
if(current>=20)
检查非常丰富,因为唯一可能的fall-through-from
if(current<20)else
已与条件匹配。代码也是次优的,因为构造了一个新的
列表
来处理从原始
列表
中删除的第二个循环,其中as
removeAll
会做得很好-尽管最佳方法是只使用
迭代器
并使用
else迭代器.remove()
,它节省了对象构造,并且易于维护。
List<String> toRemove = new ArrayList<String>();

for(String player : changedMana){ //loop through all of the players in the ArrayList
   int current = mana.get(player); //get the player's current mana
   if(current < 20){ //if the player's mana is less than 20
       current++; //add to the player's mana
       mana.put(player, current); //update the player's mana
   }
   else if(current == 20){ //if the player's mana is greater than or equal to 20
       toRemove.add(player); //add the player to the toRemove list, in order to remove the player from the ArrayList later safely
   }
   else{
     //do something if the current mana is > 20, if you don't want to, just remove this
   }
}

changedMana.removeAll(toRemove); //remove players from the changed mana array
public HashMap<String, Integer> mana = new HashMap<String, Integer>();
public ArrayList<String> changedMana = new ArrayList<String>();

@Override
public void onEnable(){
  Bukkit.getServer().getScheduler().runTaskTimer(plugin, new Runnable(){
    public void run(){
      List<String> toRemove = new ArrayList<String>();

      for(String player : changedMana){ //loop through all of the players in the ArrayList
        int current = mana.get(player); //get the player's current mana
        if(current < 20){ //if the player's mana is less than 20
           current++; //add to the player's mana
           mana.put(player, current); //update the player's mana
        }
        else if(current == 20){ //if the player's mana is greater than or equal to 20
           toRemove.add(player); //add the player to the toRemove list, in order to remove the player from the ArrayList later safely
        }
        else{
           //do something if the current mana is > 20, if you don't want to, just remove this
        }
      }

      changedMana.removeAll(toRemove); //remove players from the changed mana array
    }
  },20L,20L);
}