Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有一个Java等价于C#Out关键字_Java_C# - Fatal编程技术网

是否有一个Java等价于C#Out关键字

是否有一个Java等价于C#Out关键字,java,c#,Java,C#,我下面有一个C#静态函数,它接受一个匹配集合、一个输入字符串和一个要搜索的模式。MathCollection是通过引用传递的,因此要使用它,我不必运行两次匹配。我可以这样使用它: MatchCollection matches = new MatcchCollection(); string pattern = "foo"; string input = "foobar"; if (tryRegMatch(matches, input, patt

我下面有一个C#静态函数,它接受一个匹配集合、一个输入字符串和一个要搜索的模式。MathCollection是通过引用传递的,因此要使用它,我不必运行两次匹配。我可以这样使用它:

      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只知道如何通过值传递,从不通过引用传递;所以不,严格意义上说,不可能用泛型解决方案?谢谢。我会的。