如何在不使用集合的情况下 return 多个值?
How to return multiple values without using Collections?
我正在使用教科书 Murach 的 java 编程,在其中一个练习中,它要求我执行以下操作:
add this method (given by the book):
private static String displayMultiple(Displayable d, int count)
write the code for this method so it returns a String that contains the Displayable parameter the number of times specified by the int parameter.
Displayable
is an interface that implements getDisplayText()
. And this method just returns a String
with instance variables of an object, i.e. for an Employee, it returns first name, last name, department, and salary.
一切正常,除了“returns a String”。
这可能是一个关于循环的练习:
- 您有办法将
d
转换为字符串:getDisplayText
。比如说,这会产生 "ABCD"
- 您想 return
count
乘以该字符串 "ABCD"
。如果 count == 3
,则表示 "ABCDABCDABCD"
。
有用的关键字:for loop
、StringBuilder
。这是您可以用来入门的模板:
String text = ;// Use getDisplayText here
StringBuilder ret = new StringBuilder();
/* Loop from 0 to count - 1 */ {
// Append `text` to `ret`
}
return ret.toString();
您实际上不需要 return 多个值。
据我了解:
private static String displayMultiple(Displayable d, int count){
String s = "";
String ss = d.getDisplayText();
for(int i=0; i<count; i++){
s += ss;
}
return s;
}
如果您想 return 多个值而不使用集合,那么您可以创建一个 Class -
public class MultipleValue{
String firstValue;
String secondValue;
//other fields
}
然后从 someMethod()
你想要 return 多个值(即 firstValue
, secondValue
)你可以这样做 -
public MultipleValue someMethod(){
MultipleValue mulVal = new MultipleValue();
mulVal.setFirstValue("firstValue");
mulVal.setSecondValue("secondVAlue");
return mulVal;
}
然后从 someMethod()
的调用 class 你可以像这样提取多个值(即 firstValue
和 secondValue
) -
//from some calling method
MultipleValue mulVals = someMethod();
String firstValue = mulVals.getFirstValue();
String secondValue = mulVals.getSecondValue();
我正在使用教科书 Murach 的 java 编程,在其中一个练习中,它要求我执行以下操作:
add this method (given by the book):
private static String displayMultiple(Displayable d, int count)
write the code for this method so it returns a String that contains the Displayable parameter the number of times specified by the int parameter.
Displayable
is an interface that implementsgetDisplayText()
. And this method just returns aString
with instance variables of an object, i.e. for an Employee, it returns first name, last name, department, and salary.
一切正常,除了“returns a String”。
这可能是一个关于循环的练习:
- 您有办法将
d
转换为字符串:getDisplayText
。比如说,这会产生"ABCD"
- 您想 return
count
乘以该字符串"ABCD"
。如果count == 3
,则表示"ABCDABCDABCD"
。
有用的关键字:for loop
、StringBuilder
。这是您可以用来入门的模板:
String text = ;// Use getDisplayText here
StringBuilder ret = new StringBuilder();
/* Loop from 0 to count - 1 */ {
// Append `text` to `ret`
}
return ret.toString();
您实际上不需要 return 多个值。
据我了解:
private static String displayMultiple(Displayable d, int count){
String s = "";
String ss = d.getDisplayText();
for(int i=0; i<count; i++){
s += ss;
}
return s;
}
如果您想 return 多个值而不使用集合,那么您可以创建一个 Class -
public class MultipleValue{
String firstValue;
String secondValue;
//other fields
}
然后从 someMethod()
你想要 return 多个值(即 firstValue
, secondValue
)你可以这样做 -
public MultipleValue someMethod(){
MultipleValue mulVal = new MultipleValue();
mulVal.setFirstValue("firstValue");
mulVal.setSecondValue("secondVAlue");
return mulVal;
}
然后从 someMethod()
的调用 class 你可以像这样提取多个值(即 firstValue
和 secondValue
) -
//from some calling method
MultipleValue mulVals = someMethod();
String firstValue = mulVals.getFirstValue();
String secondValue = mulVals.getSecondValue();