• Java实现顺序表


    前言:

    🎈数据结构是一门逻辑非常严谨的学科,当我们实际处理一些数据时,要考虑各种会出现的问题。比如在顺序表中增加数据时,首先要考虑容量够不够用,增加的位置是否合理,等等一些问题,然后才是具体增加数据。

    🎈数据在内存中存储分为顺序存储(数组)和链式存储(链表),顺序存储就是在内存中分配一块连续的空间来存储一些数据。顺序表就是实现了操作这块内存数据的一些方法。

    代码实现(具体方法类)

    1. public class ArrayList {
    2. private int[] arr;
    3. private int usedSize;
    4. private static final int DEFAULT_SIZE = 10;
    5. public int getUsedSize() {
    6. return this.usedSize;
    7. }
    8. public ArrayList() {
    9. this.arr = new int[DEFAULT_SIZE];
    10. }
    11. public void add(int val) {
    12. //检测容量,不够则扩容
    13. if(getUsedSize() == arr.length) {
    14. arr = Arrays.copyOf(this.arr, this.arr.length * 2);
    15. }
    16. this.arr[usedSize] = val;
    17. this.usedSize++;
    18. }
    19. public void add(int val, int pos) throws IndexExpection{
    20. //检测pos位置是否合法
    21. if(pos < 0 || pos > getUsedSize()) {
    22. throw new IndexExpection("下标异常");
    23. }
    24. //检测容量
    25. if(getUsedSize() == arr.length) {
    26. this.arr = Arrays.copyOf(this.arr, this.arr.length * 2);
    27. }
    28. for(int i = getUsedSize() - 1; i >= pos; i--) {
    29. this.arr[i + 1] = this.arr[i];
    30. }
    31. this.arr[pos] = val;
    32. this.usedSize++;
    33. }
    34. public int indexOf(int val) throws EmptyExpection{
    35. if(isEmpty()) {
    36. throw new EmptyExpection("空异常");
    37. }
    38. for(int i = 0; i < getUsedSize(); i++) {
    39. if(this.arr[i] == val) {
    40. return i;
    41. }
    42. }
    43. return -1;
    44. }
    45. public void remove(int val) throws EmptyExpection{
    46. //判空
    47. if(isEmpty()) {
    48. throw new EmptyExpection("空异常");
    49. }
    50. int index = indexOf(val);
    51. if(index == -1) {
    52. System.out.println("没有这个数据");
    53. return;
    54. }
    55. for(int i = index; i < getUsedSize() - 1; i++) {
    56. this.arr[i] = this.arr[i + 1];
    57. }
    58. this.usedSize--;
    59. }
    60. public void clear() throws EmptyExpection{
    61. if(isEmpty()) {
    62. throw new EmptyExpection("空异常");
    63. }
    64. this.usedSize = 0;
    65. }
    66. public boolean contains(int val) throws EmptyExpection{
    67. if(isEmpty()) {
    68. throw new EmptyExpection("空异常");
    69. }
    70. for(int i = 0; i < getUsedSize(); i++) {
    71. if(this.arr[i] == val) {
    72. return true;
    73. }
    74. }
    75. return false;
    76. }
    77. public void set(int pos, int val) throws IndexExpection, EmptyExpection{
    78. if(isEmpty()) {
    79. throw new EmptyExpection("空异常");
    80. }
    81. if(pos < 0 || pos >= getUsedSize()) {
    82. throw new IndexExpection("下标异常");
    83. }
    84. this.arr[pos] = val;
    85. }
    86. public int get(int pos) throws IndexExpection, EmptyExpection{
    87. if(isEmpty()) {
    88. throw new EmptyExpection("空异常");
    89. }
    90. if(pos < 0 || pos >= getUsedSize()) {
    91. throw new IndexExpection("下标异常");
    92. }
    93. return this.arr[pos];
    94. }
    95. public boolean isEmpty() {
    96. return getUsedSize() == 0;
    97. }
    98. public void display() throws EmptyExpection{
    99. if(isEmpty()) {
    100. throw new EmptyExpection("空异常");
    101. }
    102. for(int i = 0; i < getUsedSize(); i++) {
    103. System.out.print(this.arr[i] + " ");
    104. }
    105. }
    106. }

    自定义异常代码实现

    1. public class EmptyExpection extends RuntimeException{
    2. public EmptyExpection() {
    3. }
    4. public EmptyExpection(String message) {
    5. super(message);
    6. }
    7. }
    1. public class IndexExpection extends RuntimeException{
    2. public IndexExpection() {
    3. }
    4. public IndexExpection(String message) {
    5. super(message);
    6. }
    7. }

    测试模块

    1. public class Main {
    2. public static void main(String[] args) {
    3. ArrayList arrayList = new ArrayList();
    4. arrayList.add(1);
    5. arrayList.add(2);
    6. arrayList.add(3);
    7. try {
    8. arrayList.add(10,0);
    9. int ret = arrayList.indexOf(11);
    10. arrayList.remove(10);
    11. arrayList.set(0,10);
    12. int ret2 = arrayList.get(0);
    13. arrayList.display();
    14. }catch (EmptyExpection e) {
    15. e.printStackTrace();
    16. }catch (IndexExpection e) {
    17. e.printStackTrace();
    18. }
    19. }
    20. }

    小结:

    🐵我们需大量思考一些问题,锻炼自己这种严谨的逻辑思维,相信会有不一样的收获。

  • 相关阅读:
    利用AI快速跨过新手区:用DevChat编写Python程序-CSV导入TDengine
    影响关系分类汇总
    Webrtc丢包率的计算
    项目整个管理论文之2
    从金鸡百花电影节,看“鼓浪屿元宇宙”的元力、魅力与想象力
    Android ConstraintLayout app:layout_constraintHorizontal_weight
    ESXI存储设备已经分区,无法创建数据存储。
    ASP.NET Core微服务(六)——【redis命令详细列表1】
    048:vue+openlayers鼠标click显示企业名片(代码示例)
    RoBERTa:A Robustly Optimized BERT Pretraining Approach
  • 原文地址:https://blog.csdn.net/weixin_62353436/article/details/126809159