zoukankan      html  css  js  c++  java
  • 通过对List接口的实现类ArrayList的常见方法的应用

      

    public class MyArrayList {

           private Object[] elementData;

           private int size;

      

           public int size() {

                  return size;

           }

      

           public boolean isEmpty() {

                  return size == 0;

           }

      

           public MyArrayList() {

                  this(10);

           }

      

           public MyArrayList(int initialCapacity) {

                  if (initialCapacity < 0) {

                         try {

                                throw new Exception();

                         catch (Exception e) {

                                e.printStackTrace();

                         }

                  }

                  elementData = new Object[initialCapacity];

           }

      

           public void add(Object obj) {

      

                  // 数组扩容

                  if (size == elementData.length) {

                         Object[] newArray = new Object[size * 2 1];

                         System.arraycopy(elementData, 0, newArray, 0, elementData.length);

                         /*

                          * for (int i = 0; i < elementData.length; i++) {

                          * newArray[i]=elementData[i]; }

                          */

                         elementData = newArray;

                  }

                  elementData[size++] = obj;

                  // size++;

           }

      

           public void add(int index, Object obj) {

                  rangeCheck(index);

                  ensureCapacity();//数组扩容

                  

                  System.arraycopy(elementData, index, elementData, index + 1, size - index);

                  elementData[index] = obj;

                  size++;

           }

      

           private void ensureCapacity() {

                  // 数组扩容

                  if (size == elementData.length) {

                         Object[] newArray = new Object[size * 2 + 1];

                         System.arraycopy(elementData, 0, newArray, 0, elementData.length);

                         /*

                          * for (int i = 0; i < elementData.length; i++) {

                          * newArray[i]=elementData[i]; }

                          */

                         elementData = newArray;

                  }

      

           }

      

           public Object get(int index) {

                  rangeCheck(index);

                  return elementData[index];

           }

      

           public void remove(int index) {

                  rangeCheck(index);

                  // 删除指定位置的对象

                  // a b c d e

                  if (index < 0 || index >= size) {

                         try {

                                throw new Exception();

                         catch (Exception e) {

                                e.printStackTrace();

                         }

                         int numMoved = size - index - 1;

                         if (numMoved > 0) {

                                System.arraycopy(elementData,

                         index + 1, elementData, index, numMoved);

                         }

                         elementData[--size] = null;

                  }

           }

      

           public void remove(Object obj) {

                  for (int i = 0; i < size; i++) {

                         if (get(i).equals(obj)) {// 注意:底层调用的equals方法,不是==;

                                remove(i);

                         }

                  }

           }

      

           public Object set(int index, Object obj) {

                  rangeCheck(index);

                  Object oldValue = elementData[index];

                  elementData[index] = obj;

                  return oldValue;

           }

      

           private void rangeCheck(int index) {

                  if (index < 0 || index >= size) {

                         try {

                                throw new Exception();

                         catch (Exception e) {

                                e.printStackTrace();

                         }

                  }

           }

      

           public static void main(String[] args) {

                  MyArrayList slist = new MyArrayList(3);

                  slist.add(132);

                  slist.add("444");

                  slist.add(5);

                  slist.add("333");

                  slist.add("333");

                  slist.add("abc");

                  slist.add("ccc");

                  System.out.println(slist.size);

                  System.out.println(slist.get(6));

           }

  • 相关阅读:
    正试图在 os 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起。问题的解决方法!
    ArcGIS10 图框生成和批量打印工具V5.0正式发布
    c# winform未能找到引用的组件“Excel”的解决办法
    图框工具5.0 for ArcGIS10正式发布
    python UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 2: ordinal not in range(128)错误解决办法
    ArcGIS地图文档MXD效率慢的一点建议(转)
    ArcGISEngine实现图层输出kml
    云南云测采用本人开发所有权辅助建库软件,获得好评
    瑞星发力高端企业级市场 连续中标政府大单 狼人:
    臭名昭著的十种Web恶意攻击软件 狼人:
  • 原文地址:https://www.cnblogs.com/pigdata/p/10305644.html
Copyright © 2011-2022 走看看