0

I have a String named listOfItemsBanned, and an ArrayList named itemsBanned. Let's say the ArrayList itemsBanned contains 3 things: TNT, EnderPearl, and Sand. I would want the String to be "TNT, EnderPearl, Sand". And when something is removed from itemsBanned, it would remove it from the string, too.

So.. I want to be able to get every item included in an ArrayList and put it into one String with each item separated by commas.

user3395095
  • 21
  • 1
  • 2
  • 1
    Iterate the `List` and append the content to a `StringBuilder` and when you're done, use the `StringBuilder`s `toString` method to get the `String`. You will have to do this each time you want to update the `String`, there's no way to automate (so that adding a value to the `List` magically creates the `String`) - unless you really want to write some kind of wrapper class – MadProgrammer Mar 12 '14 at 01:02

3 Answers3

2

You need only one line:

String listOfItemsBanned = itemsBanned.toString().replaceAll("^.|.$", "");

toString() of List produces a CSV of the elements, but wrapped in [ and ]. The call to replaceAll() removes the first and last characters to leave just the elements.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

You could do this:

String listOfItemsBanned = "";

for(int i = 0; i < itemsBanned.size(); i++){ //loop threw contents of itemsBanned (List<String>)
    String s = itemsBanned.get(i); //get the string in itemsBanned (i)
    listOfItemsBanned = listOfItemsBanned + "," + s; //add s to listOfItemsBanned
}

Now, if you would like to get all of the items that are banned from the string listOfItemsBanned, you could do:

String[] s = listOfItemsBanned.split(",");

//start the for loop at 1 because when saved it will be ",TnT,Sand,Enderpearl", notice the extra , in the begining
for(int i = 1; i < s.size(); i++){ //loop threw all of the items banned.
    String itmBanned = s[i];
}

You could now do anything with itmBanned, like convert it to a Material:

Material m = Material.getMaterial(itmBanned);

So, you could do something like this for a remove method:

public void remove(String type){
    String listOfItemsBanned = "";

    itemsBanned.remove(type); //remove 'type' from the array

    for(int i = 0; i < itemsBanned.size(); i++){
        String s = itemsBanned.get(i);
        listOfItemsBanned = listOfItemsBanned + "," + s; //reset the string to the new list
    }
}

and for adding:

public void remove(String type){
    String listOfItemsBanned = "";

    itemsBanned.add(type); //add 'type' to the array

    for(int i = 0; i < itemsBanned.size(); i++){
        String s = itemsBanned.get(i);
        listOfItemsBanned = listOfItemsBanned + "," + s; //reset the string to the new list
    }
}

then you could check if the player is using a banned item, and cancel the event if they do, an example if they're using a banned block, like sand or TnT would be:

@EventHandler
public void playerInteract(PlayerInteractEvent e){
    if(e.getAction.equals(Action.RIGHT_CLICK_AIR) || e.getAction.equals(Action.RIGHT_CLICK_BLOCK){
        //the user has right-clicked
        Material m = e.getItemInHand().getType(); //get the item in the user's hand
        if(m != null){ //check that it's not null
            if(listOfItemsBanned.contains(m.getName()){ //if listOfItemsBanned has the name of the item in the player's hand in it
                e.setCanceled(true); //cancel the block place
            }
        }
    }
}
Jojodmo
  • 23,357
  • 13
  • 65
  • 107
0

Imports:

import java.util.Arrays;
import java.util.List;

Code:

public static void main(String args[]) {
    String[] listOfItemsBanned = { "TNT", "EnderPearl", "Sand" }; // ArrayList
                                                                    // of
                                                                    // banned
                                                                    // items
    String output = ""; // Creates output String
    for (int i = 0; i < listOfItemsBanned.length; i++) { // Loops through
                                                            // all items in
                                                            // the ArrayList
        output += listOfItemsBanned[i]; // Adds item to String
        if (i != listOfItemsBanned.length - 1) { // If it is not the last
                                                    // item in the ArrayList
                                                    // add ", "
            output += ", ";
        }
    }
    System.out.println(output); // Output String
}

Output:

TNT, EnderPearl, Sand
Johann
  • 187
  • 1
  • 4
  • 11