Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Java to realize sequence Table

Shulou Source: shulou.com Published: 2022-06-01 12:54:31 09月15日 Update

This article is about how to use Java to implement a sequence table. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

What is a sequence table?

A sequential table is a linear table stored in a sequential storage mode, and the nodes of the linear table are stored in a set of continuous storage units of the computer in logical order.

Because the sequence table is stored in turn, as long as you know the first address of the sequence table and the storage length occupied by each data element, it is easy to calculate the location of any data element (that is, the data node).

Second, the common operation of the sequence table:

1. Create classes and construction methods

Public class MyArrayList {

Private int [] elem

Private int usedSize

Public MyArrayList () {

This.elem = new int [10]

}

Public MyArrayList (int capacity) {

This.elem = new int [capacity]

}

}

2. Capacity expansion

Please public void resize () {

This.elem = Arrays.copyOf (this.elem,2*this.elem.length)

}

3. Judge whether the sequence table is full

Please public boolean isFull () {

If (this.usedSize = = this.elem.length) {

Return true

}

Return false

}

4. Print the sequence table

Please public void display () {

For (int I = 0 position I)

< usedSize; i++) { System.out.print(elem[i]+" "); } System.out.println(); } 5、在 pos 位置新增元素 public void add(int pos, int data) { if(isFull()){ System.out.println("链表已满!"); resize(); } if(pos < 0 || pos >

This.usedSize) {

System.out.println ("invalid insertion position!")

Return

}

For (int I = usedSize-1; I > = pos;i--) {

Elem [iTun1] = elem [I]

}

Elem [pos] = data

This.usedSize++

}

6. Determine whether an element is included

Please public boolean contains (int toFind) {

For (int I = 0; I

< usedSize;i++){ if(elem[i] == toFind){ return true; } } return false; } 7、查找某个元素对应的位置 public int search(int toFind) { for(int i = 0; i < usedSize;i++){ if(elem[i] == toFind){ return i; } } return -1; } 8、获取 pos 位置的元素 public int getPos(int pos) { if(pos < 0 || pos >

= usedSize) {

System.out.println ("the pos location is invalid!")

Return-1

}

Return elem [pos]

}

9. Change the element of the pos location to value

Public void setPos (int pos, int value) {

If (pos

< 0 || pos >

= usedSize) {

System.out.println ("the pos location is invalid!")

Return

}

Elem [pos] = value

}

10. Delete the keyword Key that appears for the first time

Public void remove (int toRemove) {

Int index =-1

For (int I = 0; I < this.usedSize;i++) {

If (this.elem [I] = = toRemove) {

Index = I

}

}

If (index =-1) {

System.out.println ("element not found!")

Return

}

For (int j = index;j < this.usedSize-1;j++) {

This.elem [j] = this.elem [juni1]

}

This.usedSize--

}

11. Get the length of linked list

Public int size () {

Return this.usedSize

}

12. Clear the sequence table

Please public void clear () {

This.usedSize = 0

}

Thank you for reading! This is the end of this article on "how to use Java to achieve a sequence table". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Tags: Order location element storage data content more article linear node length good practical key keyword unit address that is common article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS Redmi Docker NVidia