当前位置:   article > 正文

Java中AyyarList类的基本实现_ayylist

ayylist
ArrayList:底层用数组实现,线程不安全,效率高。所以,查询快,插入、删除慢。一般用AyyayList就够了。
LinkedList:基于链表实现,线程不安全,效率高。所以,查询慢,插入、删除快。
Vector:基于数组实现。线程安全的,效率低。只有在多线程情况时才是用Vector。

  1. /**
  2. * 自己实现一个ArrayList,帮助我们更好地理解ArrayList类的底层结构
  3. * @author Administrator
  4. *
  5. */
  6. public class MyArrayList {
  7. private Object[] elementData;
  8. private int size;
  9. public MyArrayList(){
  10. this(10); //默认的数组长度为10
  11. }
  12. public MyArrayList(int initialCapacity){
  13. if(initialCapacity<0)
  14. try {
  15. throw new Exception();
  16. } catch (Exception e) {
  17. // TODO Auto-generated catch block
  18. e.printStackTrace();
  19. }
  20. elementData = new Object[initialCapacity];
  21. }
  22. public int size(){
  23. return size;
  24. }
  25. public void ensureCapacity(){
  26. //若数组已满,则给数组扩容
  27. if(size==elementData.length){
  28. Object[] newArray = new Object[size*2];
  29. System.arraycopy(elementData, 0, newArray, 0, elementData.length);
  30. elementData = newArray;
  31. }
  32. }
  33. public void add(Object o){
  34. ensureCapacity();
  35. elementData[size]=o;
  36. size++;
  37. }
  38. public void add(int index, Object obj){
  39. rangeCheck(index);
  40. ensureCapacity();
  41. for(int i=size-1;i>=index;i--){
  42. elementData[i+1]=elementData[i];
  43. }
  44. elementData[index]=obj;
  45. size++;
  46. }
  47. public boolean isEmpty(){
  48. return size==0;
  49. }
  50. public Object get(int index){
  51. rangeCheck(index);
  52. return elementData[index];
  53. }
  54. public Object remove(int index){
  55. rangeCheck(index);
  56. Object deletedObject = elementData[index];
  57. //int numMoved = size-index-1;
  58. //System.arraycopy(elementData, index+1, elementData, index, numMoved);
  59. for(int i=index+1; i<this.size();i++){
  60. elementData[i-1]=elementData[i];
  61. }
  62. elementData[--size]=null;
  63. return deletedObject;
  64. }
  65. public boolean remove(Object obj){
  66. for(int i=0; i<this.size();i++){
  67. if(elementData[i].equals(obj)){
  68. remove(i);
  69. return true;
  70. }
  71. }
  72. return false;
  73. }
  74. public Object set(int index, Object obj){
  75. rangeCheck(index);
  76. Object oldValue=elementData[index];
  77. elementData[index]=obj;
  78. return oldValue;
  79. }
  80. private void rangeCheck(int index){
  81. if( index < 0 ||index >= size){
  82. try {
  83. throw new Exception();
  84. } catch (Exception e) {
  85. // TODO Auto-generated catch block
  86. e.printStackTrace();
  87. }
  88. }
  89. }
  90. public static void main(String[] args) {
  91. // TODO Auto-generated method stub
  92. MyArrayList list = new MyArrayList(3);
  93. list.add(123);
  94. list.add("haha");
  95. list.add("wxxyy");
  96. list.add("you");
  97. System.out.println(list.get(3));
  98. }
  99. }



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

闽ICP备14008679号