如何使String包含ArrayList中的每个项目? (JAVA)

时间:2014-03-12 00:59:00

标签: java arraylist bukkit

我有一个名为 listOfItemsBanned 的String,以及一个名为 itemsBanned 的ArrayList。 让我们说ArrayList itemsBanned 包含3件事: TNT EnderPearl Sand 。 我希望String成为" TNT,EnderPearl,Sand"。 当从 itemsBanned 中删除某些内容时,它也会将其从字符串中删除。

所以.. 我希望能够将每个项目包含在ArrayList中并将其放入一个字符串中,每个项目用逗号分隔。

3 个答案:

答案 0 :(得分:2)

您只需要一行:

String listOfItemsBanned = itemsBanned.toString().replaceAll("^.|.$", "");
toString()的{​​{1}}生成了元素的CSV,但包含在List[中。对]的调用会删除第一个和最后一个字符,只留下元素。

答案 1 :(得分:1)

你可以这样做:

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
}

现在,如果您想获取字符串listOfItemsBanned中禁止的所有项目,您可以这样做:

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];
}

您现在可以使用itmBanned执行任何操作,例如将其转换为Material

Material m = Material.getMaterial(itmBanned);

所以,你可以为remove方法做这样的事情:

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
    }
}

并添加:

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
    }
}

然后你可以检查玩家是否正在使用被禁止的物品,并且如果他们这样做就取消该事件,例如如果他们使用禁止的块,如沙子或TnT将是:

@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
            }
        }
    }
}

答案 2 :(得分:0)

<强>进口:

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

<强>代码:

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
}

<强>输出:

TNT, EnderPearl, Sand