简体   繁体   English

将对象从一个 YAML 列表移动到另一个列表时发生奇怪的事情

[英]Weird stuff happening when moving objects from one YAML list to other

I have a problem regarding YAML files.我对 YAML 文件有疑问。 When I try moving objects from one list to another inside of a.yml file and save it, weird stuff like this happens:当我尝试在 a.yml 文件中将对象从一个列表移动到另一个列表并保存时,会发生这样的奇怪事情:

queued: &id001 []
current: *id001

What's supposed to happen:应该发生什么:

current: Fraithor # <= Minecraft username, from queued, moved when running reQueue()
queued: [] # <= Or just nothing / delete list

reQueue() method: reQueue() 方法:


        for (String path : FileBasics.FILETYPE.BANWAVE.getConfig().getStringList("queued")) {

            List<String> list = FileBasics.FILETYPE.BANWAVE.getConfig().getStringList("current");
            List<String> arrayList = new ArrayList<>();
            arrayList.add(path);

            list.addAll(arrayList);
            FileBasics.FILETYPE.BANWAVE.getConfig().set("current", list);
            list.remove(path);
            FileBasics.FILETYPE.BANWAVE.getConfig().set("queued", list);
            try {
                FileBasics.FILETYPE.BANWAVE.getConfig().save(FileBasics.FILETYPE.BANWAVE.getFile());
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

Obviously, I'm using a seperate class/emnum for the File saving (FileBasics).显然,我使用单独的类/emnum 来保存文件(FileBasics)。 It's a pretty long code, so I pasted it on pastebin HERE ( https://pastebin.com/XsqvYTBb )这是一个很长的代码,所以我将它粘贴在 pastebin HERE ( https://pastebin.com/XsqvYTBb )

You're pushing the same list to both current and queued .您将相同的列表推送到currentqueued Java has reference semantics meaning that if you push arrayList to current , current holds a reference to arrayList so if you alter it, current will point to the altered list. Java 具有引用语义,这意味着如果您将arrayList推到currentcurrent持有对arrayList引用,因此如果您更改它, current将指向更改后的列表。

Since you push the very same list to queued , YAML will add an anchor &id001 when the list first occurs, and an alias *id001 to refer to the same list afterwards.由于您将相同的列表推送到queued ,因此 YAML 将在列表首次出现时添加一个锚点&id001 ,并在之后添加一个别名*id001来引用相同的列表。

Since you don't want a list in current at all, it seems you want to do由于您根本不想要current列表,因此您似乎想做

FileBasics.FILETYPE.BANWAVE.getConfig().set("current", path);

Though I have no idea what kind of API this is and whether it accepts a string.虽然我不知道这是哪种 API 以及它是否接受字符串。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM