Solved Efficient way to remove and add ArrayList<String>

Discussion in 'Plugin Development' started by WHQ, Oct 20, 2015.

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

    WHQ

    hey guys,

    I am making a plugin and i want to know a more efficient way to add and remove arraylists
    Right now i first remove all lists and then add the list i want, but that takes a lot of time to write.

    right now i have something like this (this is not my actual plugin, just an example):
    Code:
    
    private List<String> carrot = new ArrayList<String>();
    private List<String> potato = new ArrayList<String>();
    private List<String> apple = new ArrayList<String>();
    
    public boolean onCommand(CommandSender sender, Command cmd, String label, String [] args){
        Player p = (Player) sender;
        if(cmd.getName().equalsIgnoreCase("add")){
            if(args.length >= 2){
            p.sendMessage(ChatColor.DARK_RED+"Too many arguments");   
            }
            if(args.length == 1){
            if(args[0].equalsIgnoreCase("apple")){
    
            carrot.remove(p.getName());
            potato.remove(p.getName());
            apple.add(p.getName());
            p.sendMessage(ChatColor.GREEN+"" You have selected the 'apple' ");
            }
    
     
  2. Offline

    tkuiyeager1

    @WHQ maybe try to make a method that will do that.
     
  3. Offline

    WHQ

    @tkuiyeager1

    Yeah im thinking about doing something with:

    ArrayList<String>().clear();

    i have no idea if it works though and how to do that
     
  4. Offline

    tkuiyeager1

    @WHQ i didnt mean to that, i meant to a method that you will create your self, the method will remove the player from the other lists and add him to the list you want.
     
  5. @WHQ Why do you have 3 lists? Why not just a HashMap?
    Also store UUID not name
     
  6. Offline

    teej107

    No it doesn't unless you are adding tons to the ArrayList (even then it's still fast). But a better Collection to use would be a Set. Use a HashSet.
     
  7. Offline

    WHQ

    @teej107 @bwfcwalshy @tkuiyeager1
    thanks for all your advice, i will try to look into hashmaps, my java knowledge is pretty limited so maybe it wasn't the brightest idea to make bukkit plugins right away ;)

    For now this thread is solved
     
Thread Status:
Not open for further replies.

Share This Page