java方法能带传出参数吗
当Java中函数中,想要实现output parameter时,对应的参数,不能是Immutable,即不能是,不可更改的,
而String就是Immutable,所以要换用StringBuilder之类的,Mutable的类型的变量,去实现对应的output parameter。
参考:http://stackoverflow.com/questions/2824910/how-to-use-an-output-parameter-in-java
示例:
public
Boolean extractSingleStr(String pattern, String extractFrom, StringBuilder extractedStr) {
Pattern
strP = Pattern.compile(pattern);
Matcher
foundStr = strP.matcher(extractFrom);
Boolean
found = foundStr.find();
if(found)
{
extractedStr.append(foundStr.group(1));
}
return
found;
}
使用:
StringBuilder
extractedTitle = new
StringBuilder();
if(extractSingleStr("<p\\s+class=\"mid_tit\">(.+?)</p>",
respHtml, extractedTitle))
{
etUrlOrId.setText(extractedTitle);
}