int 型動態陣列
阿新 • • 發佈:2020-09-08
main.java
package com.company; //import java.util.ArrayList; public class Main { public static void main(String[] args) { // write your code here ArrayList list = new ArrayList(); //list.get(-10); list.add(99); list.add(88); list.add(77); list.add(66); list.add(55); //list.remove(2); //list.get(5); //list.add(list.size(),100); //list.set(3,80); System.out.println(list); // ArrayList list = new ArrayList(5); // list.add(99); // list.add(88); // list.add(77); // list.add(66);// list.add(55); // list.remove(2); // System.out.println(list); } }
ArrayList.java
package com.company; import java.util.Arrays; public class ArrayList { private int size; private int[] elements; private static final int DEFALT_CAPACITY = 2; private static finalint ELEMENT_NOT_FOUND = -1; public ArrayList(int capacity){ capacity = (capacity < DEFALT_CAPACITY) ? DEFALT_CAPACITY : capacity; elements = new int[capacity]; } public ArrayList(){ //elements = new int[10]; this(DEFALT_CAPACITY); } public int size(){ return size; } public boolean isEmpty(){ return size == 0; } /** * 獲取index位置的元素 * @param index * @return */ public int get(int index){ if (index < 0 || index >= size){ throw new IndexOutOfBoundsException("index不合適"); } return elements[index]; } /** * 設定index位置的元素 * @param index * @param element * @return */ public int set(int index, int element){ if (index < 0 || index >= size){ throw new IndexOutOfBoundsException("index不合適"); } int old = elements[index]; elements[index] = element; return old; } /** * 某個元素的下標 * @param element * @return */ public int indexOf(int element){ for (int i = 0; i < size; i++){ if (elements[i] == element)return i; } return ELEMENT_NOT_FOUND; } /** * 是否包含某個元素 * @param element * @return */ public boolean contains(int element){ return indexOf(element) != ELEMENT_NOT_FOUND; } public void add(int element){ //elements[size++] = element; ensureCapacity(size+1); add(size,element); } public int remove(int index){ System.out.println(size); if (index < 0 || index >= size){ throw new IndexOutOfBoundsException("index不合適"); } int old = elements[index]; for (int i = index + 1; i <= size - 1; i++){ elements[i - 1] = elements[i]; } size--; return old; } public void clear(){ size = 0; } /** * 往index位置新增元素 注意從後往前挪動 * @param index * @param element */ public void add(int index,int element){ //允許=size if (index < 0 || index > size){ throw new IndexOutOfBoundsException("index不合適"); } for (int i = size - 1;i >= index;i--){ elements[i+1] = elements[i]; } elements[index] = element; size++; } /** * 保證要有capacity的容量 * @param capacity */ private void ensureCapacity(int capacity){ int oldCapacity = elements.length; if (oldCapacity >= capacity)return; //動態擴容 int newCapacity = oldCapacity + (oldCapacity >> 1);// 右移除以2 System.out.println(size+"動態擴容"+newCapacity); int[] newElements = new int[newCapacity]; for (int i = 0; i < size; i++){ newElements[i] = elements[i]; } elements = newElements; } @Override public String toString() { //這種會打印出全部位置的元素 // return "ArrayList{" + // "size=" + size + // ", elements=" + Arrays.toString(elements) + // '}'; StringBuilder string = new StringBuilder(); string.append("size=").append(size).append(",["); for (int i = 0;i < size; i++){ if (i != 0){ string.append(","); } string.append(elements[i]); } string.append("]"); return string.toString(); } }
Assert.java
package com.company; public class Assert { public static void test(boolean value){ try{ if(!value) throw new Exception("測試不通過"); } catch (Exception e){ e.printStackTrace(); } } }