日期:2014-05-20 浏览次数:20944 次
package com.test; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; public class Test { public static void main(String[] args) { Map<Integer, RandomInt> map = new HashMap<Integer, RandomInt>(); Map<Integer, Integer> map2 = new HashMap<Integer, Integer>(); List<RandomInt> list=new ArrayList<RandomInt>(); int id = 0; int[] a = new int[2]; for (int m = 1; m <4; m++) { a[0] = m; for (int n = m + 2; n <6; n++) { a[1] = n; RandomInt cc=new RandomInt(a); map.put(id++, cc); map2.put(id++, a[1]+a[0]); list.add(cc); } } System.out.println(map);//打印结果 System.out.println(map2);//结果无问题 System.out.println(list);// System.out.println("***********"); } } class RandomInt { private int[] in; public RandomInt() { } public RandomInt(int[] in) { this.in = in; } public int[] getIn() { return in; } public void setIn(int[] in) { this.in = in; } @Override public String toString() { return "RandomInt [in=" + Arrays.toString(in) + "]"; } }
package a; import java.util.*; public class HashMapAdd { public static void main(String[] args) { HashMap<String, Object> hm = new HashMap<String, Object>(); StringBuffer sb = new StringBuffer(); sb.append("abc,"); hm.put("1", sb); sb.append("def,"); hm.put("2", sb); sb.append("mno,"); hm.put("3", sb); sb.append("xyz."); hm.put("4", sb); int numObj = 0; Set<String> key = hm.keySet(); for (Iterator it = key.iterator(); it.hasNext();) { String s = (String) it.next(); System.out.print("get StringBufffer " + (++numObj) + " from Hashtable: "); System.out.println(hm.get(s)); } } }