Java: The difference between Iterable() and Iterator() + traversal of the sequence table, changing the capacity

Learning algorithms and data structures encountered:

If I want to use hasNext() and Next(), I need to implement the interface Iterable() first, and then I can call these two Iterator methods

But I'm thinking that implementing Iterator can also use these two methods ()

Reason: With Iterable(), each call returns an iterator from the beginning.

With Iterator(), its iterator depends on the current position of hasNext() and Next(), passed between different methods, and the position of Next() becomes agnostic.

Checked the generic type: formulate the data type of the operation as a parameter.

The generic method can receive parameters of different types, and then the generic will not exist after compilation.

//用泛型(将操作的数据定义为一个参数),接收不同的数据类型
public class SequenceListDemo<T> implements Iterable<T>{
}
constructor, application space
public SequenceListDemo(int Capacity) {
        this.List = (T[])new Object[Capacity];
        N = 0;
    }
How to define a sequence of operations table
//置空
    public void clear(){
        N = 0;
        //List[0] = null;
    }
    //得到长度
    public int getLength(){
        return (N);
    }
    //判断是否为空
    public boolean isNull(){
        return N==0;
    }
    //添加,从尾部
    public void Add(T t){
        List[N++] = t;
        System.out.println("元素"+t+"已加入");
    }
    //位置i,添加元素t
    public int add_locai(int i, T t){//添加前判断:是否满,位置是否合理
        if (i== List.length){
            System.out.println("插入失败,已满");
            return -1;
        }
        if (i<0 || i>N){
            System.out.println("插入位置错误");
            return -1;
        }
        //从i位置开始元素,往后移
        for (int j=List.length;j>i;j--){
            List[j] = List[j-1];
        }
        List[i] = t;
        N++;
        return 0;
    }
    //删除制定位置i的,最后返回该元素
    public T dele_locai(int i){
        if (i<0 || i>N){
            System.out.println("位置不合理(不在 [0,N))");
        }
        T dele_i;
        dele_i = List[i];  //取出位置i的
        for (int j=i; j<List.length; j++){
            List[j] = List[j+1];
        }
        N--;
        return dele_i;
    }
    //查看元素t第一次出现的位置i
    public int SearFirPlaOf_t(T t){
        int location;
        for (int j=0; j<=N; j++){
            if (List[j]==t){
                location = j;
                return location;
            }// System.out.println("List["+j+"]:"+List[j]+" ");
        }
        return -1;
    }
    //打印所有元素
    public void Pri_List(){
        for (int List_i=0; List_i<=N; List_i++){
            System.out.println("List["+List_i+"]:"+List[List_i]+" ");
        }
    }

final test

class Test{
    public static void main(String[] args) {
        SequenceListDemo<Integer> List = new SequenceListDemo<>(20);//创建顺序表,容量10,类型Integer
        List.Add(1);
        List.Add(10);
        List.Add(1548);
        List.Add(50499);
        List.Add(3);

        List.Pri_List();
        List.clear();
        System.out.println("----------");
        List.Pri_List();
    }
}

result:

 So I added a line to the delete method

List[0] = null;

For the improvement of the sequence table, the judgment of the element capacity can be added after the delete operation and the add operation.

After deletion, the length becomes smaller. If it is less than 1/4 of the capacity, reduce the capacity and the capacity becomes 1/2 of the original

After adding elements, the length increases. If the length is greater than 1/2 of the capacity, increase the capacity and double the capacity.

Although this can improve the shortcomings of some sequence tables occupying fixed memory, even if the capacity is increased or decreased after each calculation, the memory will always be occupied and not used. And every time the memory is changed, resources are also used to create an auxiliary collection.

So I think this improvement is rather tasteless, but the idea can be learned.

code show as below:

  //添加,从尾部
    public void Add(T t){
        List[N++] = t;
        System.out.println("元素"+t+"已加入");
        if (N>List.length*1/2){
            setCapacity(List.length*2);
        }
        System.out.println(
                List.length
        );
    }
    //位置i,添加元素t,添加后大于1/2,扩容
    public int add_locai(int i, T t){//添加前判断:是否满,位置是否合理
        if (i== List.length){
            System.out.println("插入失败,已满");
            return -1;
        }
        if (i<0 || i>N){
            System.out.println("插入位置错误");
            return -1;
        }
        //从i位置开始元素,往后移
        for (int j=List.length;j>i;j--){
            List[j] = List[j-1];
        }
        List[i] = t;
        N++;
        if (N>List.length*1/2){
            setCapacity(List.length*2);
        }
        return 0;
    }
    //删除制定位置i的,最后返回该元素,发现删除之后小于总容量的1/4,减容
    public T dele_locai(int i){
        if (i<0 || i>N){
            System.out.println("位置不合理(不在 [0,N))");
        }
        T dele_i;
        dele_i = List[i];  //取出位置i的
        for (int j=i; j<N; j++){
            List[j] = List[j+1];
        }
        N--;
        if(N>0&&N<(int)((double)List.length/4)){
            setCapacity(1/2 * List.length);
        }
        return dele_i;
    }

 

Guess you like

Origin blog.csdn.net/qq_54508596/article/details/126236710