0

我在 a 中编写了一些对象属性Hashtable<Integer,Hashtable<String,Object>>,其中:

  • Integer是主键Hashtable(代表对象编号)
  • everyHashtable<String,Object>分别代表 propertyname (String)和 property value(Object)

我想将所有属性值放入包含属性值的ArrayList(或数组...)中,然后想访问每个Object. 我怎样才能做到这一点?

4

2 回答 2

1

如果您只想要一个用于此目的的列表,Maxim 的解决方案非常好,或者您可以创建一个自定义类来实现集合并在内部管理哈希表和对象。如果您需要在程序的许多方面使用它,我喜欢第二种方式。

例如,您可以修改此类并将具有所有属性和 hastable 链接到字符串键的自定义类添加为 Element T,并在此类中添加用于搜索通过键名的自定义方法(请原谅我的英语):

  public class NList<T> implements Iterable<T> //, List<T>
  {
     private boolean synchron;
     public List<T>  list;

     public NList(boolean synchron)
     {
        this(15, synchron);
     }

     public NList(int initialCapacity, boolean synchron)
     {
        this.synchron = synchron;
        this.list = synchron ? new Vector<T>(initialCapacity) : new ArrayList<T>(initialCapacity);
     }

     public NList(Collection<T> c, boolean synchron)
     {
        this.synchron = synchron;
        this.list = synchron ? new Vector<T>(c) : new ArrayList<T>(c);
     }

     public final boolean isSynchronized()
     {
        return synchron;
     }

     //@Override
     public final boolean add(T element)
     {
        return list.add(element);
     }

     //@Override
     public final void add(int index, T element)
     {
        list.add(index, element);
     }

     //@Override
     public final T remove(int index)
     {
        return list.remove(index);
     }

     //@Override
     public final List<T> subList(int fromIndex, int toIndex)
     {
        return list.subList(fromIndex, toIndex);
     }

     //@Override
     @SuppressWarnings("unchecked")
     public final T[] toArray()
     {
        return (T[])list.toArray();
     }

     //@Override
     public final T get(int index)
     {
        return list.get(index);
     }

     //@Override
     public final int size()
     {
        return list.size();
     }

     //@Override
     public final boolean isEmpty()
     {
        return list.isEmpty();
     }

     //@Override
     public final void clear()
     {
        list.clear();
     }

     @Override
     public final Iterator<T> iterator()
     {
        return list.iterator();
     }

     //@Override
     public final boolean contains(Object element)
     {
        return list.contains(element);
     }

     //@Override
     @SuppressWarnings("hiding")
     public final <T> T[] toArray(T[] a)
     {
        return list.toArray(a);
     }

     //@Override
     public final boolean remove(Object element)
     {
        return list.remove(element);
     }

     //@Override
     public final boolean containsAll(Collection<?> c)
     {
        return list.containsAll(c);
     }

     //@Override
     public final boolean addAll(Collection<? extends T> c)
     {
        return list.addAll(c);
     }

     //@Override
     public final boolean addAll(int index, Collection<? extends T> c)
     {
        return list.addAll(index, c);
     }

     //@Override
     public final boolean removeAll(Collection<?> c)
     {
        return list.removeAll(c);
     }

     //@Override
     public final boolean retainAll(Collection<?> c)
     {
        return list.retainAll(c);
     }

     //@Override
     public final T set(int index, T element)
     {
        return list.set(index, element);
     }

     //@Override
     public final int indexOf(Object o)
     {
        return list.indexOf(o);
     }

     //@Override
     public final int lastIndexOf(Object o)
     {
        return list.lastIndexOf(o);
     }

     //@Override
     public final ListIterator<T> listIterator()
     {
        return list.listIterator();
     }

     //@Override
     public final ListIterator<T> listIterator(int index)
     {
        return list.listIterator(index);
     }
  }
于 2013-09-14T11:14:10.623 回答
0

像这样的东西:

List<HashTable<Integer,Object>> list = new ArrayList<HashTable<Integer,Object>>();

供订单使用LinkedHashTable

这就是您如何访问HashTable

Hashtable<Integer, Hashtable<String, Object>> properties = new Hashtable<Integer, Hashtable<String, Object>>(); 

    Enumeration<Integer> nmExt;
    Enumeration<String> nmInt;

    Hashtable<String, Object> innerHash;
    int externalKey;
    String interanlKey;
    Object obj;

    nmExt = properties.keys();

       while (nmExt.hasMoreElements()){
           externalKey = nmExt.nextElement();

           innerHash =  properties.get(externalKey);

           nmInt = innerHash.keys();

           while (nmExt.hasMoreElements()){
               interanlKey = nmInt.nextElement();

               obj = innerHash.get(interanlKey);

               // .....
           }

        }
于 2013-09-14T10:56:39.697 回答