简体   繁体   English

我如何在Android中的不同ArrayList中存储不同的值

[英]how can I store different value in different ArrayList in android

I'm working with string value like this String result "aa,bb,cc,dd,ee" . 我正在使用类似字符串结果"aa,bb,cc,dd,ee"字符串值。

And already split it into aa bb cc dd ee with 并已将其拆分为aa bb cc dd ee

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

Then I create four ArrayLists. 然后,我创建四个ArrayList。

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

When I use this code below to store string into each 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));
    }

The correct result would be 正确的结果是

  • strA stored aa strA存储了
  • strB stored bb strB存储bb
  • strCD stored cc and dd strCD存储的cc和dd
  • strE stored ee 存储的ee

It doesn't work because I only get the index value at 0 (strA stored aa). 它不起作用,因为我仅将索引值设为0 (strA存储为aa)。 What should I do to improve my code? 我应该怎么做才能改善我的代码?

Simply use startsWith and add appropriate values to list 只需使用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);
        }
    }

Modulus operator describes the remainder of your equation. 模运算符描述方程的其余部分。 Let us show and explain an example: 让我们展示并解释一个示例:

As explained in this documentation , performing modulus between two values will only show you the remainder value. 本文档中所述 ,在两个值之间执行模数只会显示剩余值。 For eg 8 % 3 == 2 because 8 divided by 3 leaves a remainder of 2. 例如,对于8 % 3 == 2因为8除以3剩下2。

The reason your code is not showing your desired results is due to the usage of your modulus operator. 代码未显示所需结果的原因是由于使用了模运算符。 Regarding improving your code, I can tell that all you wanted is to use the Array List index so as to add the string from the resultToString Array List. 关于改进代码,我可以告诉您,您所需resultToString就是使用Array List索引,以便从resultToString Array List中添加字符串。

Therefore I would modify your loop in the following manner: 因此,我将以以下方式修改您的循环:

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));
  }
}

An alternative is to loop directly on the resulting String[] and use that relative counter in the for loop for eg 一种替代方法是直接在结果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]);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM