手动实现 容器
Posted zbgghost
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手动实现 容器相关的知识,希望对你有一定的参考价值。
1 package cn.xlf.myarraylist; 2 3 import java.util.ArrayList; 4 5 /** 6 * 模拟实现 JDK中提供的ArrayList类 7 * @author ZBG34854 8 * 9 */ 10 public class MyArrayList { 11 /** 12 * The value is used for object storage. 13 */ 14 private Object[] value; 15 16 /** 17 * The count is the number of object used. 18 */ 19 private int size; 20 //初始化 21 public MyArrayList(){ 22 this(10); // 用一个构造器 调另一个构造器 23 //value = new Object[10]; 24 } 25 public MyArrayList(int size){ 26 value = new Object[size]; 27 } 28 29 public int size(){//返回 数组的 元素个数 30 return size; 31 } 32 //赋值 33 public void add(Object obj){ 34 value[size] = obj; 35 size++; 36 //数组自动扩容 37 if(size >= value.length){ 38 //扩容 39 int newCapacity = value.length * 2; 40 Object[] newList = new Object[newCapacity]; 41 for(int i = 0; i < value.length; i++){ 42 newList[i] = value[i]; 43 } 44 45 value = newList; 46 } 47 } 48 49 public Object get(int index){ 50 rangeCheck(index); 51 return value[index]; 52 } 53 54 public int indexOf(Object obj){ 55 if(obj == null ){ 56 return -1; 57 }else{ 58 for(int i = 0;i<value.length;i++){ 59 if(obj==value[i]){ 60 return i; 61 } 62 } 63 return -1; 64 } 65 } 66 //倒着按索引求值 67 public int lastIndexOf(Object obj){ 68 if(obj == null ){ 69 return -1; 70 }else{ 71 for(int i = value.length-1;i>=0;i--){ 72 if(obj==value[i]){ 73 return i; 74 } 75 } 76 return -1; 77 } 78 } 79 80 81 public void rangeCheck(int index){ 82 if(index < 0 || index > size - 1){ 83 try{ 84 throw new Exception();//手动抛出异常 85 }catch(Exception e){ 86 e.printStackTrace(); 87 } 88 } 89 } 90 91 public Object set(int index, Object object){ 92 rangeCheck( index); 93 Object old = value[index]; 94 value[index] = object; 95 return old; 96 } 97 98 public static void main(String[] args) { 99 MyArrayList list = new MyArrayList(2); 100 list.add("aaa"); 101 list.add(new Human("ghost")); 102 list.add("ddddd"); 103 list.add("ddddd"); 104 list.add("ddddd"); 105 list.add("ddddd"); 106 list.add("ddddd"); 107 ArrayList list2; 108 109 Human h = (Human)list.get(1); 110 System.out.println(h.getName()); 111 112 System.out.println(list.get(0)); 113 114 System.out.println(list.get(2)); 115 116 System.out.println(list.size()); 117 } 118 119 120 121 122 }
HUMAN 方法
package cn.xlf.myarraylist; public class Human { private String name; public Human(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package cn.xlf.myarraylist;
import java.util.ArrayList;
/** * 模拟实现 JDK中提供的ArrayList类 * @author ZBG34854 * */public class MyArrayList { /** * The value is used for object storage. */ private Object[] value;
/** * The count is the number of object used. */ private int size; //初始化 public MyArrayList(){ this(10); // 用一个构造器 调另一个构造器 //value = new Object[10]; } public MyArrayList(int size){ value = new Object[size]; } public int size(){ return size; } //赋值 public void add(Object obj){ value[size] = obj; size++; //数组自动扩容 if(size >= value.length){ //扩容 int newCapacity = value.length * 2; Object[] newList = new Object[newCapacity]; for(int i = 0; i < value.length; i++){ newList[i] = value[i]; } value = newList; } } public Object get(int index){ rangeCheck(index); return value[index]; } public int indexOf(Object obj){ if(obj == null ){ return -1; }else{ for(int i = 0;i<value.length;i++){ if(obj==value[i]){ return i; } } return -1; } } //倒着按索引求值 public int lastIndexOf(Object obj){ if(obj == null ){ return -1; }else{ for(int i = value.length-1;i>=0;i--){ if(obj==value[i]){ return i; } } return -1; } } public void rangeCheck(int index){ if(index < 0 || index > size - 1){ try{ throw new Exception();//手动抛出异常 }catch(Exception e){ e.printStackTrace(); } } } public Object set(int index, Object object){ rangeCheck( index); Object old = value[index]; value[index] = object; return old; } public static void main(String[] args) {MyArrayList list = new MyArrayList(2);list.add("aaa");list.add(new Human("ghost"));list.add("ddddd");list.add("ddddd");list.add("ddddd");list.add("ddddd");list.add("ddddd");ArrayList list2;Human h = (Human)list.get(1);System.out.println(h.getName());System.out.println(list.get(0));System.out.println(list.get(2));System.out.println(list.size());}
}
以上是关于手动实现 容器的主要内容,如果未能解决你的问题,请参考以下文章