一、第一中实现方式
package cn.zxg.collection;
/**
* 自定义实现ArraList,体会底层原理
*/
public class SxtArraList {
private int size;
private Object[] elementData;
private static final int DEFALT_CAPACITY=10;
public SxtArraList(){
elementData=new Object[DEFALT_CAPACITY];
}
public SxtArraList(int capacity){
elementData=new Object[capacity];
}
public void add(Object obj){
elementData[size++]=obj;
}
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append("[");
for(int i=0;i<size;i++){
sb.append(elementData[i]+",");
}
sb.setCharAt(sb.length()-1,']');
return sb.toString();
}
public static void main(String[] args) {
SxtArraList s1=new SxtArraList(20);
s1.add("a");
s1.add("b");
System.out.println(s1);
}
}
第二、增加扩容
package cn.zxg.collection;
/**
* 自定义实现ArraList,体会底层原理
*/
public class SxtArraList <E>{
private int size;
private Object[] elementData;
private static final int DEFALT_CAPACITY=10;
public SxtArraList(){
elementData=new Object[DEFALT_CAPACITY];
}
public SxtArraList(int capacity){
elementData=new Object[capacity];
}
public void add(E element){
//什么时候扩容
if(size==elementData.length){
//怎么扩容,10+10/2,要把(elementData.length>>1) 因为+的优先级比>>1高
Object[] newArray=new Object[elementData.length+(elementData.length>>1)];
System.arraycopy(elementData,0,newArray,0,elementData.length);
elementData=newArray;
}
elementData[size++]=element;
}
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append("[");
for(int i=0;i<size;i++){
sb.append(elementData[i]+",");
}
sb.setCharAt(sb.length()-1,']');
return sb.toString();
}
public static void main(String[] args) {
SxtArraList s1=new SxtArraList(20);
for(int i=0;i<40;i++){
s1.add("小白");
}
System.out.println(s1);
}
}
第三版、增加get和set方法、对索引合法判断
package cn.zxg.collection;
/**
* 自定义实现ArraList,体会底层原理
* 1、增加get和set方法
* 2、
*/
public class SxtArraList <E>{
private int size;
private Object[] elementData;
private static final int DEFALT_CAPACITY=10;
public SxtArraList(){
elementData=new Object[DEFALT_CAPACITY];
}
public SxtArraList(int capacity){
if(capacity<0){
throw new RuntimeException("容器容量不能为负");
}else if(capacity==0){
elementData=new Object[DEFALT_CAPACITY];
}
else {
elementData=new Object[capacity];
}
}
public void add(E element){
//什么时候扩容
if(size==elementData.length){
//怎么扩容,10+10/2,要把(elementData.length>>1) 因为+的优先级比>>1高
Object[] newArray=new Object[elementData.length+(elementData.length>>1)];
System.arraycopy(elementData,0,newArray,0,elementData.length);
elementData=newArray;
}
elementData[size++]=element;
}
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append("[");
for(int i=0;i<size;i++){
sb.append(elementData[i]+",");
}
sb.setCharAt(sb.length()-1,']');
return sb.toString();
}
public E get(int index){
checkRange(index);
return (E)elementData[index];
}
public void set(E element,int index){
checkRange(index);
elementData[index]=element;
}
public void checkRange(int index){
//所有合法的判断[0,size)
if(index<0||index>size-1){
throw new RuntimeException("索引越界了"+index);
}
}
public static void main(String[] args) {
SxtArraList s1=new SxtArraList(20);
for(int i=0;i<40;i++){
s1.add("小白");
}
System.out.println(s1);
s1.set("小黑",1);
System.out.println(s1);
System.out.println(s1.get(10));
System.out.println(s1.get(-1));
}
}
第四版、增加remove、size等方法
package cn.zxg.collection;
/**
* 自定义实现ArraList,体会底层原理
* 1、增加get和set方法,索引合法的判断
* 2、增加remove方法
*/
public class SxtArraList <E>{
private int size;
private Object[] elementData;
private static final int DEFALT_CAPACITY=10;
public SxtArraList(){
elementData=new Object[DEFALT_CAPACITY];
}
public SxtArraList(int capacity){
if(capacity<0){
throw new RuntimeException("容器容量不能为负");
}else if(capacity==0){
elementData=new Object[DEFALT_CAPACITY];
}
else {
elementData=new Object[capacity];
}
}
public void add(E element){
//什么时候扩容
if(size==elementData.length){
//怎么扩容,10+10/2,要把(elementData.length>>1) 因为+的优先级比>>1高
Object[] newArray=new Object[elementData.length+(elementData.length>>1)];
System.arraycopy(elementData,0,newArray,0,elementData.length);
elementData=newArray;
}
elementData[size++]=element;
}
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append("[");
for(int i=0;i<size;i++){
sb.append(elementData[i]+",");
}
sb.setCharAt(sb.length()-1,']');
return sb.toString();
}
public E get(int index){
checkRange(index);
return (E)elementData[index];
}
public void set(E element,int index){
checkRange(index);
elementData[index]=element;
}
public void checkRange(int index){
//所有合法的判断[0,size)
if(index<0||index>size-1){
throw new RuntimeException("索引越界了"+index);
}
}
public void remove(E element){
for(int i=0;i<size;i++){
if(element.equals(get(i))){
remove(i);
}
}
}
public void remove(int index){
//a,b,c,d,e,fg
//a,b,c,e,f,g,h
int numMoved=elementData.length-index-1;
if(numMoved>0){
System.arraycopy(elementData,index+1,elementData,index,numMoved);
}
elementData[--size]=null;
}
//获取容器大小
public int size(){
return size;
}
//判断容器是否为空
public boolean isEmpty(){
return size==0?true:false;
}
public static void main(String[] args) {
SxtArraList s1=new SxtArraList(20);
for(int i=0;i<40;i++){
s1.add("小白"+i);
}
System.out.println(s1);
s1.set("小黑",1);
System.out.println(s1);
System.out.println(s1.get(10));
// System.out.println(s1.get(-1));
s1.remove(3);
s1.remove("小白");
System.out.println(s1);
}
}