风也温柔

计算机科学知识库

java怎么实现链表数据结构 如何使用Java实现顺序表数据结构?

  前言

  线性表( list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构java怎么实现链表数据结构 如何使用Java实现顺序表数据结构?,常见 的线性表:顺序表、链表、栈、队列、字符串&; 线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储 时,通常以数组和链式结构的形式存储。

  Java数据结构之顺序表如何实现

  一、顺序表1.1 什么是顺序表

  顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改 。

  其实就是一个数组。那为什么还要写一个顺序表,直接用数组不就好了?不一样的java怎么实现链表数据结构,写到类里面就可以面向对象。

  顺序表一般可以分为:

  静态顺序表适用于确定知道需要存多少数据的场景.

  静态顺序表的定长数组导致N定大了,空间开多了浪费,开少了不够用.

  相比之下动态顺序表更灵活, 根据需要动态的分配空间大小.

  二、简单实现顺序表2.1 创建顺序表

  <pre class="brush:java;">public class MyArrayList {
public int[] elem;//数组
public int usedSize;//数据的有效个数

public MyArrayList(){

   this.elem = new int[10];

}
}</pre>

  登录后复制

  2.2 打印顺序表

  <pre class="brush:java;">//打印顺序表
public void display(){

    for (int i = 0; i < this.usedSize; i++) {
        System.out.print(this.elem[i] + " ");
    }
    System.out.println();
}</pre>

  登录后复制

  2.3 获取顺序表长度

  <pre class="brush:java;">//获取顺序表长度

public int size(){
    return this.usedSize;
</pre>

  登录后复制

  2.4 在 pos 位置新增元素

  在顺序表里面插入元素的时候所插入的位置的前面一定是存放了元素的

  <pre class="brush:java;">//在 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;
}</pre>

  登录后复制

  2.5 判定是否包含某个元素

  <pre class="brush:java;">//判断是否包含某个元素
public boolean contains(int toFind){

    for (int i = 0; i < this.usedSize; i++) {
        if(this.elem[i] == toFind){
            return true;
        }
    }
    return false;
}</pre>

  登录后复制

  2.6 查找某个元素对应的位置

  <pre class="brush:java;">//查找某个元素的对应位置,找不到返回-1

public int search(int toFind){
    for (int i = 0; i < this.usedSize; i++) {
        if(this.elem[i] == toFind){
            return i;
        }
    }
    return -1;
}</pre>

  登录后复制

  2.7 获取 pos 位置的元素

  <pre class="brush:java;">//获取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;
}</pre>

  登录后复制

  2.8 给 pos 位置的元素设为 value

  <pre class="brush:java;"> //给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;
}</pre>

  登录后复制

  2.9 删除你想要删除的元素

  <pre class="brush:java;">//删除第一次出现的关键字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;如果数组当中是引用数据类型
}</pre>

  登录后复制

  2.10 清空顺序表

  <pre class="brush:java;">//清空顺序表

public void clear(){
    this.usedSize = 0;
}</pre>

  登录后复制

  三、.java

  <pre class="brush:java;">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;
}
re>

  登录后复制

  四、Test.java

  <pre class="brush:java;">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();
}
re>

  登录后复制

  以上就是如何使用Java实现顺序表数据结构?的详细内容,更多请关注php中文网其它相关文章!

  声明:本文转载于:亿速云java怎么实现链表数据结构,如有侵犯,请联系删除

  文章来源:https://www.php.cn/java-article-523995.html