2012-02-11 49 views
1

因此,这部分作业要求我们采用一组字符串,然后我们将返回一个字符串列表。在字符串集中,我们将有电子邮件地址,即[email protected]。我们要拉第一部分的电子邮件地址;该名称并将其放入字符串列表中。从上面的示例myname将被放入列表中。 我目前使用的代码使用迭代器从Set中提取字符串。然后,我使用String.contains(“@”)作为错误检查,以确保String中包含@符号。然后我从字符串的末尾开始,并使用string.charAt(“@”)来检查每个字符。一旦找到我,然后用正确的部分创建一个子字符串并将其发送到列表。 我的问题是我想使用递归和减少操作。我想到的东西会分割string.length()/ 2,然后在下半部分首先使用String.contains(“@”)。如果那一半包含@符号,那么它会递归地调用函数。如果后半部分不包含@符号,则前半部分将包含它,我们将调用函数递归发送它。查找字符优化

所以我的问题是,当我递归地调用函数并发送它的“子字符串”,一旦我找到@符号,我将只有子字符串的索引,而不是原始字符串的索引。关于如何跟踪它的任何想法或者我应该关注的命令/方法。以下是我的原始代码。欢迎任何建议。

public static List<String> parseEmail(Set<String> emails) 
    { 
     List<String> _names = new LinkedList<String>(); 
     Iterator<String> eMailIt=emails.iterator(); 

     while(eMailIt.hasNext()) 
     { 
      String address=new String(eMailIt.next()); 
      boolean check=true; 
      if(address.contains("@"))//if else will catch addresses that do not contain '@' . 
      { 
       String _address=""; 
       for(int i=address.length(); i>0 && check; i--) 
       { 
       if('@'==address.charAt(i-1))  
       { 
        _address=new String(address.substring(0,i-1)); 
        check=false; 
       } 
       } 
       _names.add(_address); 
       //System.out.println(_address);//fill in with correct sub string 
      } 
      else 
      { 
       //System.out.println("Invalid address"); 
       _names.add("Invalid address");//This is whats shownn when you have an address that does not have an @ in it. 
      }        // could have it insert some other char i.e. *%# s.t. if you use the returned list it can skip over invalid emails 
     } 
     return _names; 
    } 

**有人建议我用String.indexOf(“@”),但根据API此方法只还给符号的第一次出现,我必须假设的工作,有可能在地址中是多个“@”,我必须使用最后一个。不过谢谢你的建议。我正在看另一个建议,并会回报。

***所以有一个string.lastindexOf(),这就是我所需要的。

public static List<String> parseEmail(Set<String> emails) 
    { 
     List<String> _names = new LinkedList<String>(); 
     Iterator<String> eMailIt=emails.iterator(); 

     while(eMailIt.hasNext()) 
     { 
      String address=new String(eMailIt.next()); 
      if(address.contains("@"))//if else will catch addresses that do not contain '@' . 
      { 
       int endex=address.lastIndexOf('@'); 
       _names.add(address.substring(0,endex-1)); 
//    System.out.println(address.substring(0,endex)); 
      } 
      else 
      { 
//    System.out.println("Invalid address"); 
       _names.add("Invalid address");//This is whats shownn when you have an address that does not have an @ in it. 
      }        // could have it insert some other char i.e. *%# s.t. if you use the returned list it can skip over invalid emails 
     } 
     return _names; 
    } 
+0

'eMailIt.next()'和'address.substring(0,i-1)'都返回一个String,并且不需要在代码中调用'new'运算符。 – Gevorg 2012-02-11 19:08:08

回答

2

不要重新发明轮子(除非你被问到当然)。 Java已经为你正在尝试的内置函数String.indexOf(String str)。用它。

final String email = "[email protected]"; 
final int atIndex = email.lastIndexOf("@"); 
if(atIndex != -1) { 
    final String name = email.substring(0, atIndex); 
} 
+0

我必须考虑具有多个@符号的地址,并使用字符串前的最后一个符号。 @ 123 @@ uark.com将返回@ 123 @。 – 2012-02-11 17:18:43

+0

嗯,虽然我不确定'@'是电子邮件名称或域名部分中的有效字符,但仍然可以这样做:用另一个标准JDK调用'lastIndexOf'替换对'indexOf'的调用。 – Perception 2012-02-11 17:21:20

+0

在那里,我们去....谢谢你的感知。这是我认为我需要的。它可能不会“加速”我的代码,因为我不知道它的实现如何,但它会使我的解析邮件方法代码更清晰。 – 2012-02-11 17:40:14

0

除非在作业中指定递归,您将得到最好的寻找到String.split服务。它会将字符串拆分为一个字符串数组(如果您指定它在'@'左右),并且您可以访问这两个电子邮件地址。

+0

'String.split'使用正则表达式。 _IF_表现真的很重要(并且提问者暗示如此),那么在RegExp中使用任何内容都不是好事。 – 2012-02-11 17:39:33

1

我同意前面的两个答案,如果你被允许使用内置的功能splitindexOf,那么你应该。然而,如果它是你自己发现子串的作业的一部分,那么你肯定应该通过字符串的字符并停止,当你发现@又名线性搜索

你绝对不应该在任何情况下试图以递归方式做这件事:在没有任何东西可以获得的情况下,不应该滥用分治思想:递归意味着函数调用开销,递归执行此操作只会有如果并行搜索子字符串,则可能比简单的线性搜索更快;即使如此:同步开销会杀死除了最庞大的字符串以外的所有加速器。

+0

不是递归执行它的一部分。我只是认为这会更有效率,更好的做法。 – 2012-02-11 17:20:28

+0

+1为递归部分 – 2012-02-11 17:46:50