Solved Help Keepin ArrayList in a File

Discussion in 'Plugin Development' started by HenrySartori, Sep 18, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    HenrySartori

    Hey guys, so, I want to keep the ArrayList even after i reload my server, bug to do this i need to store my arraylist in a file.

    It would be easy to do like 2 months ago, but I was having so much trouble with family and school that I just came back to java and bukkit right now, and I completely forgot how to save my ArrayList in a file.

    I`ve read some other questions in the forum, and read some posts in other sites but i couldn`t understand it at all. If you guys could help me, here we go:

    THIS IS ALL THE ARRAYLISTS I WANT TO SAVE
    Code:
    public static ArrayList<String> booked = new ArrayList<String>();
        public static ArrayList<String> mage = new ArrayList<String>();
        public static ArrayList<String> warrior = new ArrayList<String>();
        public static ArrayList<String> paladin = new ArrayList<String>();
     
  2. Offline

    RenditionsRule

    @HenrySartori
    Personally, I'd opt for a regular List<String>, then just create a new config path with the list as a value.
     
  3. Offline

    frostalf

    @HenrySartori First thing, its bad to have Arrays classified as static. As @RenditionsRule said, should use a List<String> instead as such.

    Code:
    List<String> mage = new ArrayList<String>();
    
    Now to help you, just store it into a yaml file.

    Code:
    public void onDisable() {
      int i = 0;
      List<String> booked = new ArrayList<String>();
      for (String books : booked) {
      getConfig().createSection("books." + i + books);
      i++;
      }
    }
    
    and then to retrieve it just do this.

    Code:
    List<String> booked = new ArrayList<String>();
    for (String books : getConfig().getConfigurationSection("books").getKeys(false)) {
    booked.add(books);
    }
    
     
  4. Offline

    mythbusterma

    @frostalf

    They aren't arrays, they're Lists, and using the parent type doesn't really change anything.

    @HenrySartori

    Judging by context, these are players, and you should be storing their UUIDs instead. I.e. List<UUID>
     
  5. Offline

    frostalf

    @mythbusterma
    Whether you use the ArrayList<> or List<>, it won't change the fact of how to iterate them and placing them in a file such as yaml. Nor does it change how to iterate the yaml file and placing them back into a List or ArrayList.

    But you are correct that my example code doesn't make use of the ArrayList functions as I am using the parent of it.
     
    Last edited: Sep 18, 2016
  6. Offline

    HenrySartori

    DUDE, I LOVE YOOUUU!
    Thanks bro, that createSection was the thing i couldn't remember, now i can end my plugin =3
    thanks dude!
     
  7. Offline

    bwfcwalshy Retired Staff

    @HenrySartori Change them Lists to a Map<String/Enum, UUID> then you can use 1 Map rather than 5 Lists
     
  8. Offline

    Zombie_Striker

    @HenrySartori
    If your problem has been solved, mark this thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page