01-封装属于自己的泛型数组

package com.demo;

public class TArray<T> {

    private T[] data;
    private int size;

    @SuppressWarnings("unchecked")
    public TArray(int capacity) {
        data = (T[]) new Object [capacity];
        size = 0;
    }

    public TArray() {
        this(10);
    }

    // 获取数组容量
    public int getCapacity() {
        return data.length;
    }

    // 获取数组中元素个数
    public int getSize() {
        return size;
    }

    // 判断数组是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    // 在数组末尾追加元素
    public void addLast(T e) {
        add(size, e);
    }

    // 在数组开头添加元素
    public void addFirst(T e) {
        add(0, e);
    }

    // 在数组中间插入元素
    public void add(int index, T e) {

        if (size == data.length)
            throw new IllegalArgumentException("add fail,array id full");
        if (index < 0 || index > size)
            throw new IllegalArgumentException("add fail, need index > 0 and index <=size");
        for (int i = size - 1; i >= index; i--)
            data[i + 1] = data[i];
        data[index] = e;
        size++;
    }

    // 根据引脚获取值
    public T get(int index) {

        if (index < 0 || index >= size)
            throw new IllegalArgumentException("get fail , index < 0 or index >= size");
        return data[index];
    }

    // 根据引脚设置值
    public void set(int index, T e) {

        if (index < 0 || index >= size)
            throw new IllegalArgumentException("get fail , index < 0 or index >= size");
        data[index] = e;
    }

    // 是否包含某个元素
    public boolean contains(T e) {
        
        for (int i = 0; i < size; i++) {
            if(e.equals(data[i])){
                return true;
            }
        }
        return false;
    }
    
    //查找某个元素对应的位置
    public int find(T e){
        
        for(int i = 0 ; i < size ; i++){
            if(e.equals(data[i])){
                return i;
            }
        }
        return -1;
    }
    
    //删除index对应的元素 返回删除的元素
    public T remove(int index){
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("reomve fail , illegal args");
        }
        T ret = data[index];
        for(int i = index+1 ; i < size ; i++){
            data[i-1] = data[i];
        }
        size--;
        return ret;
    }
    
    //删除第一个元素
    public T removeFirst(){
        return remove(0);
    }
    
    //删除最后一个元素
    public T removeLast(){
        return remove(size-1);
    }

    //根据元素删除数组中对应的元素  并返回删除的值
    public boolean removeElement(T e){
        boolean flag = true;
        int index = find(e);
        try {
            remove(index);
        } catch (Exception e2) {
            flag = false;
        }
        return flag;
    }
    
    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder();
        sb.append(String.format("Array: size = %d , capacity = %d \n", size, data.length));
        sb.append('[');
        for (int i = 0; i < size; i++) {
            sb.append(data[i]);
            if (i != size - 1) {
                sb.append(',');
            }
        }
        sb.append(']');
        return sb.toString();
    }

}

 对数组进行扩容和减容

package com.demo;

public class TArray<T> {

    private T[] data;
    private int size;  //data数组中 实际元素个数

    @SuppressWarnings("unchecked")
    public TArray(int capacity) {
        data = (T[]) new Object [capacity];
        size = 0;
    }

    public TArray() {
        this(10);
    }

    // 获取数组容量
    public int getCapacity() {
        return data.length;
    }

    // 获取数组中元素个数
    public int getSize() {
        return size;
    }

    // 判断数组是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    // 在数组末尾追加元素
    public void addLast(T e) {
        add(size, e);
    }

    // 在数组开头添加元素
    public void addFirst(T e) {
        add(0, e);
    }

    // 在数组中间插入元素
    public void add(int index, T e) {

        if (index < 0 || index > size)
            throw new IllegalArgumentException("add fail, need index > 0 and index <=size");
        if (size == data.length)
            //扩容数组
            resize(2*data.length);
        for (int i = size - 1; i >= index; i--)
            data[i + 1] = data[i];
        data[index] = e;
        size++;
    }

    @SuppressWarnings("unchecked")
    private void resize(int length) {
        T [] newData = (T[]) new Object [length];
        for(int i = 0 ; i < size ; i++){
            newData[i] = data[i];
        }
        data = newData;
    }

    // 根据引脚获取值
    public T get(int index) {

        if (index < 0 || index >= size)
            throw new IllegalArgumentException("get fail , index < 0 or index >= size");
        return data[index];
    }

    // 根据引脚设置值
    public void set(int index, T e) {

        if (index < 0 || index >= size)
            throw new IllegalArgumentException("get fail , index < 0 or index >= size");
        data[index] = e;
    }

    // 是否包含某个元素
    public boolean contains(T e) {
        
        for (int i = 0; i < size; i++) {
            if(e.equals(data[i])){
                return true;
            }
        }
        return false;
    }
    
    //查找某个元素对应的位置
    public int find(T e){
        
        for(int i = 0 ; i < size ; i++){
            if(e.equals(data[i])){
                return i;
            }
        }
        return -1;
    }
    
    //删除index对应的元素 返回删除的元素
    public T remove(int index){
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("reomve fail , illegal args");
        }
        
        T ret = data[index];
        for(int i = index+1 ; i < size ; i++){
            data[i-1] = data[i];
        }
        size--;
        //删减容量
        if(size == data.length/2){
            resize(size);
        }
        return ret;
    }
    
    //删除第一个元素
    public T removeFirst(){
        return remove(0);
    }
    
    //删除最后一个元素
    public T removeLast(){
        return remove(size-1);
    }

    //根据元素删除数组中对应的元素  并返回删除的值
    public boolean removeElement(T e){
        boolean flag = true;
        int index = find(e);
        try {
            remove(index);
        } catch (Exception e2) {
            flag = false;
        }
        return flag;
    }
    
    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder();
        sb.append(String.format("Array: size = %d , capacity = %d \n", size, data.length));
        sb.append('[');
        for (int i = 0; i < size; i++) {
            sb.append(data[i]);
            if (i != size - 1) {
                sb.append(',');
            }
        }
        sb.append(']');
        return sb.toString();
    }

}

测试代码

package com.demo;

public class Demo {
    public static void main(String[] args) {
        TArray<Integer> tArray = new TArray<>(10);
        for(int i = 0 ; i < tArray.getCapacity() ; i++){
            tArray.add(i, i);
        }
        System.out.println(tArray);
        tArray.add(2, 345);
        System.out.println(tArray);
        tArray.remove(3);
        System.out.println(tArray);
    }
}

输出结果

Array: size = 10 , capacity = 10 
[0,1,2,3,4,5,6,7,8,9]
Array: size = 11 , capacity = 20 
[0,1,345,2,3,4,5,6,7,8,9]
Array: size = 10 , capacity = 10 
[0,1,345,3,4,5,6,7,8,9]

猜你喜欢

转载自www.cnblogs.com/wangjije/p/11233418.html