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));

           }

  • 相关阅读:
    Vue组件库elementUI 在el-row 或 el-col 上使用@click无效失效,
    js判断客户端是手机端还是PC端
    IOS上微信在输入框弹出键盘后,页面不恢复,下方有留白,有弹窗弹出时页面内容感应区域错位
    vue打包问题:Tip: built files are meant to be served over an HTTP server.
    在vue项目npm run build后,index.html中引入css和js 报MIME type问题
    Vue项目中如何使用less(添加less依赖)
    Mac 下永久路由的添加 & Mac 校园网连接教程
    JetBrains RubyMine 2019 for Mac(Ruby代码编辑器) 这些功能你用了几个?
    代码片段管理软件Snippetslab mac版软件测评
    十分钟玩转 XMind 中的多种思维结构
  • 原文地址:https://www.cnblogs.com/pigdata/p/10305644.html
Copyright © 2011-2022 走看看