是否有一个Java等价于C#Out关键字
我下面有一个C#静态函数,它接受一个匹配集合、一个输入字符串和一个要搜索的模式。MathCollection是通过引用传递的,因此要使用它,我不必运行两次匹配。我可以这样使用它:是否有一个Java等价于C#Out关键字,java,c#,Java,C#,我下面有一个C#静态函数,它接受一个匹配集合、一个输入字符串和一个要搜索的模式。MathCollection是通过引用传递的,因此要使用它,我不必运行两次匹配。我可以这样使用它: MatchCollection matches = new MatcchCollection(); string pattern = "foo"; string input = "foobar"; if (tryRegMatch(matches, input, patt
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”修饰符使其通过引用传递。我正在进行大量匹配,这将使编码更简单,否则我必须运行匹配,然后单独测试是否成功。否。但您可以通过简单地包装对象来近似参考惯用法
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);
}
Java只知道如何通过值传递,从不通过引用传递;所以不,严格意义上说,不可能用泛型解决方案?谢谢。我会的。