当前位置:   article > 正文

数据结构:顺序表(java版本带迭代器)_顺序表和iterator的内存图

顺序表和iterator的内存图
  1. package Unit3表栈队列;
  2. import java.util.Iterator;
  3. import java.util.NoSuchElementException;
  4. import java.util.jar.JarException;
  5. import java.util.Scanner;
  6. //import com.sun.tools.javac.code.Type.ForAll;
  7. public class MyArrayList<T>{
  8. private final int max_size=10;
  9. private int thesize;//记录当前数组元素的个数
  10. private T[]arr;//记录数组元素
  11. public MyArrayList() {
  12. thesize=0;
  13. doClear();
  14. // System.out.println(thesize);
  15. }//新建MyArrayList对象
  16. private void doClear() {
  17. thesize=0;//
  18. ensuresize(max_size);
  19. }
  20. public void clear() {
  21. doClear();
  22. }
  23. public int size() {
  24. return thesize;//返回当前数组元素个数
  25. }
  26. public boolean isEmpty() {
  27. return size()==0;//判断是否为空
  28. }
  29. public T get(int idx) {
  30. if(idx<0||idx>=thesize)
  31. throw new ArrayIndexOutOfBoundsException();
  32. return arr[idx];
  33. }
  34. public T set(T newval,int idx) {
  35. if(idx<0||idx>=thesize)
  36. throw new ArrayIndexOutOfBoundsException();//抛出数组越界异常
  37. T old=arr[idx];
  38. arr[idx]=newval;
  39. return old;
  40. }
  41. public void trimsize()
  42. {
  43. ensuresize(size());//将MyArrayList的大小改为thesize
  44. }
  45. public void add(T newval) {
  46. if(arr.length==size()) {
  47. ensuresize(2*size()+1);
  48. }
  49. arr[thesize++]=newval;
  50. }
  51. public void add(int idx,T newval) {
  52. // System.out.println(thesize);
  53. if(arr.length==size()) {
  54. ensuresize(2*size()+1);
  55. for(int i=thesize;i>idx;i--)
  56. arr[i]=arr[i-1];
  57. arr[idx]=newval;
  58. thesize++;
  59. System.out.println(thesize+"--------");
  60. }
  61. }
  62. public void ensuresize(int newsize) {
  63. if(newsize<thesize)
  64. return;
  65. T []old=arr;
  66. arr=(T [])new Object[newsize];
  67. for(int i=0;i<size();i++)
  68. arr[i]=old[i];
  69. // thesize=newsize;
  70. }
  71. public T remove(int idx) {
  72. T old=arr[idx];
  73. for(int i=idx;i<thesize-1;i++)
  74. arr[i]=arr[i+1];
  75. thesize--;
  76. return old;
  77. }
  78. public Iterator iterator(){
  79. return new MyArrayListIterator();
  80. }
  81. private class MyArrayListIterator implements Iterator{
  82. private int current=0;
  83. public boolean hasNext() {
  84. if(current<size())
  85. return true;
  86. return false;
  87. }
  88. public T next() {
  89. if(!hasNext()) {
  90. throw new NoSuchElementException();
  91. }
  92. return arr[current++];
  93. }
  94. public void remove() {
  95. MyArrayList.this.remove(--current);
  96. }
  97. }
  98. public static void main(String []args) {
  99. Scanner cin=new Scanner(System.in);
  100. MyArrayList<Integer>arrayList=new MyArrayList<Integer>();
  101. for(int i=0;i<5;i++) {
  102. arrayList.add(cin.nextInt());
  103. }
  104. Iterator it=arrayList.iterator();
  105. while(it.hasNext())
  106. System.out.println(it.next());
  107. System.out.println(arrayList.size());
  108. arrayList.remove(0);
  109. it=arrayList.iterator();
  110. while(it.hasNext())
  111. System.out.println(it.next());
  112. //for(Integer i:arrayList)
  113. // System.out.println(i);
  114. }
  115. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/674280
推荐阅读
相关标签
  

闽ICP备14008679号