日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义ArrayList

發布時間:2023/12/20 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义ArrayList 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ArrayList是通過數組實現的。

實現ArrayList類中的以下功能:

  • size():列表的大小
  • isEmpty():判斷列表是否為空
  • clear():清空整個列表
  • get(int index):獲取下標為index的值
  • set(int index,Object value):修改下標為index的值
  • add(Object value):向列表中(也就是在列表的最后)添加數據
  • remove(int index):刪除下標為index的值
  • 實現ArrayList的迭代器(Iterator),實現hasnext()、next()、remove()方法。注意:這里并沒有實現remove()方法必須在next()方法之后調用的邏輯
  • package com.test.test;import java.util.Iterator; import java.util.NoSuchElementException;public class Test2 {public static void main(String[] args) {MyArrayList<String> mList = new MyArrayList<String>();mList.add("我");mList.add("是");mList.add("大");mList.add("小");mList.add("牛");mList.add("奶");// System.out.println(mList.isEmpty()); // System.out.println(mList.size());mList.set(0, "你");for (int i = 0; i < mList.size(); i++) {String value = (java.lang.String) mList.get(i);System.out.println(value);}// Iterator<String> iterator = mList.iterator(); // while(iterator.hasNext()){ // System.out.println(iterator.next()); // }// mList.clear(); // System.out.println(mList.size());}}/*** 自定義的ArrayList類* 2015年10月19日 下午4:59:05* @author 張耀暉** @param <String>*/ class MyArrayList<String> implements Iterable<String> {private int theSize;private Object[] theItems;private static final int DEFAULT_LENGTH = 10;//設置默認的數組的大小為10// 清除數據public void clear() {theSize = 0;ensureCapacity(DEFAULT_LENGTH);}// list大小,返回list的大小public int size() {return theSize;}// list是否為空,返回是否為空的標志public boolean isEmpty() {return size() == 0;}//獲取list中的index對應的值,返回獲取到的值public Object get(int index) {if (index < 0 || index >= size()) {throw new ArrayIndexOutOfBoundsException();}return theItems[index];}//設置某個位置的元素的值,返回原來位于該位置的元素public Object set(int index,Object value){if(index<0||index>=size()){throw new ArrayIndexOutOfBoundsException();}Object oldItem = theItems[index];theItems[index] = value;return oldItem;}//在數組的最后的位置添加元素,返回添加是否成功的標志public boolean add(Object value){add(size(), value);//這里的size()其實就是list所擁有的最后的下標+1的位置return true;}//往list中添加值,在index位置處添加valuepublic void add(int index,Object value) {ensureCapacity(index);for(int i=index;i<size();i++){theItems[i+1] = theItems[i];//將添加位置以后的數據向后移動}theItems[index] = value;//設置添加位置的值theSize++;//列表的大小增加1}//移除index下標對應的值public Object remove(int index) {Object removeItem = theItems[index];//獲取刪除位置的原來的值for(int i=index;i<size()-1;i++){theItems[i] = theItems[i+1];//將刪除位置以后的數據向前移動}theSize--;//列表的大小減少1return removeItem;}// 確定數組的大小public void ensureCapacity(int newCapacity) {if (newCapacity < theSize) {return;} else {//如果需要的容量大于原先數組的容量Object[] old = theItems;//保存原來的數組內容theItems = new Object[newCapacity+1];//新建一個擴容數組for (int i = 0; i < size(); i++) {theItems[i] = old[i];//將原來的數組中的值逐個賦值給新數組}}}@Overridepublic Iterator<String> iterator() {return new ArrayListIterator();}private class ArrayListIterator implements Iterator<String>{private int current = 0;@Override//判斷是否還有下一個數據public boolean hasNext() {return current<size();}@Override//返回下一個數據public String next() {if(!hasNext()){throw new NoSuchElementException();}return (String) theItems[current++];}@Override//移除剛才返回的下一個數據(next()返回的值)public void remove() {MyArrayList.this.remove(--current);}}}

    打印結果:

    總結

    以上是生活随笔為你收集整理的自定义ArrayList的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。