import java.util.Arrays;
public class EntryDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 产生一个容器
MyContainer mycontainer = new MyContainer();
mycontainer.put("name1", "阿波罗");
mycontainer.put("name2", "缪斯");
mycontainer.put("name3", "宙斯");
MyContainer.Entry[] myentry = mycontainer.entryArrays();
for (int i = 0; i < myentry.length; i++) {
MyContainer.Entry entry = myentry[i];
System.out.println(entry.getKey() + "___" + entry.getValue());
}
}
}
class MyContainer {
/** 存放Entry对象的数组,默认大小为5 */
private Entry[] entrys = new Entry[5];
int count = 0; // 下标
// 对外提供一个接口向容器中存放封装好的数据,Entry对象
public void put(String key, String value) {
Entry entry = new Entry();
entry.setKey(key);
entry.setValue(value);
// 存放到entry对象数组
entrys[count++] = entry;
// 数组扩容
if (count >= entrys.length) {
// 扩容后新的数组的大小
int newCapacity = entrys.length * 2;
// 利用数组的拷贝的方法
entrys = Arrays.copyOf(entrys, newCapacity);
}
}
// 把容器中的数据返回
public Entry[] entryArrays() {
// 拷贝有效的数据返回
return Arrays.copyOfRange(entrys, 0, count);
}
public static class Entry {
/** 内部类把键值对封装在Entry对象中 */
private String key;
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
public class EntryDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 产生一个容器
MyContainer mycontainer = new MyContainer();
mycontainer.put("name1", "阿波罗");
mycontainer.put("name2", "缪斯");
mycontainer.put("name3", "宙斯");
MyContainer.Entry[] myentry = mycontainer.entryArrays();
for (int i = 0; i < myentry.length; i++) {
MyContainer.Entry entry = myentry[i];
System.out.println(entry.getKey() + "___" + entry.getValue());
}
}
}
class MyContainer {
/** 存放Entry对象的数组,默认大小为5 */
private Entry[] entrys = new Entry[5];
int count = 0; // 下标
// 对外提供一个接口向容器中存放封装好的数据,Entry对象
public void put(String key, String value) {
Entry entry = new Entry();
entry.setKey(key);
entry.setValue(value);
// 存放到entry对象数组
entrys[count++] = entry;
// 数组扩容
if (count >= entrys.length) {
// 扩容后新的数组的大小
int newCapacity = entrys.length * 2;
// 利用数组的拷贝的方法
entrys = Arrays.copyOf(entrys, newCapacity);
}
}
// 把容器中的数据返回
public Entry[] entryArrays() {
// 拷贝有效的数据返回
return Arrays.copyOfRange(entrys, 0, count);
}
public static class Entry {
/** 内部类把键值对封装在Entry对象中 */
private String key;
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}