给出一个简单的代码,水平很差,欢迎p我,哈哈! package com.vagrant.studyclass;import java.util.ArrayList; import java.util.Hashtable; import java.util.Iterator; import java.util.List; import java.util.Set;public class Test { /** * @param args */ public static void main(String[] args) throws Exception{
String key = "key";
Hashtable h = new Hashtable(); Hashtable hh = new Hashtable(); hh.put(new Integer(1), "hao1"); hh.put(new Integer(2), "hao2"); hh.put(new Integer(3), "hao3"); hh.put(new Integer(4), "hao4");
h.put(key, hh);
Object[] o = getValue((Hashtable) h.get(key)); for (int i = 0; i < o.length; i++) { System.out.println("value-----------" + o[i].toString()); } }
public static Object[] getValue(Hashtable h) { if (h == null) { return null; }
List list = new ArrayList(); Set set = h.keySet(); for (Iterator it = set.iterator(); it.hasNext();) { Integer key = (Integer)it.next(); list.add(h.get(key)); }
Object[] o = new Object[list.size()]; list.toArray(o); return o; }}
『
A a1;
A a2;
』
封装一下 放到数组里呢?
A2再队A呢?
package com.vagrant.studyclass;import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;public class Test { /**
* @param args
*/
public static void main(String[] args) throws Exception{
String key = "key";
Hashtable h = new Hashtable();
Hashtable hh = new Hashtable();
hh.put(new Integer(1), "hao1");
hh.put(new Integer(2), "hao2");
hh.put(new Integer(3), "hao3");
hh.put(new Integer(4), "hao4");
h.put(key, hh);
Object[] o = getValue((Hashtable) h.get(key));
for (int i = 0; i < o.length; i++) {
System.out.println("value-----------" + o[i].toString());
}
}
public static Object[] getValue(Hashtable h) {
if (h == null) {
return null;
}
List list = new ArrayList();
Set set = h.keySet();
for (Iterator it = set.iterator(); it.hasNext();) {
Integer key = (Integer)it.next();
list.add(h.get(key));
}
Object[] o = new Object[list.size()];
list.toArray(o);
return o;
}}
http://jakarta.apache.org/commons/collections/