如果不满足条件,如何跳转到for循环中的下一个对象

时间:2014-10-13 12:25:09

标签: java swing for-loop while-loop

我在这里有一个很大的循环,它位于一个方法中,它向我的地址簿中的所有人模仿大量文本消息。此循环(成功)检查包含在启动召回后发送的任何回复的电子邮件。我的问题是,它将按1到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 个答案:

答案 0 :(得分:0)

如果我表现得很好,你就是这样做的:

  1. 对于地址簿中的每个联系人,请执行以下操作:
  2. 继续检查邮件,直到我正在检查联系人的回复 还没到。 (按顺序1检查答复
  3. 但你想要反过来:

    1. 继续检查邮件,直至收到回复:
    2. 检查是谁发送了回复并更新了表格。
    3. 您可以使用简单的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是用于保存键值对的结构。在你的情况下(我的建议)用于保存字符串和整数。字符串表示电话号码,而整数表示其在表中的位置。所以,如果我们有一个像这样的表:

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

      我们想要一个包含值的地图:

      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);
          }
      }