【数据结构】:顺序表

 顺序表的增删改查:

class MyArrayListIndexOutOfException extends RuntimeException {
    public MyArrayListIndexOutOfException(String messge) {
        super(messge);
    }
}
public class MyArrayList {
    private String[] data=null;
    private int size=0;
    private int capccity=100;
    public MyArrayList(){
        data=new String[capccity];
    }
    public void realloc(){
        capccity*=2;
        String[] newdata=new String[capccity];
        //将元数据拷贝进新数组
        for(int i=0;i<data.length;i++){
            newdata[i]=data[i];
        }
        data=newdata;
    }
    //1.尾插
    public void add(String elem){
        if(size>=capccity){
            //先扩容
            realloc();
        }
        data[size]=elem;
        size++;
    }
    //2.把元素插入到中间的任意位置
    public void add(int index,String elem){
        if(index<0||index>size){
            System.out.println("越界");
            return;
        }
        if(size>=capccity){
            realloc();
        }
        // 需要把 index 位置的元素, 依次往后搬运,
        // 给 index 位置腾出一个空闲空间, 来放置 elem
        for(int i=size-1;i>=index;i--){
            data[i+1]=data[i];
        }
        data[index]=elem;
        size++;
    }
    // 3. 按照下标位置删除元素, 这个方法的返回结果就是被删掉的元素
    public String remove(int index) {
        if(index<0||index>size){
            System.out.println("下标非法");
            return null;
        }
        String result=data[index];
        for(int i=index;i<size;i--){
            data[i]=data[i+1];
        }
        size--;
        return result;
    }
    // 4. 按照元素的值来删除元素, 这个方法返回成功失败.
    public boolean remove(String elem) {
        int index=0;
        for(;index<size;index++){
            if (data[index].equals(elem)){
                break;
            }
        }
        if(index>=size){
            System.out.println("没有此元素");
            return false;
        }
        for(int i=index;i<size-1;i++){
            data[i]=data[i+1];
        }
        size--;
        return true;
    }
    // 5. 根据下标获取元素
    public String get(int index) {
        if(index<0||index>=size){
            System.out.println("下标非法");
            throw new MyArrayListIndexOutOfException("下标越界:index:"+index);
        }
        return data[index];
    }
    // 6. 根据下标修改元素
    public void set(int index, String elem) {
        if (index<0||index>=size){
            System.out.println("下标非法");
            throw new MyArrayListIndexOutOfException("下标越界:index:"+index);
        }
    }
    // 7. 判断元素是否存在
    public boolean contains(String elem) {
        for(int i=0;i<size;i++){
            if(data[i].equals(elem)){
                return true;
            }
        }
        return false;
    }
    // 8. 查找元素位置
    public int indexOf(String elem) {
        for(int i=0;i<size;i++){
            if(data[i].equals(elem)){
              return i;
            }
        }
        return -1;
    }
    // 9. 查找元素位置(从后往前找)
    public int lastIndexOf(String elem) {
        for (int i = size - 1; i >= 0; i--) {
            if (data[i].equals(elem)) {
                return i;
            }
        }
        return -1;
    }
    public void clear() {
        size = 0;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public String toString() {
       StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("[");
        for(int i=0;i<size;i++){
            stringBuilder.append(data[i]);
            if(i<size-1){
                stringBuilder.append(",");
            }
        }
        stringBuilder.append(']');
        return stringBuilder.toString();
    }
    private static void testAdd(){
        MyArrayList myArrayList=new MyArrayList();
        myArrayList.add("红楼梦");
        myArrayList.add("三国演义");
        myArrayList.add("水浒传");
        myArrayList.add("西游记");
        System.out.println(myArrayList);
        myArrayList.add(2,"雪山飞狐");
        System.out.println(myArrayList);
    }
    private static void testRemove(){
        MyArrayList myArrayList=new MyArrayList();
        myArrayList.add("红楼梦");
        myArrayList.add("三国演义");
        myArrayList.add("水浒传");
        myArrayList.add("西游记");
        System.out.println(myArrayList);
        myArrayList.remove(0);
        System.out.println(myArrayList);
        myArrayList.remove("水浒传");
        System.out.println(myArrayList);
    }
    private static void testGetAndSet() {
        MyArrayList myArrayList=new MyArrayList();
        myArrayList.add("红楼梦");
        myArrayList.add("三国演义");
        myArrayList.add("水浒传");
        myArrayList.add("西游记");
        System.out.println(myArrayList);
        System.out.println(myArrayList.get(2));
        myArrayList.set(2,"儒林外史");
        System.out.println(myArrayList);
    }
    private static void testContainsAndIndexOf() {
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.add("红楼梦");
        myArrayList.add("三国演义");
        myArrayList.add("水浒传");
        myArrayList.add("红楼梦");
        myArrayList.add("西游记");
        System.out.println(myArrayList);
        System.out.println(myArrayList.contains("红楼梦"));

        System.out.println(myArrayList.indexOf("红楼梦"));

        System.out.println(myArrayList.lastIndexOf("红楼梦"));
    }
    private static void testSizeEmptyClear() {
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.add("红楼梦");
        myArrayList.add("三国演义");
        myArrayList.add("水浒传");
        myArrayList.add("红楼梦");
        myArrayList.add("西游记");
        System.out.println(myArrayList);
        System.out.println(myArrayList.size());
        System.out.println(myArrayList.isEmpty());
        myArrayList.clear();
        System.out.println(myArrayList.size());
        System.out.println(myArrayList.isEmpty());
    }

    public static void main(String[] args) {
//        testRemove();
//        testGetAndSet();
//        testContainsAndIndexOf();
        testSizeEmptyClear();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_44292334/article/details/113659215