繁体   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