2017-10-16 118 views
1

我試圖刪除字符串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和我只是想去掉最後一個。

回答

3

因爲要休息的最後一個字分開,也許用「lastIndex的()」和「子串()」方法是一種更好的方式:

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); 
} 
+0

謝謝,這只是它的工作原理! – Barkork

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); 
} 
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) + " "); 
     } 

    } 
0

這將是很好的點移動到一個函數的字符串刪除最後一個單詞的責任。請嘗試驗證碼

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.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]

其他的事情是什麼時候發生只有一個詞?正如我在其他答案中看到的那樣,它會失敗。

相關問題