海信家电”换帅 “ 高玉玲接替代慧忠出任新任董事长
2024-11-22
List list = new ArrayList(); list.add("data"); // 在这里没问题 list.ensureCapacity(4); // 这里就不行了ensureCapacity() 只在ArrayList中才有。 |
Object o = new ArrayList(); List list = (List)o; |
IAdaptable adaptable = new ArrayList(); List list = (List)adaptable.getAdapter(java.util.List.class); |
IAdaptable adaptable = new HashMap(); List list = (List)adaptable.getAdapter(java.util.List.class); |
public class HashMap implements IAdaptable { public Object getAdapter(Class clazz) { if (clazz == java.util.List.class) { List list = new ArrayList(this.size()); list.addAll(this.values()); return list; } return null; } // ... } |
<List> <Entry>First String</Entry> <Entry>Second String</Entry> <Entry>Third String</Entry> </List> |
import nu.xom.*; public class NodeListFactory implements IAdapterFactory { /** The supported types that we can adapt to */ private static final Class[] types = { Node.class, }; public Class[] getAdapterList() { return types; } /** The actual conversion to a Node */ public Object getAdapter(Object list, Class clazz) { if (clazz == Node.class && list instanceof List) { Element root = new Element("List"); Iterator it = list.iterator(); while(it.hasNext()) { Element item = new Element("Entry"); item.appendChild(it.next().toString()); root.appendChild(item); } return root; } else { return null; } } } |
Platform.getAdapterManager().reGISterAdapters( new NodeListFactory(), List.class ); |
Node getNodeFrom(IAdaptable list) { Object adaptable = list.getAdapter(Node.class); if (adaptable != null) { Node node = (Node)adaptable; return node; } return null; } |
public class AdaptableList implements IAdaptable, List { public Object getAdapter(Class adapter) { return Platform.getAdapterManager().getAdapter(this, adapter); } private List delegate = new ArrayList(); public int size() { return delegate.size(); } // ... } |
评论 {{userinfo.comments}}
{{child.content}}
{{question.question}}
提交