[Java] Implement basic operations of sequence tables (data structure)


Preface

Before understanding the sequential list, we must first understand what a linear list is. A linear list (linear list) is a finite sequence of n data elements with the same characteristics. Linear tables are a data structure widely used in practice. Common linear tables: sequential lists, linked lists, stacks, queues...
Linear tables are logically linear structures, that is to say A continuous straight line. However, the physical structure is not necessarily continuous. When linear tables are physically stored, they are usually stored in the form of arrays and linked structures


Sequence table

The sequence table is a linear structure that uses a section ofstorage units with consecutive physical addresses to store data elements in sequence. Generally, array storage is used. Complete the addition, deletion, checking and modification of data on the array
Insert image description here
Next we need to implement some methods to add, delete, check and modify the array

Create a class:

public class MyArrayList {
    
    
    //数组
    public int[] elem;
    //数组中的元素个数
    public int usedSize;
    //当前数组默认的容量
    public static final int DEFAULT_CAPACITY = 5;
    public MyArrayList() {
    
    
        elem = new int[DEFAULT_CAPACITY];
    }
}    

1. Print sequence table

public void display() {
    
    
    for (int i = 0; i < usedSize; i++) {
    
    
        System.out.print(elem[i]+" ");
    }
    System.out.println();
}  

2. Add elements

By default, adding elements is to add elements at the last position of the array
Before adding elements, we must first determine whether the array is full

Determine whether the array is full:

public boolean isFull() {
    
    
    return usedSize == elem.length;
}

Add elements:

public void add(int data) {
    
    
    if(isFull()) {
    
    
        //满了进行扩容
        elem = Arrays.copyOf(elem,2*elem.length);
    }
    elem[usedSize] = data;
    usedSize++;
}

test:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        MyArrayList myArray = new MyArrayList();
        myArray.add(10);
        myArray.add(20);
        myArray.add(30);
        myArray.add(40);
        myArray.display();
    }
}

Insert image description here

3. Add elements at any location

Note: When adding elements here, you must ensure that the legality of the position cannot be less than 0, nor greater than the length of the array, nor can it be inserted at intervals, that is, there must be elements in front of the inserted position; when inserting at the same time, the remaining elements must Move backward; if it is illegal, throw an exception
Also before adding elements, we need to determine whether the array is full

Is the location legal?

private void checkPosOfAdd(int pos) {
    
    
    if(pos < 0||pos > usedSize) {
    
    
        throw new PosException("pos位置不合法:"+pos);
    }
}  

Add elements anywhere:

public void add(int pos, int data) {
    
    
    //判断位置是否合法
    checkPosOfAdd(pos);
    if(isFull()) {
    
    
        elem = Arrays.copyOf(elem,2*elem.length);
    }
    for (int i = usedSize - 1; i >= pos; i--) {
    
    
        elem[i+1] = elem[i];
    }
    elem[pos] = data;
    usedSize++;
}    

test:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        MyArrayList myArray = new MyArrayList();
        myArray.add(10);
        myArray.add(20);
        myArray.add(30);
        myArray.add(40);
        myArray.add(1,15);
        myArray.display();
    }
}

Insert image description here

4. Determine whether a certain element is included

Traverse the array to determine whether it is the same as this element:

public boolean contains(int toFind) {
    
    
    for (int i = 0; i < usedSize; i++) {
    
    
        if(elem[i] == toFind) {
    
    
            return true;
        }
    }
    return false;
}    

test:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        MyArrayList myArray = new MyArrayList();
        myArray.add(10);
        myArray.add(20);
        myArray.add(30);
        myArray.add(40);
        System.out.println(myArray.contains(20));
        System.out.println(myArray.contains(200));
    }
}

Insert image description here

5. Find the position of an element

Traverse this array to find whether it is the same as the element you are looking for. If it is the same, it will return the subscript of the element. If it is different, it will return -1:

public boolean indexOf(int toFind) {
    
    
    for (int i = 0; i < usedSize; i++) {
    
    
        if(elem[i] == toFind) {
    
    
            return i;
        }
    }
    return -1;
}  

test:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        MyArrayList myArray = new MyArrayList();
        myArray.add(10);
        myArray.add(20);
        myArray.add(30);
        myArray.add(40);
        System.out.println(myArray.indexOf(20));
        System.out.println(myArray.indexOf(200));
    }
}

Insert image description here

6. Get elements at any position

Similarly, we need to determine whether the position is legal and whether the sequence table is empty. When both conditions are legal, the element at the position is returned.

Whether the sequence table is empty:

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

Get an element at any position:

public int get(int pos) {
    
    
    //判断该位置是否合法
    checkPosOfAdd(pos);
    if(isEmpty()) {
    
    
        throw new EmptyException("顺序表为空");
    }
    return elem[pos];
}    

test:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        MyArrayList myArray = new MyArrayList();
        myArray.add(10);
        myArray.add(20);
        myArray.add(30);
        myArray.add(40);
        System.out.println(myArray.get(1));
    }
}

Insert image description here

7. Set the element at any position to value

The same method as getting an element at any position is used to determine whether the position is legal and whether the sequence table is empty.

Set the element at any position to value:

public void set(int pos, int value) {
    
    
    //判断位置是否合法
    checkPosOfAdd(pos);
    if(isEmpty()) {
    
    
        	throw  new EmptyException("顺序表为空");
    }
    this.elem[pos] = value;
}    

test:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        MyArrayList myArray = new MyArrayList();
        myArray.add(10);
        myArray.add(20);
        myArray.add(30);
        myArray.add(40);
        myArray.set(1,15);
        myArray.display();
    }
}

Insert image description here

8. Delete the first occurrence of the keyword

When performing a deletion operation, you must determine whether the sequence table is empty, find the subscript of the element to be deleted, and finally
move the data

Delete operation:

 public void remove(int toRemove) {
    
    
     if(isEmpty()) {
    
    
         throw new EmptyException("顺序表为空");
     }
     int ret = indexOf(toRemove);
     for (int i = ret; i < usedSize; i++) {
    
    
         elem[i] = elem[i+1];
     }
     usedSize--;
}    

test:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        MyArrayList myArray = new MyArrayList();
        myArray.add(10);
        myArray.add(20);
        myArray.add(30);
        myArray.add(40);
        myArray.remove(10);
        myArray.display();
    }
}

Insert image description here

9. Get the sequence table length

public int size() {
    
    
    return usedSize;
}    

test:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        MyArrayList myArray = new MyArrayList();
        myArray.add(10);
        myArray.add(20);
        myArray.add(30);
        myArray.add(40);
        System.out.println(myArray.size());
    }
}

Insert image description here

10. Clear the sequence list

public void clear() {
    
    
    usedSize = 0;
}    

test:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        MyArrayList myArray = new MyArrayList();
        myArray.add(10);
        myArray.add(20);
        myArray.add(30);
        myArray.add(40);
        myArray.display();
        System.out.println("*******");
        myArray.clear();
        myArray.display();
    }
}

Insert image description here

Traversal method of sequence table Arraylist

public class Test {
    
    
    public static void main(String[] args) {
    
    
        ArrayList<Integer> list = new ArrayList<>();
        list.add(12);
        list.add(23);
        list.add(34);
        list.add(45);
        //直接打印
        System.out.println(list);
        //for循坏遍历
        for (int i = 0; i < list.size(); i++) {
    
    
            System.out.print(list.get(i)+" ");
        }
        System.out.println();
        System.out.println("***********");
        //for——each遍历
        for (int x : list) {
    
    
            System.out.print(x+" ");
        }
        System.out.println();
        System.out.println("***********");
        //迭代器
        Iterator<Integer> it = list.iterator();
        while (it.hasNext()) {
    
    
            System.out.print(it.next()+" ");
        }
        System.out.println();
        System.out.println("***********");
        //从前往后打印
        ListIterator<Integer> it2 = list.listIterator();
        while (it2.hasNext()) {
    
    
            System.out.print(it2.next()+" ");
        }
        System.out.println();
        System.out.println("***********");
        //从后往前打印
        ListIterator<Integer> it3 = list.listIterator(list.size());
        while (it3.hasPrevious()) {
    
    
            System.out.print(it3.previous()+" ");
        }
        System.out.println();
    }
}

Insert image description here


Summarize

In the collection framework, ArrayList is an ordinary class that implements the List interface. The specific framework diagram is as follows:
Insert image description here
1. ArrayList is implemented in a generic manner and must be used when using it. It must be instantiated first
2. ArrayList implements the RandomAccess interface, indicating that ArrayList supports random access
3. ArrayList implements the Cloneable interface, indicating that ArrayList can be cloned< /span> 6. The bottom layer of ArrayList is a continuous space and can be dynamically expanded. It is a dynamic type sequence list CopyOnWriteArrayList 5. Unlike Vector, ArrayList is not thread-safe and can be used in a single thread. , in multi-threading, you can choose Vector or
4. ArrayList implements the Serializable interface, indicating that ArrayList supports serialization


Guess you like

Origin blog.csdn.net/2301_78373304/article/details/134822068