2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
We are going to talkSequence table, we have to talk about linear lists first, because sequential lists are a type of linear lists.
As the name suggests, a linear table is a table that organizes data like a line.
You can understand it by associating it with queuing in real life.
Characteristics of linear lists:
The sequence table is aPhysical address contiguousThe storage units are like people standing in line.
A linear structure that stores data elements sequentially, usually usingArraysstorage.
Complete the addition, deletion, query and modification of data in the array.
Implement a sequence table (storing int type data) by yourself, and use the sequence table as a class. We implement some interfaces, namely someMember MethodsTo realize the addition, deletion, query and modification of data.
public class SeqList {
private int[] elem;
private int usedSize;
// 默认构造方法
SeqList(){ }
// 将顺序表的底层容量设置为initcapacity
SeqList(int initcapacity){ }
// 新增元素,默认在数组最后新增
public void add(int data) { }
// 在 pos 位置新增元素
public void add(int pos, int data) { }
// 判定是否包含某个元素
public boolean contains(int toFind) { return true; }
// 查找某个元素对应的位置
public int indexOf(int toFind) { return -1; }
// 获取 pos 位置的元素
public int get(int pos) { return -1; }
// 给 pos 位置的元素设为 value
public void set(int pos, int value) { }
//删除第一次出现的关键字key
public void remove(int toRemove) { }
// 获取顺序表长度
public int size() { return 0; }
// 清空顺序表
public void clear() { }
}
By default, we initially open up 10 spaces for data.
private static final int DEFAULT_SIZE = 10;
public SeqList() {
this.elem= new int[DEFAULT_SIZE];
}
Pass in the array length and use the array length to allocate space.
private static final int DEFAULT_SIZE = 10;
public SeqList(int initcapacity) {
this.elem= new int[initcapacity];
}
Add new elements, which are added at the end of the array by default.
Points to consider:
public void add(int data) {
if(isFull()){
elem = Arrays.copyOf(elem,elem.length * 2);
}
elem[usedSize++] = data;
}
/**
* 判断当前的顺序表是不是满的!
*
* @return true:满 false代表空
*/
private boolean isFull() {
return usedSize == elem.length;
}
Insert element at index pos.
Precautions:
private boolean checkPosInAdd(int pos) throws PosIllegalException{
if(pos < 0 || pos > usedSize){
throw new PosIllegalException("位置不合法");
}
return true;
}
// 在 pos 位置新增元素
public void add(int pos, int data) {
try{
if(checkPosInAdd(pos)){
if(isFull()){
elem = Arrays.copyOf(elem,elem.length * 2);
}
for (int i = usedSize; i > pos ; i--) {
elem[i] = elem[i-1];
}
elem[pos] = data;
usedSize++;
}
}catch(PosIllegalException e){
e.printStackTrace();
}
}
Check whether the given data is contained in the sequence table.
Just loop through it and return true if found, otherwise return false.
public boolean contains(int toFind) {
for (int i = 0; i < usedSize; i++) {
if(elem[i] == toFind){
return true;
}
}
return false;
}
Look at the subscripts of the given data in this sequence table.
Just loop through it and find the return index, if not, return -1.
public int indexOf(int toFind) {
for (int i = 0; i < usedSize; i++) {
if(elem[i] == toFind){
return i;
}
}
return -1;
}
Output the element at position pos.
Precautions:
public int get(int pos) {
try{
if(checkPosInAdd(pos)){
return elem[pos];
}
}catch(PosIllegalException e){
e.printStackTrace();
}
return 0;
}
Modify the pos position element.
Precautions:
public void set(int pos, int value) {
try{
if(checkPosInAdd(pos)){
elem[pos] = value;
}
}catch(PosIllegalException e){
e.printStackTrace();
}
}
Delete the first occurrence of key.
Precautions:
public void remove(int key) {
for (int i = 0; i < usedSize; i++) {
if(elem[i] == key){
for (int j = i; j < usedSize - 1; j++) {
elem[j] = elem[j + 1];
}
usedSize--;
}
}
}
The length of the sequence table is obtained, which refers to the number of useful data, that is, usedSize.
public int size() {
return usedSize;
}
Set the contents of the sequence table to empty.
Precautions:
public void clear() {
for (int i = 0; i < usedSize; i++) {
elem[i] = 0;
}
usedSize = 0;
}
In Java, the ArrayList class is provided to represent sequential lists.
Interface Description:
Java provides 3 construction methods as shown in the table:
method | Method usage introduction |
---|---|
ArrayList() | No parameter construction |
ArrayList(Collection<? extends E> c) | Constructing ArrayList from other Collections |
ArrayList(int initialCapacity) | Specify the initial capacity of the sequence table |
The common methods provided are similar to what we implemented above.
The advantages of sequential lists are as follows:
Links to the exercises are below:
Pascal's Triangle