zoukankan      html  css  js  c++  java
  • java内部类案例

    实现键值对的存储输出

    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;
    		}
    
    	}
    		
    	
    }
    

      

  • 相关阅读:
    ios7--UIImageView
    ios6--UILabel
    ios5--计算器
    ios4--UIView的常见属性(尺寸和位置)
    ios2--UIView的常见属性
    PG数据库获取最近四个小时 使用产品的用户审计信息
    可添加头部尾部RecyclerView,很帅哦~
    http请求及缓存框架 GalHttprequest
    据说年薪30万的Android程序员必须知道的帖子
    Android显示GIF动画 GifView
  • 原文地址:https://www.cnblogs.com/liubing2018/p/8463434.html
Copyright © 2011-2022 走看看