拆分arraylist项目值并将每个项目放在不同的数组中

时间:2017-05-18 11:13:15

标签: android string arraylist

我有一个ArrayList值,每个项目都有"="符号,我想split符号,add每个ArrayList。我尝试了很多方法来分裂但不是    正是按照我的要求工作。

Below my ArrayList value
[COOL 35x1=AED 35, COOL PRIME 40x1=AED 40]

I exactly want the output
[COOL 35x1,COOL PRIME 40x1]
[AED 35,AED 40]

3 个答案:

答案 0 :(得分:0)

未经测试,但应该有效。

private ArrayList<String> getNewList(ArrayList<String> arrayList){

    ArrayList<String> newList = new ArrayList<>();

    for (int i = 0; i < arrayList.size(); i++) {
        newList.add(arrayList.get(i).replace('=', ' '));
    }

    return newList;
}

答案 1 :(得分:0)

效果很好。

public List<String> list1 = new ArrayList<String>();
public List<String> list2 = new ArrayList<String>();

public String getList(ArrayList<String> yourList)
{
    for(int i=0;i<yourList.size();i++)
    {
        String[] parts = yourList.get(i).split("="); 
        list1.add(parts[0]);
        list2.add(parts[1]);
    }
    return list1 + "\n" + list2;
}

答案 2 :(得分:0)

以下是工作代码:

// Original ArrayList
List<String> listOriginal = new ArrayList<String>();
listOriginal.add("COOL 35x1=AED 35");
listOriginal.add("COOL PRIME 40x1=AED 40");

// New ArrayList
List<String> listNew = new ArrayList<String>();

// Looping through all items in Original ArrayList
for(int i = 0; i < listOriginal.size(); i++)
{
    String item = listOriginal.get(i); // Ex: item = "COOL 35x1=AED 35"
    String[] parts = item.split("=");  // parts = ["COOL 35x1", "AED 35"]

    listOriginal.set(i, parts[0]);  // "COOL 35x1=AED 35" replaced by "COOL 35x1" 
    listNew.add(parts[1]);  // Add "AED 35"
}

检查结果:

for(int i = 0; i < listOriginal.size(); i++)
    Log.d("Original ArrayList", "Index " + i + ": " + listOriginal.get(i));

for(int i = 0; i < listNew.size(); i++)
    Log.d("New ArrayList", "Index " + i + ": " + listNew.get(i));

<强>输出:

D/Original ArrayList: Index 0: COOL 35x1
D/Original ArrayList: Index 1: COOL PRIME 40x1
D/New ArrayList: Index 0: AED 35
D/New ArrayList: Index 1: AED 40