Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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 最有效的执行方法_Java_Arraylist_While Loop_Hashmap - Fatal编程技术网

Java 最有效的执行方法

Java 最有效的执行方法,java,arraylist,while-loop,hashmap,Java,Arraylist,While Loop,Hashmap,我目前正在使用BukkitAPI,但这与普通Java有更多的关系,所以我在这里询问。我有两个hashmap来存储数据,并且需要能够比较这两个hashmap public HashMap<Scoreboard, ArrayList<PlayerScore>> lastList = new HashMap<Scoreboard, ArrayList<PlayerScore>>(); public HashMap<Scoreboard, Array

我目前正在使用BukkitAPI,但这与普通Java有更多的关系,所以我在这里询问。我有两个hashmap来存储数据,并且需要能够比较这两个hashmap

public HashMap<Scoreboard, ArrayList<PlayerScore>> lastList = new HashMap<Scoreboard, ArrayList<PlayerScore>>();
public HashMap<Scoreboard, ArrayList<PlayerScore>> currentList = new HashMap<Scoreboard, ArrayList<PlayerScore>>();
public HashMap lastList=new HashMap();
public HashMap currentList=新HashMap();
我可以使用while循环进行迭代,这是可行的,但这有一个问题,因为我必须迭代循环中的另一个ArrayList,因为有两个hashmap,我最终总共做了4个hashmap。。。这是我当前的代码:

    public void remove(Scoreboard board) {
    Iterator<Entry<Scoreboard, ArrayList<PlayerScore>>> lastIt = lastList.entrySet().iterator();
    Iterator<Entry<Scoreboard, ArrayList<PlayerScore>>> currentIt = currentList.entrySet().iterator();
    while (lastIt.hasNext()) {
        System.out.println("dbg1");
        Entry<Scoreboard, ArrayList<PlayerScore>> nextLast = lastIt.next();
        if (nextLast.getKey().equals(board)) {
            System.out.println("dbg2");
            while (currentIt.hasNext()) {
                Entry<Scoreboard, ArrayList<PlayerScore>> nextCurrent = currentIt.next();

                ArrayList<PlayerScore> lastArray = nextLast.getValue();
                ArrayList<PlayerScore> currentArray = nextCurrent.getValue();

                Iterator<PlayerScore> lastArrayIt = lastArray.iterator();
                Iterator<PlayerScore> currentArrayIt = currentArray.iterator();
                while (lastArrayIt.hasNext()) {
                    System.out.println("dbg3");
                    PlayerScore nextCurrentArray = currentArrayIt.next();

                    while (currentArrayIt.hasNext()) {
                        System.out.println("dbg4");
                        if (!lastArray.contains(nextCurrentArray)) {
                            System.out.println("dbg5");
                            board.resetScores(nextCurrentArray.getString());
                            lastArrayIt.remove();
                            currentArrayIt.remove();
                            break;
                        }
                    }

                    break;
                }

                break;
            }
        }

        break;
    }
}
删除公共空白(记分板){
迭代器lastIt=lastList.entrySet().Iterator();
迭代器currentIt=currentList.entrySet().Iterator();
while(lastIt.hasNext()){
System.out.println(“dbg1”);
条目nextLast=lastIt.next();
if(nextLast.getKey().equals(板)){
System.out.println(“dbg2”);
while(currentIt.hasNext()){
条目nextCurrent=currentIt.next();
ArrayList lastArray=nextList.getValue();
ArrayList currentArray=nextCurrent.getValue();
迭代器lastArrayIt=lastArray.Iterator();
迭代器currentArrayIt=currentArray.Iterator();
while(lastArrayIt.hasNext()){
系统输出打印项次(“dbg3”);
PlayerScore nextCurrentArray=currentArrayIt.next();
while(currentArrayIt.hasNext()){
System.out.println(“dbg4”);
如果(!lastary.contains(nextCurrentArray)){
System.out.println(“dbg5”);
resetScores(nextCurrentArray.getString());
lastArrayIt.remove();
currentArrayIt.remove();
打破
}
}
打破
}
打破
}
}
打破
}
}
我知道,这很混乱,但我真的不知道还能做些什么。while循环执行得如此之多,以至于服务器控制台中只填充了“dbg4”,因为它的输出速度如此之快。这也会使服务器崩溃


有人知道更好的方法吗?

您不需要迭代hashmap。你可以这样做:

ArrayList<PlayerScore> lastArray = lastList.remove(board);
ArrayList<PlayerScore> currentArray = currentList.remove(board);
ArrayList lastArray=lastList.remove(板);
ArrayList currentArray=currentList.remove(板);

您不需要迭代hashmap。你可以这样做:

ArrayList<PlayerScore> lastArray = lastList.remove(board);
ArrayList<PlayerScore> currentArray = currentList.remove(board);
ArrayList lastArray=lastList.remove(板);
ArrayList currentArray=currentList.remove(板);

您不需要迭代hashmap。你可以这样做:

ArrayList<PlayerScore> lastArray = lastList.remove(board);
ArrayList<PlayerScore> currentArray = currentList.remove(board);
ArrayList lastArray=lastList.remove(板);
ArrayList currentArray=currentList.remove(板);

您不需要迭代hashmap。你可以这样做:

ArrayList<PlayerScore> lastArray = lastList.remove(board);
ArrayList<PlayerScore> currentArray = currentList.remove(board);
ArrayList lastArray=lastList.remove(板);
ArrayList currentArray=currentList.remove(板);

哦,天哪,我不知道我在想什么,现在对我来说已经很晚了。谢谢威尔·马克将在8分钟内给出答案……所以我总是很乐意帮忙:)有时最简单的解决方案是最难的:)哦,天哪,我不知道我在想什么,现在对我来说已经很晚了。谢谢威尔·马克将在8分钟内给出答案……所以我总是很乐意帮忙:)有时最简单的解决方案是最难的:)哦,天哪,我不知道我在想什么,现在对我来说已经很晚了。谢谢威尔·马克将在8分钟内给出答案……所以我总是很乐意帮忙:)有时最简单的解决方案是最难的:)哦,天哪,我不知道我在想什么,现在对我来说已经很晚了。谢谢将在8分钟内标记为答案…因此总是很乐意提供帮助:)有时最简单的解决方案是最难的:)