簡體   English   中英

如果不滿足條件,我如何跳至for循環中的下一個對象

[英]How do I skip to the next object in a for loop if a condition isn't met

我在這里有一個很大的循環,它位於一種方法中,該方法向我的地址簿中的所有人員模仿大量短信。 此循環(成功)檢查一封電子郵件,其中包含在發起召回后發送的所有答復。 我的問題是它將按1的順序檢查答復,並且直到收到下一行答復時才檢查其他答復。

如何更改此循環以檢查循環中的所有聯系人,直到所有聯系人都已答復或應用程序終止為止?

Map<String, Integer> replies = new HashMap<String, Integer>();

for (int j = 0; j < v.size(); j++){
    replies.put(((Member)v.elementAt(j)).getPhoneNo(), j); //Save a reference to the phone number AND its index in "v".
}
String host = "pop.gmail.com";
String mailStoreType = "pop3";
String username = "********@gmail.com";
String password = "********";
String[] phoneToCheck = new String[v.size()];
int phoneCheck = phoneToCheck.length;

while(phoneCheck > 0){

    MailReader.check(host, mailStoreType, username, password); 

    for(int i = 0; i < v.size(); i++) {
        con = (Member) v.elementAt(i);
        phoneToCheck[i] = con.getPhoneNo();

        if (replies.containsKey(phoneToCheck)) {
            newFrame.addNotify();
            System.out.println("IT WORKED");

            model.setValueAt(MailReader.getReply(phoneToCheck[i]), 
                                                 replies.get(phoneToCheck[i]), 3);
            model.fireTableDataChanged();

            replies.remove(phoneToCheck);

            phoneCheck --;
        }
    }               
}

如果我了解得很好,那么您的操作是這樣的:

  1. 對於地址簿中的每個聯系人,請執行以下操作:
  2. 繼續檢查郵件,直到沒有收到我正在檢查的聯系人的回復。 按1的順序檢查答復

但是您想要相反的方法:

  1. 繼續檢查郵件,直到收到答復:
  2. 檢查誰發送了回復並更新表格。

您可以通過一個簡單的while循環和一個查找映射來實現:

Map<String, Integer> replies = new HashMap<String, Integer>();
for (int j = 0; j < v.size(); j++)
    replies.put(((Member)v.getElementAt(j)).getPhoneNo(), j); //Save a reference to the phone number AND its index in "v".

MailReader mR2 = new MailReader();
String host = "pop.gmail.com";
String mailStoreType = "pop3";
String username = "********@gmail.com";
String password = "********";

while(!replies.isEmpty()){
    MailReader.check(host, mailStoreType, username, password); //Is this correct? Why are you using a static method? What's the purpose of mR2?
    String phoneToCheck = MailReader.getPhone(); //You have to implement this if it's not present
    if (replies.containsKey(phoneToCheck)) {
        newFrame.addNotify();
        System.out.println("IT WORKED");

        model.setValueAt(MailReader.getReply(phoneToCheck), replies.get(phoneToCheck), 3);
        model.fireTableDataChanged();
        replies.remove(phoneToCheck);
    }
}

編輯

如果您的v對象與您的表結構不同,則可以直接從表中獲取電話號碼(假設已填充):

for(int i = 0; i < model.getRowCount(); i++){
    replies.put((String)model.getValueAt(i,3), i); //Assuming 3 is phone number column
}

編輯2

也許我的解釋不夠好。

在修改中,您根據需要更改了Map結構,但使它毫無用處。

映射是用於保存鍵值對的結構。 在您的情況下(我的建議)用於保存String和Integer。 字符串代表電話號碼,而整數代表其在表格中的位置。 因此,如果我們有一個像這樣的表:

------------+---------------
|     1     |    1234567   |
------------+---------------
|     2     |    7654321   |
------------+---------------

我們想要一個包含值的Map:

Map = {[12345678, 1],[7654321, 2]}

這樣,當我們調用Map.get("7654321")"7654321"是鍵)時,我們將獲得2 ,這是電話號碼表中的行。

現在您的代碼:

Map<String, Integer> replies = new HashMap<String, Integer>();

//First thing first, fill the map with the phone number and row number 
//as described above.
for (int j = 0; j < v.size(); j++)
    replies.put(((Member)v.elementAt(j)).getPhoneNo(), j); 
    //                                       ^         ^   
    //                                phone number     row

    //[omissed for brevity]

    //This is wrong, we want to check if the map has still phone numbers in it
    //while(phoneCheck >0){
    while(!replies.isEmpty()){ // This means "while replies is not(!) empty"

        //I don't know how MailReader is implemented, but I assume that this call
        //checks if a new mail has arrived.
        MailReader.check(host, mailStoreType, username, password);

        //Now comes the IMPORTANT part. You have to retrieve the phone number
        //from the INCOMING MAIL! So you can understand who replied.

        //Again, I don't know how MailReader is implemented, I assumed that a getPhone()
        //method is available.
        //Is such method doesn't exist you have to CREATE IT. It must retrieve the 
        //phone number contained in the mail.

        String phoneToCheck = MailReader.getPhone(); //Get the phone from the mail.

        //This is completely useless.
        /** 
        for(int i = 0; i<v.size(); i++) {
            con = (Member) v.elementAt(i);
            phoneToCheck[i] = con.getPhoneNo();
        **/
        if (replies.containsKey(phoneToCheck)) {
            newFrame.addNotify();
            System.out.println("IT WORKED");

            //Where the hell does i came from?? It belongs to the for loop you FINISHED earlier!!
            //model.setValueAt(MailReader.getReply(phoneToCheck[i]), replies.get(phoneToCheck[i]), 3);
            model.setValueAt(MailReader.getReply(phoneToCheck), replies.get(phoneToCheck), 3);
            model.fireTableDataChanged();
            replies.remove(phoneToCheck);

            //phoneCheck --; Useless
        }
        }

    }

}

我可以找到的使用數組的唯一含義是,郵件一次包含多個電話號碼。 在這種情況下,您必須以這種方式修改代碼:

String phonesToCheck[] = MailReader.getPhones(); //Get the phones from the mail. BEWARE!! This is an array!
for(String phoneToCheck: phonesToCheck){
    if (replies.containsKey(phoneToCheck)) {
        newFrame.addNotify();
        System.out.println("IT WORKED");

        model.setValueAt(MailReader.getReply(phoneToCheck), replies.get(phoneToCheck), 3);
        model.fireTableDataChanged();
        replies.remove(phoneToCheck);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM