标签:oid map lct private remove cep 集合类 不能 element
---恢复内容开始---
Java 集合类:1.用来存储数量不等的多个对象 ;2.保存具有映射关系的关联数组。
collction接口:1.set:元素无序,不可重复的集合
2.list :元素有序,可重复集合----动态的数组
Map接口:具有映射关系 “ key-value对”的集合 通过key来寻找value
Arraylist方法 的自我实现:
1.先建立一个节点的类:
package cn.klscode; //用来表示一个节点 public class Node { Node previous; Object obj; Node next; public Node(){}
public Node(Node previous, Object obj, Node next) {
super();
this.previous = previous;
this.obj = obj;
this.next = next; } public Object getPrevious() { return previous; } public void setPrevious(Object previous) { this.previous = (Node) previous; } public Object getObj() { return obj; } public void setObj(Object obj) { this.obj = obj; } public Object getNext() { return next; } public void setNext(Node next) { this.next = next; } }
package cn.klscode; //实现一个Arraylist,帮助理解 public class Kls { private Object[] elementData;//私有化一个Object类型的数组 private int size; public int size() { return size; }//判断elementdata是否为空 public boolean isempty(){ return size==0; }; // public Kls(){ // this(10); // } public Kls(int initalCapacity){ if (initalCapacity<0){ try { throw new Exception(); } catch (Exception e) { e.printStackTrace(); // TODO: handle exception } } elementData=new Object[initalCapacity]; } // 利用arraycopy来实现对数据的添加 public void add(Object obj){ if (size==elementData.length) { Object[] newArray=new Object[size*2+1]; // for (int i = 0; i < elementData.length; i++) { // newArray[i]=elementData[i]; // } System.arraycopy(elementData,0,newArray, 0 ,elementData.length); elementData=newArray; } elementData[size++]=obj; // elementData[size]=obj; // size++; } //利用数组的特性来实现对应下标显示数据 public Object get(int index) { if (index<0||index>size) { try { throw new Exception(); } catch (Exception e) { e.printStackTrace(); // TODO: handle exception } } return elementData[index]; } //通过索引来实现对数据的删除 public void remove(int index){ rangeCheck(index); int numMoved=size-index-1; if(numMoved>0){ System.arraycopy(elementData, index+1, elementData, index, numMoved); } elementData[--size]=null; } //通过元素来反推i调用remove(i); public void remove(Object obj ){ for (int i = 0; i < size; i++) { if (get(i).equals(obj)) { remove(i); } } } public Object set(int index,Object obj) { rangeCheck(index); Object oldValue=elementData[index]; elementData[index]=obj; return oldValue; }
public void add(int index,Object obj){ ensureCapacity(); rangeCheck(index); System.arraycopy(elementData, index, elementData, index+1, size-index);; elementData[index]=obj; size++; }
private void rangeCheck(int index) { // TODO Auto-generated method stub if (index<0||index>size) { try { throw new Exception(); } catch (Exception e) { e.printStackTrace();// TODO: handle exception } } }
//按照2*size+1来扩充数组,相当于新建了一个数组,将旧的的数组放到新建的数组里 private void ensureCapacity() { if (size==elementData.length) { Object[] newArray=new Object[size*2+1]; System.arraycopy(elementData, 0, newArray, 0,elementData.length); elementData=newArray; } } public static void main(String[] args) { Kls kls=new Kls(5); kls.add("333"); kls.add("333"); kls.add("333"); kls.add("333"); kls.add("333"); kls.add("333"); kls.add("333"); kls.add("333"); kls.add("333"); kls.add("333"); System.out.println(kls.size); kls.remove(3); System.out.println(kls.size); for (int i = 0; i < kls.size; i++) { String str=(String) kls.get(i); System.out.print(str+"\t"); } //System.out.println(kls); } }
2.Hashmap方法的具体实现:
package hashmap; /** *自定义实现Map的功能! *暂不完美! *Map:存放键值对,根据键对象找对应的值对象.键不能重复! * */ public class Klmap { SxtEntry[] arr = new SxtEntry[990]; int size; public void put(Object key,Object value){ SxtEntry e = new SxtEntry(key,value); //解决键值重复的处理 for(int i=0;i<size;i++){ if(arr[i].key.equals(key)){ arr[i].value=value; return ; } } arr[size++] = e; } public Object get(Object key){ for(int i=0;i<size;i++){ if(arr[i].key.equals(key)){ return arr[i].value; } } return null; } public boolean containsKey(Object key){ for(int i=0;i<size;i++){ if(arr[i].key.equals(key)){ return true; } } return false; } public boolean containsValue(Object value){ for(int i=0;i<size;i++){ if(arr[i].value.equals(value)){ return true; } } return false; } public static void main(String[] args) { Klmap m = new Klmap(); m.put("kl", new W("yc")); m.put("kls", new W("sd")); W w = (W) m.get("kl"); System.out.println(w.name); } } class SxtEntry { Object key; Object value; public SxtEntry(Object key, Object value) { super(); this.key = key; this.value = value; } } class W{ String name; public W(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
通过key "kl"来获取得到“yc”
标签:oid map lct private remove cep 集合类 不能 element
原文地址:http://www.cnblogs.com/klstime/p/7282660.html