1. import java.util.Arrays;
    2. class StaticSeq {
    3. public int Maxsize;
    4. int[] elem = new int[Maxsize];//数据域
    5. public int size;//有效元素个数
    6. public StaticSeq(int capacity) {
    7. this.Maxsize = capacity;//构造方法,用来确定静态顺序表的容量
    8. }
    9. }
    10. class DynamicSeq {
    11. public int[] elem;//数据域引用
    12. public int size;//有效元素个数
    13. }
    14. public class SeqList {
    15. public int[] elem;
    16. public int size;
    17. public SeqList() {
    18. this.initSeqList();
    19. }
    20. //初始化顺序表
    21. public void initSeqList(){
    22. this.elem = new int[10];
    23. }
    24. // 打印顺序表
    25. public void display() {
    26. for (int i = 0; i < this.size; i++) {
    27. System.out.print(this.elem[i] + " ");
    28. }
    29. System.out.println();
    30. }
    31. // 在 pos 位置新增元素
    32. public void add(int pos, int data) {
    33. if (this.elem == null) {
    34. this.initSeqList();
    35. }
    36. if (pos < 0 || pos > this.size) {
    37. System.out.println("插入位置非法!");
    38. return;
    39. }
    40. if (isFull()) {
    41. this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length);
    42. }
    43. for (int i = size - 1; i >= pos; i--) {
    44. this.elem[i+1] = this.elem[i];
    45. }
    46. this.elem[pos] = data;
    47. this.size++;
    48. }
    49. public boolean isFull() {
    50. return this.size == this.elem.length;
    51. }
    52. public boolean isEmpty() {
    53. return this.size == 0;
    54. }
    55. // 判定是否包含某个元素
    56. public boolean contains(int toFind) {
    57. for (int j : this.elem) {
    58. if (j == toFind) {
    59. return true;
    60. }
    61. }
    62. return false;
    63. }
    64. // 查找某个元素对应的位置
    65. public int search(int toFind) {
    66. for (int i = 0; i < this.size; i++) {
    67. if (this.elem[i] == toFind) {
    68. return i;
    69. }
    70. }
    71. return -1;
    72. }
    73. //顺序插入元素
    74. public void seqAdd(int value) {
    75. if (this.elem == null) {
    76. this.initSeqList();
    77. }
    78. if (this.isFull()) {
    79. this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length);
    80. }
    81. this.elem[this.size] = value;
    82. this.size++;
    83. }
    84. // 获取 pos 位置的元素
    85. public int getPos(int pos) {
    86. if (pos < 0 || pos >= this.size) {
    87. System.out.println("位置非法!");
    88. return -1;
    89. }
    90. return this.elem[pos];
    91. }
    92. // 给 pos 位置的元素设为 value
    93. public void setPos(int pos, int value) {
    94. if (pos < 0 || pos >= this.size) {
    95. System.out.println("修改位置非法!");
    96. return;
    97. }
    98. this.elem[pos] = value;
    99. }
    100. //删除第一次出现的关键字key
    101. public void remove(int toRemove) {
    102. if (isEmpty()) {
    103. System.out.println("顺序表为空!");
    104. return;
    105. }
    106. for (int i = 0; i < this.size; i++) {
    107. if (this.elem[i] == toRemove) {
    108. for (int j = i; j < this.size - 1; j++) {
    109. this.elem[j] = this.elem[j+1];
    110. }
    111. this.size--;
    112. return;
    113. }
    114. }
    115. System.out.println("未找到!");
    116. }
    117. // 获取顺序表长度
    118. public int size() {
    119. return this.size;
    120. }
    121. // 清空顺序表
    122. public void clear() {
    123. this.size = 0;
    124. }
    125. //
    126. //清空顺序表
    127. public void clear() {
    128. this.usedSize = 0;//简单类型
    129. //如果是引用数据类型,就要将所有的数据都要置为空
    130. /*for (int i = 0; i < usedSize; i++) {
    131. this.elem[i] == null;
    132. }
    133. this.usedSize = 0;
    134. */
    135. }
    136. }
    1. public class MyArrayList{
    2. public int[] elem;
    3. public int usedSize;//表示当前有效数据个数
    4. public MyArrayList(){
    5. this.elem = new int[10];
    6. }
    7. //打印顺序表
    8. public void display() {
    9. for (int i = 0; i < this.usedSize; i++) {
    10. System.out.print(this.elem[i] +" ");
    11. }
    12. System.out.println();
    13. }
    14. //获取顺序表长度
    15. public int size() {
    16. return this.usedSize;
    17. }
    18. //在pos位置新增元素
    19. public void add(int pos, int data){
    20. if(pos < 0 || pos > usedSize){
    21. System.out.println("pos位置不合法");
    22. return;
    23. }
    24. if(isFull()){
    25. this.elem = Arrays.copyOf(this.elem,this.elem.length * 2);
    26. }
    27. for(int i = this.usedSize - 1;i >= pos;i--){
    28. this.elem[i+1] = this.elem[i];
    29. }
    30. this.elem[pos] = data;
    31. this.usedSize++;
    32. }
    33. public boolean isFull(){
    34. return this.usedSize == this.elem.length;
    35. }
    36. //判定是否包含某个元素
    37. public boolean contains(int toFind) {
    38. for (int i = 0; i < this.usedSize; i++) {
    39. if (this.elem[i] == toFind){
    40. return true;
    41. }
    42. }
    43. return false;
    44. }
    45. //查找某个元素对应的位置
    46. public int search(int toFind) {
    47. for (int i = 0; i < this.usedSize; i++) {
    48. if (this.elem[i] == toFind){
    49. return i;
    50. }
    51. }
    52. return -1;
    53. }
    54. //获取pos位置的元素
    55. public int getPos(int pos) {
    56. if (pos < 0 || pos >= this.usedSize){
    57. System.out.println("pos位置不合法");
    58. return -1;
    59. }
    60. if (isEmpty()){
    61. System.out.println("顺序表为空");
    62. return -1;
    63. }
    64. return this.elem[pos];
    65. }
    66. public boolean isEmpty(){
    67. return this.usedSize == 0;
    68. }
    69. //给pos位置的元素设为/更新 value
    70. public void setPos(int pos, int value) {
    71. if (pos < 0 || pos >= this.usedSize){
    72. System.out.println("pos位置不合法");
    73. return;
    74. }
    75. if (isEmpty()){
    76. System.out.println("顺序表为空");
    77. return;
    78. }
    79. this.elem[pos] = value;
    80. }
    81. //删除第一次出现的关键字key
    82. public void remove(int toRemove) {
    83. if (isEmpty()){
    84. System.out.println("顺序表为空");
    85. return;
    86. }
    87. int index = search(toRemove);
    88. if (index == -1){
    89. System.out.println("没有要删除的数字");
    90. return;
    91. }
    92. for (int i = index; i < this.usedSize - 1; i++) {
    93. this.elem[i] = this.elem[i + 1];
    94. }
    95. this.usedSize--;
    96. }
    97. //清空顺序表
    98. public void clear() {
    99. this.usedSize = 0;//简单类型
    100. //如果是引用数据类型,就要将所有的数据都要置为空
    101. /*for (int i = 0; i < usedSize; i++) {
    102. this.elem[i] == null;
    103. }
    104. this.usedSize = 0;
    105. */
    106. }