【问题标题】:how can I store different value in different ArrayList in android如何在android中的不同ArrayList中存储不同的值
【发布时间】:2017-06-08 10:54:51
【问题描述】:

我正在使用类似这样的字符串值字符串结果"aa,bb,cc,dd,ee"

并且已经将其拆分为aa bb cc dd ee

qrList = result.getContents().toString().split("\\,");
List<String> resultToString= new ArrayList(Arrays.asList(qrList));

然后我创建了四个 ArrayList。

 ArrayList<String> strA = new ArrayList();
 ArrayList<String> strB = new ArrayList();
 ArrayList<String> strCD = new ArrayList();
 ArrayList<String> strE = new ArrayList();

当我使用下面的代码将字符串存储到每个新的ArrayList 中时。

for(int count=0; count<resultToString.size(); count++){
    //separate string to array list
    if(count%5==0){
        strA.add(resultToString.get(count));
    }else if(count%5==1){
        strB.add(resultToString.get(count));
    }else if(count%5==2||count%5==3){
        strCD.add(resultToString.get(count));
    }else if(count==4){
        strE.add(resultToString.get(count));
    }

正确的结果是

  • strA 存储 aa
  • strB 存储 bb
  • strCD 存储 cc 和 dd
  • strE 存储 ee

它不起作用,因为我只获得了0 的索引值(strA 存储了 aa)。 我应该怎么做才能改进我的代码?

【问题讨论】:

  • 如果你format your code first就太好了
  • 你分工好吗?我手动尝试过,例如 "aa,bb,cc,dd,ee".toString().split("\\,");然后你的代码就可以正常工作了。
  • 你说的不可能。 count==4 对于您的给定输入必须为真
  • 对于 motis10,拆分方法工作正常。

标签: java android string arraylist


【解决方案1】:

只需使用startsWith 并将适当的值添加到列表中

    for(int count=0; count<resultToString.size(); count++){
        //separate string to array list
        String s = resultToString.get(count);

        if(s.startsWith("a")){
            strA.add(s);
        }else if(s.startsWith("b")){
            strB.add(s);
        }else if(s.startsWith("c")||s.startsWith("d")){
            strCD.add(s);
        }else if(s.startsWith("e")){
            strE.add(s);
        }
    }

【讨论】:

  • 请解释为什么他的代码不起作用,而不是提供替代解决方案。
  • @earthw0rmjim by OP What should I do to improve my code
  • 我认为字符串是任意的
  • @earthw0rmjim 我找不到任何问题,所以我去测试并再次没有问题,这一定是某个地方出了问题,尽管我建议稍微清楚一点
【解决方案2】:

模数运算符描述方程的其余部分。让我们展示和解释一个例子:

正如in this documentation 解释的那样,在两个值之间执行取模只会显示余数。例如8 % 3 == 2 因为 8 除以 3 余数为 2。

您的代码未显示您想要的结果的原因是由于使用了模数运算符。关于改进您的代码,我可以说您想要的只是使用数组列表索引来添加来自resultToString 的字符串 数组列表。

因此,我将按以下方式修改您的循环:

for(int count=0; count < resultToString.size(); count++) {
  //separate string to array list
  if (count == 0){
     strA.add(resultToString.get(count));
  } else if (count == 1){
     strB.add(resultToString.get(count));
  } else if (count == 2 || count == 3){
     strCD.add(resultToString.get(count));
  } else if (count == 4){
     strE.add(resultToString.get(count));
  }
}

另一种方法是直接在生成的 String[] 上循环,并在 for 循环中使用该相对计数器,例如

String[] qrList = results.getContent().toString().split("\\,");

for(int count = 0; count < qrList.length; count++) {
  //each index represents the split string e.g
  // [0] == "aa"
  // [1] == "bb"
  // [2] == "cc"
  // [3] == "dd"
  // [4] == "ee"
  if (count == 0){
     strA.add(qrList[count]);
  } else if (count == 1){
     strB.add(qrList[count]);
  } else if (count == 2 || count == 3){
     strCD.add(qrList[count]);
  } else if (count == 4){
     strE.add(qrList[count]);
  }
}

【讨论】:

  • 致Koshux,当我使用你的代码进行测试时,它无法在ArrayList中存储任何值。
猜你喜欢
  • 1970-01-01
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多