楼主想用一个方法返回多个值,对值的类型有要求吗?
#1:同类型,可用数组,见楼上答复;
另外也可以用Hashtable,见代码如下:
- import java.util.*;
- /*
- * 利用Hashtable,实现一个函数返回多个值
- * [b]建议修改本程序HashMap,避免安全警告![/b]
- * Created by Ian Feyman
- * */
- public class Test
- {
- public static Hashtable Lookfor(String[] key,int x,int y,int z)
- {
- Hashtable<String,Integer> retValue = new
- Hashtable<String,Integer>();
-
- //进行操作
- retValue.put(key[1],x);
- retValue.put(key[2],y);
- retValue.put(key[3],z);
- return retValue;
- }
- public static void ShowResult(Hashtable htable)
- {
- System.out.println("Value1: "+htable.get("one")+
- "\nValue2: "+ htable.get("two")+
- "\nValue3: "+htable.get("three"));
- }
- public static void main(String[] args)
- {
- Hashtable<String,Integer> multiValue = new
- Hashtable<String,Integer>();
- String[] str = new String[6];
- str[1] = "one";
- str[2] = "two";
- str[3] = "three";
- multiValue = Lookfor(str,100,200,300);
- ShowResult(multiValue);
- }
- }
复制代码
|