我有一个String ArrayList,其中包含: [3 9 12,6 6 12,36 12 48,1 1 2,1 2 3,8 10 18,5 12 17,75 5 82,32 2 34,92 15 107,99 6 105]
每个String包含三个整数。如何分隔由空格分隔的整数,并将它们添加到三个不同的ArrayLists中,使它们看起来像这样:
list1:[3,6,36,1,1,8,5,77,32,92,99]
list2:[9,6,12,1,2,10,12,5,2,15,6]
list3:[12,12,48,2,3,18,17,82,34,107,105]
三个结果列表仍然是String类型。任何帮助将不胜感激。
答案 0 :(得分:2)
您可以获取String ArrayList 3 9 12
的单个元素,然后使用split
ArrayList<String> arrList1 = new ArrayList<String>(); // for the 1st num in each pair of 3
ArrayList<String> arrList2 = new ArrayList<String>(); // for the 2nd num in each pair of 3
ArrayList<String> arrList2 = new ArrayList<String>(); // for the 3rd num in each pair of 3
// then loop this part below for however many elements are in the original ArrayList "list"
String str = list.get(0); // assuming index 0 was "3 9 12"
String[] arr = str.split(" ");
arrList1.add(arr[0]);
arrList2.add(arr[1]);
arrList3.add(arr[2]);
答案 1 :(得分:0)
迭代您的ArrayList,然后使用String.split()分隔您的值。
for(String values : yourArrayList){
String[] parts = values.split(" ");
youNewArrayList1.add(parts[0]);
youNewArrayList2.add(parts[1]);
youNewArrayList3.add(parts[2]);
}