个人笔记:数据结构-线性表(数组实现)一

1.作为一名菜鸟级别的程序?,数据结构必学的东东。今天闲着没事干,来学习回顾一下。用Java实现该数据结构,我觉得是非常方便的。在这做一下笔记。
写一下博客,来记录一下自己学习的轨迹,之后在回顾起来,也是蛮有感觉的。
package com.jun;
import java.util.Arrays;

/**
 *
 * 用数组实现线性表
 * @author jun
 * @time 2018/10/12
 */
public class ArrList<T> {

    //用于存放数据域,默认为10,在构造器中初始化
    private Object[] objects;
    //线性表大小,-1表示为空
    private int size = -1;

    public ArrList(){
        objects = new Object[10];
    }

    public ArrList(int initCapcity){
        objects= new Object[initCapcity];
    }

    //添加元素
    public boolean add(T t){
            this.grow();
            objects[++size] = t;
            return true;
    }

    //删除元素,最后面一个
    public boolean remove(){
//        System.arraycopy(objects,0,objects,0,size-1);
//        size--;

        objects[size--] = null;
        return true;
    }
    //删除特定的元素,必须是正数
    public boolean removeAt(int i){
        if(i>=0){
            //删除的是尾部
            if(i==size){
                remove();
                return true;
            }
            //删除的是头部
            if(i==0){
                System.arraycopy(objects,1,objects,0,size);
                size--;
                return true;
            }
            System.arraycopy(objects,i+1,objects,i,size-i);
            size--;
            return true;
        }
        return false;
    }
    //插入到某个值之后
    public boolean addAt(int i,T t){
        if(i!=-1){
            this.grow();//扩容
            //进行数组的挪动,i后面的都移动到后面,也可以用for循环来移动
            System.arraycopy(objects,i+1,objects,i+2,size-i);
            objects[i+1] = t;
            size++;
            return true;
        }
         if(i<0&&i>=size){
             throw new ArrayIndexOutOfBoundsException();
         }
        return false;
    }

    //查找某个元素值,要注意元素为空的时候
    public int search(T t){
        if(t==null){
            for(int i =0;i<=size;i++){
                if(objects[i]==null){
                    return i;
                }
            }
        }else{
            Object object = (Object) t;
            for(int i =0;i<objects.length;i++){
                if(objects[i]==object||objects[i].equals(object)){
                    return i;
                }
            }
        }
        return -1;
    }

    //获取线性表的某个对象
    public T get(int i){
        if(i<0&&i>=size){
            throw new ArrayIndexOutOfBoundsException("下标越界");
        }
         Object o = objects[i];
         if(o==null){
             return null;
         }
         return (T) o;
    }

    //获取线性表的大小,-1表示为空
    public int getSize(){
        return size+1;
    }

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

    //用于扩容处理
    public void grow(){
        if(size==objects.length){
            Object[] objects1s = new Object[size+(size<<1)/2];//扩容为原来的一半,与ArrayList集合一样的机制
            System.arraycopy(objects,0,objects1s,0,objects.length);
            objects = objects1s;
        }

    }

    //判断两个线性表是否包含相等
    @Override
    public boolean equals(Object obj) {
        ArrList arrList = (ArrList)obj;
        int size1 = arrList.getSize();
        for(int i =0;i<size1;i++){
            if(this.get(i)!=arrList.get(i)||!this.get(i).equals(arrList.get(i))){
                return false;
            }
        }
        return true;
    }


    @Override
    public String toString() {
//        Object[] os = Arrays.copyOf(objects,size);
        StringBuffer sb = new StringBuffer();
        for(int i = 0;i<=size;i++){
            if(this.get(i)==null){
                sb.append("null, ");
            }else {
                sb.append(this.get(i).toString()+", ");
            }
        }
        String string = sb.toString();
        string = string.substring(0,string.length()-2);
        return "ArrList{ "+ string+
                " }";
    }

}

自己捣鼓捣鼓,多敲才是王道。?
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值