Args to Array

Discussion in 'Plugin Development' started by DaanSander, Mar 26, 2015.

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

    DaanSander

    Hello i am trying to make a command so when i do /string test test test it will add all the args to a arraylist and print them out into the console

    what i have tried:
    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("string")) {
                ArrayList<String[]> test = new ArrayList<String[]>();
                test.add(args);
                Bukkit.getConsoleSender().sendMessage(test.toString());
            }
            return true;
        }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @DaanSander for(int i = 1;i<args.length;i++){test.add(args[i]);}
     
    Last edited: Mar 26, 2015
    tomudding likes this.
  3. Offline

    DaanSander

    @timtower when i do that and it sends me []

    new code:
    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("string")) {
                Player p = (Player) sender;
                ArrayList<String[]> test = new ArrayList<String[]>();
                for(int i = 1;i<args.length;i++) {
                    test.add(args);
                }
                p.sendMessage(test.toString());
            }
            return true;
        }
     
  4. Offline

    timtower Administrator Administrator Moderator

    @DaanSander You are adding the entire args, to the list, you only need to add 1 arg at the time.
    Realized that it was the forum messing it up, see the edit
     
  5. @DaanSander @timtower
    Code:
    ArrayList<String[]> test = new ArrayList<String[]>(Arrays.asList(args));
     
  6. Offline

    timtower Administrator Administrator Moderator

    @Assist Why does your list contain arrays?
     
    teej107 likes this.
  7. @timtower
    Oops, I copied the ArrayList from his post and forgot to change the string arrays to strings :oops:
    Code:
    ArrayList<String> test = new ArrayList<String>(Arrays.asList(args));
     
  8. Offline

    timtower Administrator Administrator Moderator

    @Assist That would make an immutable list though with all arguments. Not sure if that is what the OP wants.
     
  9. Offline

    teej107

    That's a little redundant.

    EDIT: @timtower It's still modifiable. He is adding the contents of the immutable List to the modifiable ArrayList
     
  10. It's what he asked, though.
    "it will add all the args to a arraylist"
     
  11. Offline

    teej107

    @Assist Unless the OP doesn't need to edit the List, then it is redundant. The Arrays#asList() already returns a List.
     
  12. @teej107
    I don't know what I was thinking... Maybe I shouldn't work on 4 things at the same time.
     
  13. Offline

    1Rogue

    The return value of Arrays#asList should be placed into a concrete list implementation. The return value isn't an immutable list, it's a (private static) class implementation of an ArrayList which is fixed-size (but modifiable). When you store the list as a generic "List" type (because of Liskov's substitution principle), you forego the benefit of clearly seeing that the list you are working with is fixed sized (or immutable, for other cases)., and as such you should not store unmodifiable/fixed-size references.
     
    teej107 likes this.
  14. Offline

    teej107

    @1Rogue Good argument!
    EDIT: But if the OP has no intention to edit the value of Arrays#asList(), it wouldn't make a difference.

    EDIT 2: From trying to understand what the OP wants, it would be best to use a List (add the contents from Arrays#asList() into a seperate List if you plan on using that List later)
     
    Last edited: Mar 26, 2015
Thread Status:
Not open for further replies.

Share This Page