仅删除字符串ArrayList中的最后一个单词

时间:2017-10-16 09:31:36

标签: java arraylist

我试图删除字符串Arraylist中的最后一个单词。

public static void main(String[] args) {
    ArrayList<String> list = new ArrayList<String>();
    list.add("8.4 8.4"); list.add("6.5 8.0"); list.add("5.0 6.3 10.0");
    ArrayList<String> restlist = new ArrayList<String>();
    ArrayList<String> lastlist = new ArrayList<String>();
    for(String str : list) {
        String everything = str.trim();
        String[] words = everything.split(" ");
        String lastWord = words[words.length - 1];
        String restWords = everything.replace(lastWord, "");
        lastlist.add(lastWord);
        restlist.add(restWords);
    }
    System.out.println(restlist);
    System.out.println(lastlist);
}

如果我运行我的代码,则打印出来:

[ , 6.5 , 5.0 6.3 ]
[8.4, 8.0, 10.0]

问题是它还删除了第一个8.4,我只想删除最后一个。

4 个答案:

答案 0 :(得分:3)

因为你想要分开其余的最后一个单词,可能使用&#34; lastIndex()&#34;和&#34; substring()&#34;方法是一种更好的方法:

for(String str : list) {
    String everything = str.trim();
    int lastPos = everything.lastIndexOf(' ');
    if(lastPos < 0){
       continue; //check if there is a space char
    }
    String lastWord = everything.substring(lastPos + 1);
    String restWords = everything.substring(0,lastPos)
    lastlist.add(lastWord);
    restlist.add(restWords);
}

答案 1 :(得分:1)

将函数移除到字符串中删除最后一个单词的义务将是一件好事。请尝试以下代码:

private static ArrayList<String> lastlist = new ArrayList<String>();

public static void main(String[] args) {
    ArrayList<String> list = new ArrayList<String>();
    list.add("8.4 8.4"); list.add("6.5 8.0"); list.add("5.0 6.3 10.0");
    ArrayList<String> restlist = new ArrayList<String>();
    for(String str : list) {
        String restWords = cutOffLastWord(str);
        if (restWords.trim().length()>0) {
            restlist.add(restWords);
        }
    }
    System.out.println(restlist);
    System.out.println(lastlist);
}

private static String cutOffLastWord(String inputText) {
    String outputText = "";
    String[] words = inputText.split("\\s+");
    if (words.length > 1) {
        for (int i=0; i<words.length-1; i++) {
            if (i>0) outputText += " ";
            outputText += words[i];
        }
    }
    lastlist.add(words[words.length-1]);
    System.out.println(words[words.length-1] + " removed from " + inputText);

    return outputText;
}

}

这将是输出:

<8.4> 8.4从8.4 8.4中删除

8.0从6.5 8.0中删除

10.0从5.0 6.3 10.0中删除

[8.4,6.5,5.0 6.3]

[8.4,8.0,10.0]

其他的事情是只有一个单词时会发生什么?正如我在其他答案中看到的那样,它将失败。

答案 2 :(得分:0)

你可以试试这个:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    ArrayList<String> list = new ArrayList<String>();
    list.add("8.4 8.4"); list.add("6.5 8.0"); list.add("5.0 6.3 10.0");
    ArrayList<String> restlist = new ArrayList<String>();
    ArrayList<String> lastlist = new ArrayList<String>();
    for(String str : list) {
        String everything = str.trim();
        String[] words = everything.split(" ");
        String lastWord = words[words.length - 1];
        String restWords = everything.substring(0,everything.length() - lastWord.length());
        lastlist.add(lastWord);
        restlist.add(restWords);
    }
    System.out.println(restlist);
    System.out.println(lastlist);
}

答案 3 :(得分:0)

或者你可以先创建一个表;然后删除最后一列。

   public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
        list.add("8.4 8.4");
        list.add("6.5 8.0");
        list.add("5.0 6.3 10.0");
        List<List<String>> split = new ArrayList<List<String>>();
        for (String str : list) {
            String[] strSp = str.split(" ");
            List<String> row = new ArrayList<>();
            split.add(row);
            for (int i = 0; i < strSp.length - 1; i++) {
                row.add(strSp[i]);
            }
        }
        System.out.println("without last column=> " + split);
}
public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("8.4 8.4");
        list.add("6.5 8.0");
        list.add("5.0 6.3 10.0");
        List<List<String>> split = new ArrayList<List<String>>();
        for (String str : list) {
            String[] strSp = str.split(" ");
            List<String> row = new ArrayList<>();
            split.add(row);
            Collections.addAll(row, strSp);
        }
        System.out.println("all=> " + split);
        System.out.println();

        System.out.println("without last column=> ");
        for (List<String> strings : split) {
            for (int i = 0; i < strings.size() - 1; i++) {
                System.out.print(strings.get(i) + " ");
            }
            System.out.println();
        }

        System.out.println();
        System.out.print("last columns=> ");
        for (List<String> strings : split) {
            System.out.print(strings.get(strings.size() - 1) + " ");
        }

    }