A linear list is a finite sequence of n data elements with the same characteristics. Linear table is a data structure that is widely used in practice. Common linear tables are: sequence list, linked list, stack, queue, string... Linear table is logically a linear structure, that is to say, it is 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.
A sequence table uses a storage unit with a continuous physical address to store data in sequence The linear structure of elements is generally stored in an array. Complete the addition, deletion, checking and modification of data on the array.
is actually an array. Then why do you need to write a sequence table? Wouldn't it be better to just use an array? The difference is that writing it in a class can be object-oriented.
Sequence tables can generally be divided into:
Static sequence table: Use fixed-length array storage
Dynamic sequence table: Use dynamically opened array storage
Static sequence table is suitable for scenarios where you know how much data needs to be stored.
The fixed-length array of the static sequence table causes N to be too large , it is a waste to open too much space, and it is not enough to open too little.
In contrast, the dynamic sequence table is more flexible, and the space size can be dynamically allocated according to the needs.
public class MyArrayList { public int[] elem;//数组 public int usedSize;//数据的有效个数 public MyArrayList(){ this.elem = new int[10]; } }
//打印顺序表 public void display(){ for (int i = 0; i < this.usedSize; i++) { System.out.print(this.elem[i] + " "); } System.out.println(); }
//获取顺序表长度 public int size(){ return this.usedSize; }
When inserting an element into the sequence table, the inserted position must be in front of where the element is stored
//在 pos 位置新填元素 public void add(int pos,int data){ if(pos < 0 || pos >usedSize){ System.out.println("pos 位置不合法!"); return; } if(isfull()) { Arrays.copyOf(this.elem,2*this.elem.length); } for (int i = this.usedSize - 1; i >= pos; i--) { this.elem[i + 1] = this.elem[i]; } this.elem[pos] = data; this.usedSize++; } //判断是否满 public boolean isfull(){ return this.usedSize == this.elem.length; }
//判断是否包含某个元素 public boolean contains(int toFind){ for (int i = 0; i < this.usedSize; i++) { if(this.elem[i] == toFind){ return true; } } return false; }
//查找某个元素的对应位置,找不到返回-1 public int search(int toFind){ for (int i = 0; i < this.usedSize; i++) { if(this.elem[i] == toFind){ return i; } } return -1; }
//获取pos位置的值 public int getPos(int pos){ if(pos < 0 || pos >= this.usedSize){ System.out.println("pos 位置不合法"); return -1;//这里说明一下,业务上的处理,不考虑 } if(isEmpty()){ System.out.println("顺序表为空!"); return -1; } return this.elem[pos]; } public boolean isEmpty(){ return this.usedSize == 0; }
//给pos位置元素更新value public void setPos(int pos,int value){ if (pos < 0 || pos >= this.usedSize){ System.out.println("pos 位置不合法"); return; } if(isEmpty()){ System.out.println("顺序表为空!"); return; } this.elem[pos] = value; }
//删除第一次出现的关键字key public void remove(int toRmove){ if (isEmpty()){ System.out.println("顺序表为空!"); return; } int index = search(toRmove); if(index == -1){ System.out.println("没有你要删除的数字!"); return; } for (int i = index; i < this.usedSize - 1; i++) { this.elem[i] = this.elem[i+1]; } this.usedSize--; //this.elem[useSize] = null;如果数组当中是引用数据类型 }
//清空顺序表 public void clear(){ this.usedSize = 0; }
import java.util.Arrays; public class MyArrayList { public int[] elem; public int usedSize; public MyArrayList(){ this.elem = new int[10]; } //打印顺序表 public void display(){ for (int i = 0; i < this.usedSize; i++) { System.out.print(this.elem[i] + " "); } System.out.println(); } //获取顺序表长度 public int size(){ return this.usedSize; } //在 pos 位置新填元素 public void add(int pos,int data){ if(pos < 0 || pos >usedSize){ System.out.println("pos 位置不合法!"); return; } if(isfull()) { Arrays.copyOf(this.elem,2*this.elem.length); } for (int i = this.usedSize - 1; i >= pos; i--) { this.elem[i + 1] = this.elem[i]; } this.elem[pos] = data; this.usedSize++; } //判断是否满 public boolean isfull(){ return this.usedSize == this.elem.length; } //判断是否包含某个元素 public boolean contains(int toFind){ for (int i = 0; i < this.usedSize; i++) { if(this.elem[i] == toFind){ return true; } } return false; } //查找某个元素的对应位置,找不到返回-1 public int search(int toFind){ for (int i = 0; i < this.usedSize; i++) { if(this.elem[i] == toFind){ return i; } } return -1; } //获取pos位置的值 public int getPos(int pos){ if(pos < 0 || pos >= this.usedSize){ System.out.println("pos 位置不合法"); return -1;//这里说明一下,业务上的处理,不考虑 } if(isEmpty()){ System.out.println("顺序表为空!"); return -1; } return this.elem[pos]; } public boolean isEmpty(){ return this.usedSize == 0; } //给pos位置元素更新value public void setPos(int pos,int value){ if (pos < 0 || pos >= this.usedSize){ System.out.println("pos 位置不合法"); return; } if(isEmpty()){ System.out.println("顺序表为空!"); return; } this.elem[pos] = value; } //删除第一次出现的关键字key public void remove(int toRmove){ if (isEmpty()){ System.out.println("顺序表为空!"); return; } int index = search(toRmove); if(index == -1){ System.out.println("没有你要删除的数字!"); return; } for (int i = index; i < this.usedSize - 1; i++) { this.elem[i] = this.elem[i+1]; } this.usedSize--; //this.elem[useSize] = null;如果数组当中是引用数据类型 } //清空顺序表 public void clear(){ this.usedSize = 0; } }
public class Test { public static void main(String[] args) { MyArrayList myArrayList = new MyArrayList(); myArrayList.add(0,1); myArrayList.add(1,2); myArrayList.add(2,3); myArrayList.add(3,4); myArrayList.add(4,5); myArrayList.display(); System.out.println(myArrayList.contains(3)); System.out.println(myArrayList.getPos(3)); myArrayList.setPos(0,99); myArrayList.display(); } }
The above is the detailed content of How to implement sequential table data structure using Java?. For more information, please follow other related articles on the PHP Chinese website!