对手机号码只使用国家代码进行排序。

huangapple go评论96阅读模式
英文:

Sorting the mobile number with countrycode alone

问题

我有一个联系人号码列表界面我将这些联系人存储在SQLite数据库中并从数据库中显示出来

我需要筛选仅包含国家代码的手机号码当用户以前缀0和无前缀0的方式保存相同的号码时例如+919876543211098765432119876543211我需要仅显示用户保存的带有国家代码的手机号码

如果用户仅保存了手机号码则需要单独显示该号码或者如果带有前缀0”,则需要单独显示该号码

```java
for(int i=0; i < arrayListContact.size()-1; i++){
   if(arrayListContact.get(i).getContact().startsWith("+")){
      plusArrayList.add(arrayListContact.get(i));
   } else if(arrayListContact.get(i).getContact().startsWith("0")){
      zeroArrayList.add(arrayListContact.get(i));
   } else {
      mobileArrayList.add(arrayListContact.get(i));
   }
}

tempArrayList.addAll(plusArrayList);

for(int i=0; i < tempArrayList.size()-1; i++){
  String mobile = tempArrayList.get(i).getContact();
  String mobileWithout = getMobileNumberAlone(mobile);

for (int j = 0; j < zeroArrayList.size() - 1; j++) {
  if(!zeroArrayList.get(j).getContact().equals("0" + mobileWithout)){
     tempArrayList.add(zeroArrayList.get(j));
  }
}
}

我将tempArrayList传递给适配器(Adapter)。但在下面的循环中,它进入了无限循环。


<details>
<summary>英文:</summary>
I am having contact numbers listing screen. I stored those contacts in SQLite database and showing from that. 
I need to filter mobile number which contains country code alone. When user saved same number with prefix &quot;0&quot; &amp; without prefix &quot;0&quot;. For example - +919876543211, 09876543211, 9876543211. I need to show only mobile number with country code when user saved like the example. 
If user saved only mobile number need to show that alone or with prefix &quot;0&quot; means need to show that alone. 

for(int i=0; i < arrayListContact.size()-1; i++){
if(arrayListContact.get(i).getContact().startsWith("+")){
plusArrayList.add(arrayListContact.get(i));
} else if(arrayListContact.get(i).getContact().startsWith("0")){
zeroArrayList.add(arrayListContact.get(i));
} else {
mobileArrayList.add(arrayListContact.get(i));
}
}

tempArrayList.addAll(plusArrayList);

for(int i=0; i < tempArrayList.size()-1; i++){
String mobile = tempArrayList.get(i).getContact();
String mobileWithout = getMobileNumberAlone(mobile);

for (int j = 0; j < zeroArrayList.size() - 1; j++) {
if(!zeroArrayList.get(j).getContact().equals("0" + mobileWithout)){
tempArrayList.add(zeroArrayList.get(j));
}
}
}


I am passing tempArrayList to Adapter. But in below loop it is taking endless loop.
</details>
# 答案1
**得分**: 0
在你的第二个循环中,你正在向循环数组中添加元素,因此循环永远不会结束:
```java
for (int i=0; i < tempArrayList.size()-1; i++)  // 总是使用最新的数组大小
{
String mobile = tempArrayList.get(i).getContact();
String mobileWithout = getMobileNumberAlone(mobile);
for (int j=0; j < zeroArrayList.size() - 1; j++) 
{
if (!zeroArrayList.get(j).getContact().equals("0" + mobileWithout))
{ 
tempArrayList.add(zeroArrayList.get(j));  // 添加到数组,造成无限循环
}
}
}

尝试使用以下方法代替:

int tmplen = tempArrayList.size();  // 初始大小,永远不会改变
for (int i=0; i < tmplen-1; i++)
......
英文:

In your second loop, you're adding to the loop array so the loop never ends:

for(int i=0; i &lt; tempArrayList.size()-1; i++)  // always uses latest array size
{
String mobile = tempArrayList.get(i).getContact();
String mobileWithout = getMobileNumberAlone(mobile);
for (int j = 0; j &lt; zeroArrayList.size() - 1; j++) 
{
if(!zeroArrayList.get(j).getContact().equals(&quot;0&quot; + mobileWithout))
{ tempArrayList.add(zeroArrayList.get(j)); }  // adding to array, infinite loop
}
}

Try this instead:

int tmplen = tempArrayList.size();  // starting size, never changes
for(int i=0; i &lt; tmplen-1; i++)
......

huangapple
  • 本文由 发表于 2020年9月8日 01:23:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63781878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定