Closed OMAR-3OOV closed 2 years ago
This is not a bug.
You are creating an ArrayList that is not of \
List myWrongList = new ArrayList();
myWrongList.add(Arrays.asList("element1", "element2", "element3");
myWrongList.add("simpleString1");
myWrongList.add("simpleString2");
myWrongList.add("simpleString3");
Now, the myWrongList contains 4 elements, and not 6 elements. myWrongList contains 1 List of Strings and 3 other strings
To fix your problem, rather than calling add(anotherList) you should be calling addAll
As stated, that is the intended behaviour. Thank you @EverNife for the answer.
With config.set("key", Arrays.asList(config.getStringList("key"), "newElement"))
you're creating a list of lists instead of appending the previous elements. To append multiple elements you must use addAll
.
Example to add elements to a list:
// Create the YamlFile
YamlFile config = new YamlFile("examples/test61.yml");
// Create a new config file if it does not exist or load its contents otherwise
config.createOrLoad();
// Set the list
config.set("key", new ArrayList<>(Arrays.asList("oldElement1", "oldElement2")));
// Get the previously set list (copy)
List<String> list = config.getStringList("key");
// Add several elements to the list
list.addAll(Arrays.asList("newElement1", "newElement2"));
// Add single element to the list
list.add("newElement3");
// Update the list elements
config.set("key", list);
// You can also add elements with list indexing!
config.set("key[-1]", "lastElement");
// Save the configuration file
config.save();
key:
- oldElement1
- oldElement2
- newElement1
- newElement2
- newElement3
- lastElement
so basically I used the library for a few days, and I found out that when I have a list already and try to add an element to it, something weird happens, so I create the List first by using
config.set("key", ArrayList<String>(""))
so first I want to create empty list. but in the YML file it's not empty, so the bug is when I try to add an element like this:it displayed in the file like this
it's so weird bug...