当前位置:   article > 正文

java类库ArrayList的基本实现_java arraylist库

java arraylist库
基本实现,具体的请看JDK源码
  1. import java.util.Iterator;
  2. /**
  3. * 类名:MyArrayList
  4. * 说明:和ArrayList源码基本差不多
  5. */
  6. public class MyArrayList<AnyType> implements Iterable<AnyType>{
  7. private static final int DEFAULT_CAPACITY = 10;
  8. private AnyType[] theItems;
  9. private int theSize;
  10. public MyArrayList(){
  11. clear();
  12. }
  13. public void clear(){
  14. theSize = 0;
  15. ensureCapacity(DEFAULT_CAPACITY);
  16. }
  17. public int size(){
  18. return theSize;
  19. }
  20. public void trimToSize(){
  21. ensureCapacity(theSize);
  22. }
  23. public boolean isEmpty(){
  24. return theSize == 0;
  25. }
  26. public AnyType get(int index){
  27. if(index < 0||index >= theSize)
  28. throw new ArrayIndexOutOfBoundsException();
  29. return theItems[index];
  30. }
  31. public AnyType set(int index,AnyType newVal){
  32. if(index < 0||index >= theSize)
  33. throw new ArrayIndexOutOfBoundsException();
  34. AnyType old = theItems[index];
  35. theItems[index] = newVal;
  36. return old;
  37. }
  38. public void add(int index,AnyType x){
  39. if(theItems.length == index)
  40. ensureCapacity(2 * size() + 1);
  41. for(int i = theSize; i > index; i--)
  42. theItems[i] = theItems[i - 1];
  43. theItems[index] = x;
  44. theSize++;
  45. }
  46. public boolean add(AnyType x){
  47. add(theSize,x);
  48. return true;
  49. }
  50. public AnyType remove(int index){
  51. if(index < 0||index >= theSize)
  52. throw new ArrayIndexOutOfBoundsException();
  53. AnyType old = theItems[index];
  54. for(int i = index; i < theSize;i++)
  55. theItems[i] = theItems[i+1];
  56. theSize--;
  57. return old;
  58. }
  59. public void ensureCapacity(int newCapacity){
  60. if(newCapacity < theSize)
  61. return;
  62. AnyType[] old = theItems;
  63. theItems = (AnyType[]) new Object[newCapacity];
  64. for(int i = 0;i < theSize;i++)
  65. theItems[i] = old[i];
  66. }
  67. public Iterator<AnyType> iterator() {
  68. // TODO Auto-generated method stub
  69. return new Iterator(){
  70. private int current = 0;
  71. @Override
  72. public boolean hasNext() {
  73. return current < theSize;
  74. }
  75. @Override
  76. public AnyType next() {
  77. return theItems[current++];
  78. }
  79. @Override
  80. public void remove() {
  81. MyArrayList.this.remove(--current);
  82. }
  83. };
  84. }
  85. public String toString(){
  86. String s = new String();
  87. for(int i = 0;i < theSize;i++)
  88. s += get(i)+" ";
  89. return s;
  90. }
  91. /**
  92. * 方法名:MyArrayList.java
  93. * 说明:
  94. */
  95. public static void main(String[] args) {
  96. // TODO Auto-generated method stub
  97. MyArrayList<Integer> list = new MyArrayList<Integer>();
  98. list.add(1);
  99. list.add(2);
  100. list.add(3);
  101. list.add(4);
  102. list.add(5);
  103. System.out.println(list);
  104. list.remove(1);
  105. System.out.println(list);
  106. list.set(1,2);
  107. System.out.println(list);
  108. Iterator ite = list.iterator();
  109. while(ite.hasNext()){
  110. System.out.println(ite.next()+" ");
  111. }
  112. }
  113. }

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

闽ICP备14008679号