How to write a for loop that skips elements if they are equal to the next index in an array list?

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

How to write a for loop that skips elements if they are equal to the next index in an array list?

问题

我有一个包含用户收集的喜欢事物列表的数组列表。例如,氛围、开放区域、宽敞等。

现在我从我的 Firestore 数据库中获取这些值,并将它们保存在 arrayList<String> 中。如果有多个用户表示喜欢某个氛围,我想在我的应用程序评论页面中显示他们喜欢的不同事物,而不是一遍又一遍地显示相同的事物。

如何编写一个循环,以跳过相等的元素,并使用彼此不相等的值初始化我的 TextView?

我已经使第一个 TextView 和第二个 TextView 成为唯一值,但是如何使第三个 TextView 也从我的 ArrayList 中获得唯一值?

这是我目前的做法:

private ArrayList<String> allLikes;
for (int i = 0; i < allLikes.size() - 1; i++) {
    if (!allLikes.get(i).equals(allLikes.get(i + 1))) {
        liked1.setText(allLikes.get(i));
        liked2.setText(allLikes.get(i + 1));
        // liked3.setText(allLikes.get(i + 2)); // 这一行导致错误。
        Toast.makeText(DetailsActivity.this, "不相等", Toast.LENGTH_SHORT).show();
    } else {
        liked1.setText(allLikes.get(0));
        Toast.makeText(DetailsActivity.this, "相等", Toast.LENGTH_SHORT).show();
    }
}
英文:

I have an array list containing a list of liked things collected from users. for example, atmosphere, open area, spacious, etc.

now I am getting these values from my firestore DB and saving them in the arrayList&lt;String&gt;. if more than one user said they liked the atmosphere I want to display different things they liked in my app's reviews page and not the same things over and over.

how to write a for loop that will skip elements if they are equal and initialize my TextView with values that are not equal to each other?

I made things work for the first TextView and the second textView to be unique but how will I make the third textView also a unique value from my ArrayList?

heres what i've done so far:

private ArrayList&lt;String&gt; allLikes;
for(int i = 0; i&lt;allLikes.size()-1; i++){
     if(allLikes.get(i) != allLikes.get(i+1)){
        liked1.setText(allLikes.get(i));
        liked2.setText(allLikes.get(i+1));
        // liked3.setText(allLikes.get(i+2)); //this line is causing an error.
        Toast.makeText(DetailsActivity.this, &quot;NOT EQUAL&quot;, Toast.LENGTH_SHORT).show();
      }else{
        liked1.setText(allLikes.get(0));
        Toast.makeText(DetailsActivity.this, &quot;EQUAL&quot;, Toast.LENGTH_SHORT).show();
      }
}      

答案1

得分: 2

private ArrayList<String> allLikes = null;
// 填充你的列表
Set<String> set = new HashSet<>(allLikes);
int i = 0;
for (Iterator<String> it = set.iterator(); it.hasNext(); ) {
    String s = it.next();
    i++;
    if (i == 1)
        textView1.setText(s);
    else if (i == 2)
        textView2.setText(s);
    else if (i == 3)
        textView3.setText(s);
    else
        break;
}
英文:

You may use this pattern:

private ArrayList&lt;String&gt; allLikes = null;
//fill your list
Set&lt;String&gt; set = new HashSet&lt;&gt;(allLikes);
int i=0;
for (Iterator&lt;String&gt; it = set.iterator(); it.hasNext(); ) {
    String s= it.next();
    i++;
    if (i==1)
	    textView1.setText(s);
    else if (i==2)
	    textView2.setText(s);
    else if (i==3)
	    textView3.setText(s);
    else
	    break;
}

When you convert a List to a Set, all the similar entries are removed.

huangapple
  • 本文由 发表于 2020年9月2日 12:44:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63698840.html
匿名

发表评论

匿名网友

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

确定