实现键值对的存储输出
import java.util.Arrays; public class EntryDemoTest { //实现键值对的存储 public static void main(String[] args) { // TODO Auto-generated method stub MyContainer container = new MyContainer(); container.put("a", "小明1"); container.put("b", "小明2"); container.put("c", "小明3"); container.put("d", "小明4"); MyContainer.Entry[] entrys = container.entryArray(); for(int i = 0;i<entrys.length;i++) { MyContainer.Entry entry = entrys[i]; System.out.println(entry.getKey()+"----"+entry.getValue()); } } } class MyContainer{ //定义,初识设定数组容量 Entry[] entrys =new Entry[5]; private int count = 0;//数组的下标 public void put(String key,String value) {//容器的进口 Entry entry = new Entry(); entry.setKey(key); entry.setValue(value); entrys[count++] = entry; if(count>entrys.length) { int newCapacity; newCapacity = entrys.length*2; entrys = Arrays.copyOf(entrys, newCapacity); } } //返回有数据的内容 public Entry[] entryArray() { return Arrays.copyOfRange(entrys, 0 , count); } public static class Entry{//将键值对封装在Entry里,这是个静态的! private String key; private String value; public void setKey(String key) { this.key = key; } public void setValue(String value) { this.value = value; } public String getKey() { return key; } public String getValue() { return value; } } }