Question

I have made a simple android demo program for binding words to a string and then using them for sending emails,So i want all them unique(No single word(email Id) should repeat in that String).I have tried as below,but i get repeated values when i press haedware backbutton and unchecking my contact List..

code

String str1 = "";
for (String bean : myAdapter.myId) {
    str1 = str1 + ","+ getFacebook(ConnectedActivity.this,Integer.parseInt(bean));
    System.out.println("========My facebookId:::::: " + str1);
}
if (str1.length() > 0) {
    if (str1.toUpperCase().contains(str1)) {
        System.out.println("contains duplicates::::::::::>>>>>>>>>>>>>");
    } else {
        str1 = str1.substring(1);
        MainActivity.showAlert(ConnectedActivity.this,"FACEBOOK SENDER", str1, "OK", "EMAIL");
    }
Was it helpful?

Solution

Following code will only keep emails thats only appear once. repeated ones will not appear at all.

String str1 = "";
ArrayList<String> all =  new ArrayList<String>();
for (String bean : myAdapter.myId) {
    String s = getFacebook(ConnectedActivity.this, Integer.parseInt(bean));
    all.add(s);
}

int i,j;
for (i=0;i<all.size();i++)
{
    String temp = all.get(i);
    boolean found = false;
    for (j=i+1;j<all.size();j++)
    {
        if(temp.equals(all.get(j)))
        {
            found = true;
            all.remove(j);
            j--;
        }
    }
    if (!found)
        str1 += "," + temp;
}
System.out.println(str1);

OTHER TIPS

for (int i = 0; i < myId.size(); i++) {

                        if(myId.get(i).equals(value[1]))
                        {
                            myId.remove(i);
                        }

                    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top