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; }
@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; }
@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
@timtower Oops, I copied the ArrayList from his post and forgot to change the string arrays to strings Code: ArrayList<String> test = new ArrayList<String>(Arrays.asList(args));
@Assist That would make an immutable list though with all arguments. Not sure if that is what the OP wants.
That's a little redundant. EDIT: @timtower It's still modifiable. He is adding the contents of the immutable List to the modifiable ArrayList
@Assist Unless the OP doesn't need to edit the List, then it is redundant. The Arrays#asList() already returns a List.
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.
@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)