2014-10-19 55 views
1

我有这个C#静态函数下面,需要一个MatchCollection,输入字符串和模式来搜索。 MathCollection通过引用传递,所以为了使用它,我不需要两次运行匹配。我正是如此使用它:有没有Java相当于C#出关键字

 MatchCollection matches = new MatcchCollection(); 
     string pattern = "foo"; 
     string input = "foobar"; 

     if (tryRegMatch(matches, input, pattern) { //do something here} 

public static boolean tryRegMatch(out MatchCollection match, string input, string pattern) 
    { 
     match = Regex.Matches(input, pattern); 
     return (match.Count > 0); 
    } 

的问题是,是否有可能做到这一点在Java中我已看过一些文章,状态Java是按值传递(保持简单)。默认情况下C#是,但是你可以使用'out'修饰符来使它通过引用传递。我做了很多匹配,这会使编码更简单,否则我必须运行匹配,然后单独测试以获得成功。

+1

Java只知道如何通过值传递,从不参考;所以不,这是不可能的_stricto sensu_ – fge 2014-10-19 01:53:19

回答

1

不,但您可以通过简单地包装对象来近似参考习惯用法。

class MatchRef 
{ 
    public MatchCollection match; 
} 

public static boolean tryRegMatch(MatchRef matchRef, string input, string pattern) 
{ 
    matchRef.match = Regex.Matches(input, pattern); 
    return (matchRef.match.Count > 0); 
} 
+0

也许是一个与泛型的解决方案? – Mephy 2014-10-19 02:53:06

+0

谢谢。我去做。 – 2014-10-19 13:01:45