이 String result와 같은 문자열 값으로 작업하고 있습니다 "aa,bb,cc,dd,ee"
.
그리고 이미로 분할 aa bb cc dd ee
과
qrList = result.getContents().toString().split("\\,");
List<String> resultToString= new ArrayList(Arrays.asList(qrList));
그런 다음 4 개의 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));
}
올바른 결과는 다음과 같습니다.
0
(strA stored aa) 에서만 인덱스 값을 가져 오기 때문에 작동하지 않습니다 . 코드를 개선하려면 어떻게해야합니까?
모듈러스 연산자는 방정식의 나머지 부분을 설명합니다. 예를 보여주고 설명하겠습니다.
이 문서에 설명 된 대로 두 값 사이의 계수를 수행하면 나머지 값만 표시됩니다. 예를 들어 8 % 3 == 2
8을 3으로 나누면 나머지는 2가됩니다.
코드가 원하는 결과를 표시하지 않는 이유는 모듈러스 연산자를 사용하기 때문입니다. 코드 개선과 관련하여 원하는 것은 Array List 인덱스를 사용하여 Array List의 문자열을 추가하는 것 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]);
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다