2015-05-22 34 views
0

嗨,大家好我目前正在嘗試將字符串轉換爲arrayList反向,然後刪除任何尾隨零 例如「001203」轉換爲(3,0.2,1,0,0 ),然後(3,0,2,1)java將字符串轉換爲int並刪除尾隨零

我的代碼是目前

public convert(String nums) { 
    List = new ArrayList<Integer>(); // create arraylist 

    for (int i = nums.length(); i >= 0; i--) { //convert to int 
     int j = Integer.parseInt(digits); 
     List .add(j); 

     for (Iterator<Integer> k = List .iterator(); k.hasNext();) { //remove trailing zeros 
      if (k.next().equals(0)) { 
       k.remove(); 
      } 
     } 

    } 

此代碼移除目前全部爲0,而不是尾隨零。意思是我的輸出是(3,2,1)而不是(3,0,2,1);

任何幫助將不勝感激提前 感謝

+2

在數組的末尾開始迭代。刪除零直到找到一個非零值,此時您應該打破循環 – CubeJockey

+1

「001203」如何轉換爲「3,0.2 ?, 1,0,0」這是一個錯字? – gtgaxiola

+0

嘗試編寫一個方法來修剪零像這樣: 查找第一個非0,這是新字符串的開始,第二個值是非0字符的最後一次出現,之後您可以使用String.subString(start,end) ;)那樣你會確保正確的回報。 你的方法是從字符串中刪除每個0。 – Fincio

回答

1

與方法的問題是,在你的內部循環中,無論它在列表中的位置如何,你都會刪除零。

有多種方法可以實現你想要的。下面是使用一個單一的一分法(不認爲扭轉你的列表供您字符串或集合操作):

String nums = "001203"; 

    List<Integer> list = new ArrayList<>(); 

    // a boolean flag to help check for leading zeroes 
    // set it to true first i.e. assume there are leading zeroes 
    boolean checkLeadingZeroes = true; 

    // Ignore leading zeroes during iteration, 
    // and append to list in reverse order 

    for (int i=0; i < nums.length(); i++){ 
     int n = Integer.parseInt(nums.charAt(i)+""); 

     // only check for leading zeroes if flag is true 

     if (checkLeadingZeroes){ 
      // If flag is set to false here, you've found the first non-zero 

      checkLeadingZeroes = (n == 0); 
     } 

     if (!checkLeadingZeroes) { 
      /* If flag is false, n is not a leading zero 
      * Add n to the beginning of your list (index 0) 
      */ 
      list.add(0, n); 
     } 
    } 

其他幾個選項:

  1. 更改你的內部循環,使您遍歷倒退並刪除零,直到找到非零值爲止

  2. 修剪所有前導零(例如,使用正則表達式操作或循環),然後向後循環以創建您的列表。

希望有所幫助。

0

這裏是你想在短期內做一個簡單的例子:

String example = "001203"; 
// Replacing all leading 0s with empty, converting to char array 
char[] chars = example.replaceAll("^0+", "").toCharArray(); 
// creating list of integers 
List<Integer> list = new ArrayList<Integer>(); 
// iterating chars 
for (char c: chars) { 
    // trying to convert to integer 
    try { 
     list.add(Integer.parseInt(Character.toString(c))); 
    } 
    catch (NumberFormatException nfe) { 
     // TODO handle non-numeric characters! 
    } 
} 
// reversing list 
Collections.reverse(list); 
// printing list 
System.out.println(list); 

輸出

[3, 0, 2, 1] 

注意

另一種選擇,而無需使用Collections.reverse,爲您char[]年底開始迭代:

for (int i = chars.length - 1; i > -1; i--) { 
    try { 
     list.add(Integer.parseInt(Character.toString(chars[i]))); 
... 
0

試試這個代碼:

public static void main(String[] args) { 
    String numStr = "001203"; 
    String revNumStr = new StringBuilder(numStr).reverse().toString(); 

    int lastZeroIndex = revNumStr.lastIndexOf('0'); 
    if(lastZeroIndex == revNumStr.length() - 1) { 
     String newStr = ""; 
     boolean done = false; 
     for(int i = (revNumStr.length() - 1) ; i >= 0 ; i --) { 

      if(revNumStr.charAt(i) != '0') { 
       done = true; 
      } 

      if(done) { 
       newStr += revNumStr.charAt(i); 
      } 
     } 

     revNumStr = new StringBuilder(newStr).reverse().toString(); 
    } 

    System.out.println("The revrese string is : " + revNumStr); 
}