ArrayList01

添加方法

package com.list.mycollection;

public class SxtArrayList01 {

    private Object[]  elementData;
    private int  size;//添加时候用

    private static final int DEFALT_CAPACITY = 10 ;//默认初始值


    public SxtArrayList01(){
        elementData = new Object[DEFALT_CAPACITY];
    }

    public SxtArrayList01(int  capacity) {
        elementData  = new Object[capacity];
    }

    public  void  add(Object  obj){
        elementData[size++] = obj;
    }

    @Override
    public String toString() {

        StringBuilder  sb = new StringBuilder();

        //[a,b,c]
        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) {
        SxtArrayList01  s1 = new SxtArrayList01(20);
        s1.add("aa");
        s1.add("bb");

        System.out.println(s1);

    }


}
View Code

扩容

package com.list.mycollection;

public class SxtArrayList02<E> {

    private Object[]  elementData;
    private int  size;

    private static final int DEFALT_CAPACITY = 10 ;


    public SxtArrayList02(){
        elementData = new Object[DEFALT_CAPACITY];
    }

    public SxtArrayList02(int  capacity) {
        elementData  = new Object[capacity];
    }

    public  void  add(E  element){
        elementData[size++] = element;
    }

    @Override
    public String toString() {

        StringBuilder  sb = new StringBuilder();

        //[a,b,c]
        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) {
        SxtArrayList02  s1 = new SxtArrayList02(20);
        s1.add("aa");
        s1.add("bb");

        System.out.println(s1);


    }


}
View Code

增加数组扩容功能

package com.list.mycollection;
/**
 * 增加数组扩容功能
 * 
 *
 */
public class SxtArrayList03<E> {

    private Object[]  elementData;
    private int  size;

    private static final int DEFALT_CAPACITY = 10 ;


    public SxtArrayList03(){
        elementData = new Object[DEFALT_CAPACITY];
    }

    public SxtArrayList03(int  capacity) {
        elementData  = new Object[capacity];
    }

    public  void  add(E  element){

        //什么时候扩容??
        if(size == elementData.length){
            //扩容操作,添加一半
            Object[]  newArray  =  new Object[elementData.length+(elementData.length>>1)];  //10-->10+10/2
            System.arraycopy(elementData, 0, newArray, 0, elementData.length);
            elementData = newArray;
        }

        elementData[size++] = element;

    }

    @Override
    public String toString() {

        StringBuilder  sb = new StringBuilder();

        //[a,b,c]
        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) {
        SxtArrayList03 s1 = new SxtArrayList03(20);
        s1.add("aa");
        s1.add("bb");



        for(int a=0;a<500;a++){
            s1.add(a);
        }
        System.out.println(s1);
    }


}
View Code

set和get

package com.list.mycollection;
/**
 * 增加set和get方法
 * 增加:数组边界的检查
 * 
 *
 */
public class SxtArrayList04<E> {

    private Object[]  elementData;
    private int  size;

    private static final int DEFALT_CAPACITY = 10 ;


    public SxtArrayList04(){
        elementData = new Object[DEFALT_CAPACITY];
    }

    public SxtArrayList04(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){
            //扩容操作
            Object[]  newArray  =  new Object[elementData.length+(elementData.length>>1)];  //10-->10+10/2
            System.arraycopy(elementData, 0, newArray, 0, elementData.length);
            //从旧数组那里开始拷贝,从新数组哪里开始覆盖,覆盖多少个
            elementData = newArray;
        }

        elementData[size++] = element;
    }

    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)    10    0-9
        if(index<0||index>size-1){
            //不合法
            throw  new RuntimeException("索引不合法:"+index);
        }
    }



    @Override
    public String toString() {

        StringBuilder  sb = new StringBuilder();

        //[a,b,c]
        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) {
        SxtArrayList04  s1 = new SxtArrayList04();

        for(int i=0;i<40;i++){
            s1.add("gao"+i);
        }

        s1.set("dddd", 10);
        System.out.println(s1);
        System.out.println(s1.get(39));

    }


}
View Code

增加remove

package com.list.mycollection;
/**
 * 增加remove
 * 
 *
 */
public class SxtArrayList05<E> {

    private Object[]  elementData;
    private int  size;

    private static final int DEFALT_CAPACITY = 10 ;


    public SxtArrayList05(){
        elementData = new Object[DEFALT_CAPACITY];
    }

    public SxtArrayList05(int  capacity) {

        if(capacity<0){
            throw  new  RuntimeException("容器的容量不能为负数");
        } else if(capacity==0){
            elementData  = new Object[DEFALT_CAPACITY];
        }else{
            elementData  = new Object[capacity];
        }


    }

    public int size(){
        return size;
    }

    public  boolean isEmpty(){
        return  size==0?true:false;
    }


    public  void  add(E  element){

        //什么时候扩容??
        if(size == elementData.length){
            //扩容操作
            Object[]  newArray  =  new Object[elementData.length+(elementData.length>>1)];  //10-->10+10/2
            System.arraycopy(elementData, 0, newArray, 0, elementData.length);
            elementData = newArray;
        }

        elementData[size++] = element;
    }

    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)    10    0-9
        if(index<0||index>size-1){
            //不合法
            throw  new RuntimeException("索引不合法:"+index);
        }
    }

    public void  remove(E  element){
        //element,将它和所有元素挨个比较,获得第一个比较为true的,返回。
        for(int i=0;i<size;i++){
            if(element.equals(get(i))){   //容器中所有的比较操作,都是用的equals而不是==

                //将该元素从此处移除
                remove(i);
            }
        }
    }

    public  void  remove(int index){

        //a,b,c,d,e,f,g,h
        //a,b,c,e,f,g,h,h
        int numMoved = elementData.length-index-1;
        if(numMoved>0){
            //从哪里开始拷贝,从哪里开始覆盖,覆盖几个
            System.arraycopy(elementData, index+1, elementData, index, numMoved);
        }
         //最后一个元素设置为null,size减1
        elementData[--size] = null;

    }


    @Override
    public String toString() {

        StringBuilder  sb = new StringBuilder();

        //[a,b,c]
        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) {
        SxtArrayList05  s1 = new SxtArrayList05();

        for(int i=0;i<40;i++){
            s1.add("gao"+i);
        }

        s1.set("dddd", 10);
        System.out.println(s1);
        System.out.println(s1.get(39));

        s1.remove(3);
        s1.remove("gao11");
        System.out.println(s1);
        System.out.println(s1.size);
        System.out.println(s1.isEmpty());
    }


}
View Code
原文地址:https://www.cnblogs.com/javakangkang/p/14000711.html