簡體   English   中英

將Char的ArrayList轉換為另一個Int的ArrayList

[英]Converting ArrayList of Char into another ArrayList of Int

在過去的一個小時里,這一直困擾着我,我覺得自己一無所獲。

我有一個ArrayList字符,我需要將數字字符轉換為整數,並將其放入新數組中。 所以:

ArrayList<Character> charList contains [5, ,1,5, ,7, ,1,1]

我想使用當前的charList並將內容放入Integer類型的新ArrayList中,該數組顯然不包含空格。

ArrayList<Integer> intList contains [5, 15, 7, 11]

現在任何幫助將不勝感激。

首先,使用charList中的字符創建一個String ,然后在該空格處拆分該字符串,最后將每個標記解析為一個int ,如下所示:

char[] chars = new char[charList.size()];
charList.toArray(chars);
String s = new String(chars);
String[] tok = s.split(" ");
ArrayList<Integer> res = new ArrayList<Integer>();
for (String t : tok) {
    res.add(Integer.parseInt(t));
}

使用Java8,您可以通過以下方式進行轉換:

String str = charList.stream().map(Object::toString).collect(Collectors.joining());
ArrayList<Integer> intList = Arrays.asList(str.split(" ")).stream().map(Integer::parseInt)
                                   .collect(Collectors.toCollection(ArrayList::new));

首先,我們將以','分隔的字符收集到一個字符串中,然后將其拆分為一個數字列表(如Strings),然后從過濾器流中將每個String解析為一個int並將它們收集到一個新列表中。

for(Character ch : charList) {
    int x = Character.digit(ch, 10);
    if (x != -1) {
         intList.add(x);
    }
 }

是否要將每個單獨的字符更改為對應的整數?

如果是這樣,

Integer.parseInt(String)

將字符串轉換為int和

Character.toString(char)

將字符轉換為字符串

如果要將多個字符轉換為單個整數,可以分別轉換所有字符,然后執行類似的操作

int tens = Integer.parseInt(Character.toString(charList.get(i)));
int ones = Integer.parseInt((Character.toString(charList.get(i+1)));
int value = tens * 10 + ones;
intList.add(i, value);

遍歷char數組(如果看到)

  • 一個數字:將其附加到String(builder)
  • 一個空格:解析當前的String(builder)

這是一個簡單的實現:

StringBuilder sb = new StringBuilder();
List<Character> charList = getData();  // get Values from somewhere
List<Integer> intList = new ArrayList<Integer>();

for (char c:charList) {
  if (c != ' ') {
     sb.append(c);
  } else {
     intList.add(Integer.parseInt(sb.toString());
     sb = new StringBuilder();
  }
}
public static List<Integer> getIntList(List<Character> cs) {
  StringBuilder buf = new StringBuilder();
  List<Integer> is = new ArrayList<Integer>();
  for (Character c : cs) {
    if (c.equals(' ')) {
      is.add(Integer.parseInt(buf.toString()));
      buf = new StringBuilder();
    } else {
      buf.append(String.valueOf(c));
    }
  }
  is.add(Integer.parseInt(buf.toString()));
  return is;
}


List<Character> cs = Arrays.asList('5', ' ', '1', '5', ' ', '7', ' ', '1', '1');
List<Integer> is = getIntList(cs); // => [5, 15, 7, 11]

遍歷字符列表,將它們解析為整數,然后添加到整數列表中。 您應該注意NumberFormatException 這是為您准備的完整代碼:

Character[] chars = new Character[]{'5',' ','1','5',' ','7',' ','1','1'};
List<Character> charList = Arrays.asList(chars);
ArrayList<Integer> intList = new ArrayList<Integer>();

for(Character ch : charList)
{
    try
    { intList.add(Integer.parseInt(ch + "")); }
    catch(NumberFormatException e){}
}

如果您已經填充了字符列表,則可以跳過上面代碼的前兩行。

這里是另一個....沒有parseInt

        char[] charList = "5 15 7 11 1234 34 55".Trim().ToCharArray();
        List<int> intList = new List<int>();
        int n = 0;
        for(int i=0; i<charList.Length; i++)
        {
            if (charList[i] == ' ')
            {
                intList.Add(n);
                n = 0;
            }
            else
            {
                n = n * 10;
                int k = (int)(charList[i] - '0');
                n += k;
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM