[英]Return more than one variable from Java method
抱歉愚蠢的問題......任何人都可以幫我從方法中返回兩個變量(我在這里閱讀並嘗試重新編碼,但沒有結果)。
public class FileQualityChecker {
EnviroVars vars = new EnviroVars();
public int n = 0;
public int z = 0;
public int m = 0;
String stringStatus;
public void stringLenghtCheck(String pathToCheckedFile)
{
try{
FileInputStream fstream = new FileInputStream(pathToCheckedFile+"\\"+"Test.dat");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null)
{
if (
strLine.length() == vars.strLenghtH ||
strLine.length() == vars.strLenghtCUoriginal ||
strLine.length() == vars.strLenghtCEAoriginal ||
strLine.length() == vars.strLenghtCAoriginal ||
strLine.length() == vars.strLenghtTrailer ||
strLine.length() == vars.strLenghtLastRow
)
{
stringStatus = "ok";
n++;
z++;
}
else
{
stringStatus = "Fail";
n++;
m++;
}
System.out.println (n +" " + strLine.length() +" " + stringStatus);
}
//Close the input stream
in.close();
}
catch
(Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
/*How to return m and z from method to use these vars for writing in the file*/
return (m, z);
}
}
我需要在另一個類中使用m和z將它們寫入文件。 謝謝。
我對這些問題的第一個問題是,這兩個結果之間的關系是什么?
如果它們不相關,這是否指向您的方法做兩件不同的事情?
如果它們相關(它們似乎是在這種情況下),則將它們包裝在自定義對象中。 這使您有機會稍后在此對象中添加更多結果, 並且可以將行為附加到此對象。
您的另一個解決方案是將回調對象傳遞給此方法,因此您根本不返回任何內容,而是您的方法然后調用此對象上的方法,例如
// do some processing
callbackObject.furtherMethod(m, n);
這樣做的好處是遵循OO原則,即讓對象為您做事,而不是向他們詢問信息並自己動手 。
創建一個單獨的類,並將m和z作為該對象中的變量,創建並填充該對象並返回該對象
class result {
int m ;
int z ;
// define getters and setters
}
public result stringLengthCheck(String pathToFile)
{
// your code
result r = new result();
r.setM(m);
r.setZ(z);
return r ;
}
如何返回地圖。 例如:
Map<String,Integer> result = new HashMap<String,Integer>();
result.put("ok",m);
result.put("fail",z);
return result;
並且您的方法應更改為:
public Map stringLenghtCheck(String pathToCheckedFile)
最后,你可以通過檢查地圖得到結果:
map.get("ok")
map.get("fail")
您不能從方法返回多個變量。 你需要返回別的東西。 可以提出許多建議:
set
和get
使用回調(如void setToArray(int[] array, args...)
) - 相同的+/- 1-3點說和
還有一些黑客(黑客 - 不是偶然的情況!)也可以發生:
使用連接。 您可以使用分隔符將多個字符串連接在一起,或者在long中存儲兩個整數
InputStream
和OutputStream
Iterator
(我把它放在'hacks'之下只是因為在很多方面它假設存儲變量就像在包裝器中一樣)
String
(或byte[]
)中並在需要時解析
String
對於存儲兩個整數,經常使用Point
和Dimension
類。 但是對於給出的代碼,我建議使用簡單的數組或在FileQualityChecker
類中存儲變量,因為m
和z
似乎不適合坐標或維度上下文。
您可以創建自定義類或返回列表:
return Arrays.asList(m, z);
(您的方法的返回類型將是List<Integer>
)
或者在你的情況下,由於m
和z
是類成員,你可以返回任何內容(void)並提供getter以在方法返回后獲取m
和z
的值。
不,你不能,但寫一個包含這些字段的類很容易:
class StrAndInt {
String str;
int num;
StrAndInt(String str, int num) {
this.str = str;
this.num = num;
}
}
class MyApplication {
....
....
StrAndInt myMethod() {
....
....
return new StrAndInt("The value returned", 10);
}
另一種選擇是不返回任何內容,因為您可以使用其他方法獲取這些結果。
FileQualityChecker fqc = ..
fqc.stringLenghtCheck(fileName);
int okCount = fqc.getOkCount();
int failedCount = fqc.getFailedCount();
我不認為你可以從方法返回兩個變量,因為一個方法只能有一個返回類型。 所以IMO你有兩個選擇:
1)創建一個具有相同類型變量的類,並為在該類中定義的變量生成getter-setter。 現在,從要返回變量的方法創建該類的對象,並使用setter設置實例變量的值,並使用要返回的變量的值返回該對象。
2)創建這些變量的數組並返回該數組。
我希望m
和n
使用pass by值 。 2.Instead use Pass by Reference.To在Java中通過引用傳遞 ,它必須是一個Java對象。 3.因此創建新的Integer對象m = new Integer(); 。
您可以將兩個變量放在int[]
並在調用方法時提取這兩個值。
return new Integer[]{m, z};
調用方法后:
Integer[] array = Foo.method();
int m = array[0];
int z = array[1];
你可以簡單地構造一個對象。
但是,如果必須返回多個變量,則可以返回類似類型變量的數組,例如:
public static StringBuilder[] remedySBarrayreturner() {
sbbb.append("huhu");
sbcc.append("hihi");
return new StringBuilder[] {sbbb, sbcc};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.