/** Moo */ @Test(expected = IndexOutOfBoundsException.class) public void testSetIndexOutOfBoundsException2() { assertEquals(0, words.size()); words.add("6"); words.add("7"); words.set(2, "5"); }
/** Moo */ @Test public void testGet() { words.add("moo"); words.add("cow"); assertSame("moo", words.get(0)); assertSame("cow", words.get(1)); }
public int getPivotIndex(MyArrayList a, int start, int end) { int first = (Integer) a.get(start); int second = (Integer) a.get((start + end) / 2); int third = (Integer) a.get(end); int firstInd = start; int secondInd = (start + end) / 2; int thirdInd = end; if (first <= second) { if (third <= first) { return firstInd; } else { if (third >= second) { return secondInd; } else { return thirdInd; } } } else { if (third >= first) { return firstInd; } else { if (third <= second) { return secondInd; } else { return thirdInd; } } } }
/** Moo */ @Test public void testIndexOf() { words.add("monkey"); assertEquals(0, words.indexOf("monkey")); words.add("donkey"); assertEquals(-1, words.indexOf("honkey")); }
/** Moo */ @Test public void testContainsTrue() { words.add("100"); assertTrue(words.contains("100")); words.add("500"); assertTrue(words.contains("500")); }
public static void main(String[] args) { int[] initArr = {12, 45, 99, 66, 3}; MyArrayList arr = new MyArrayList(); arr.init(initArr); for (int i : arr) { System.out.println(i); } }
/** Moo */ @Test public void testIsEmpty() { assertTrue(words.isEmpty()); words.add("hello"); assertFalse(words.isEmpty()); words.remove(0); assertTrue(words.isEmpty()); }
/** Moo */ @Test(expected = IndexOutOfBoundsException.class) public void testGetFail3() { assertEquals(0, words.size()); words.add("0"); words.add("0"); words.add("0"); words.get(3); }
/** Moo */ @Test public void testContainsFalse() { assertFalse(words.contains("0")); words.add("100"); assertFalse(words.contains("0")); words.add("200"); assertFalse(words.contains("500")); }
/** Moo */ @Test public void trimToSize() { words.trimToSize(); assertEquals(0, words.size()); words.add("1"); words.trimToSize(); assertEquals(1, words.size()); }
/** Moo */ @Test public void testToString() { assertEquals("[]", words.toString()); words.add("5"); words.add("6"); words.add("7"); assertEquals("[5, 6, 7]", words.toString()); }
/** Moo */ @Test public void testLargeAdd() { for (int i = 0; i < 10000; i++) { words.add("" + i); } assertEquals(10000, words.size()); assertEquals("0", words.get(0)); assertEquals("9999", words.get(9999)); }
/** Moo */ @Test public void testRemoveObject() { words.add("5"); words.add("6"); words.add("7"); assertTrue(words.remove("6")); assertFalse(words.remove("8")); assertEquals(2, words.size()); }
/** Moo */ @Test public void testSet() { words.add("6"); words.add("7"); assertEquals("6", words.set(0, "3")); assertEquals("3", words.get(0)); assertEquals("7", words.get(1)); assertEquals(2, words.size()); }
/** Moo */ @Test public void testToArray() { words.add("5"); words.add("6"); words.add("7"); Object[] vals = words.toArray(); assertEquals("5", vals[0]); assertEquals("6", vals[1]); assertEquals("7", vals[2]); assertEquals(3, vals.length); }
public static void main(String[] args) { MyArrayList names = new MyArrayList(); try { names.myadd((Object) "Sarah"); names.myadd((Object) "Bill"); names.myadd((Object) "Sarah"); names.myadd((Object) "Dave"); } catch (ArrayExp ex) { ex.printStackTrace(); } finally { System.out.println("All Done"); } }
/** * Return all hashed values as an hashMap * * @return hashed values in hashMap */ @WrittenWhile("Caffeine levels dangerously low.") public T[] toArray() { MyArrayList<T> result = new MyArrayList<>(); for (MyLinkedList<Tuple<T>> list : hashed) { int j = 0; while (list.get(j) != null) { result.add(list.get(j).getValue().value); j++; } } return result.toArray(1); }
/** * Put a new hashed value in the hashMap. * * @param key a String identifier for the value * @param value a value corresponding with the String key */ public void put(String key, T value) { int i = hash(key); MyLinkedList<Tuple<T>> list = hashed.get(i); list.add(new Tuple<>(key, value)); keys.add(key); hashed.set(i, list); }
/** Moo */ @Test public void testAddLoc() { words.add(0, "5"); System.out.println(words.toString()); words.add(0, "3"); System.out.println(words.toString()); words.add(1, "4"); System.out.println(words.toString()); assertSame("3", words.get(0)); assertSame("4", words.get(1)); assertSame("5", words.get(2)); assertEquals(3, words.size()); }
/** Moo */ @Test public void testLastIndexOf() { assertEquals(-1, words.lastIndexOf("0")); words.add("5"); assertEquals(0, words.lastIndexOf("5")); words.add("5"); assertEquals(1, words.lastIndexOf("5")); words.add("3"); words.add("5"); assertEquals(3, words.lastIndexOf("5")); assertEquals(-1, words.lastIndexOf("Hello")); }
/** Moo */ @Test public void testAdd() { assertTrue(words.add("hello")); assertEquals(1, words.size()); assertTrue(words.add("world")); assertEquals(2, words.size()); assertTrue(words.add("blue")); assertEquals(3, words.size()); assertSame("hello", words.get(0)); assertSame("world", words.get(1)); assertSame("blue", words.get(2)); }
/** 打印金额、积分情况 */ public void printMssage() { double totalPrice = 0; int fre = 0; Iterator iterator = list.iterator(); // 获取迭代器 while (!iterator.isDone()) { Rental r = (Rental) iterator.getCurrent(); totalPrice += r.getMovie().getPrice(r.getDays()); fre += r.getMovie().getFre(r.getDays()); iterator.next(); } System.out.println(this.name + "您消费的金额是:" + totalPrice + "您这次所获得的积分是:" + fre); }
private static void quickSortClassic( MyArrayList a, int start, int end, IPivotStrategy pivotStrategy) { if (end - start < 1) { return; } int pivotIndex = pivotStrategy.getPivotIndex(a, start, end); int pivotValue = (Integer) a.get(pivotIndex); swap(a, start, pivotIndex); int i = start + 1; int j = start + 1; while (j <= end) { if ((Integer) a.get(j) < pivotValue) { swap(a, j, i); i++; } j++; } swap(a, start, i - 1); quickSortClassic(a, start, i - 2, pivotStrategy); quickSortClassic(a, i, end, pivotStrategy); }
/** Moo */ @Test public void testRemove() { words.add("5"); words.add("6"); words.add("7"); assertEquals("5", words.remove(0)); assertEquals(2, words.size()); assertEquals("7", words.remove(1)); assertEquals(1, words.size()); }
public static void main(String[] args) { MyArrayList arrayList = new MyArrayList(); arrayList.insert("1", 0); arrayList.insert("2", 0); arrayList.insert("3", 0); arrayList.insert("4", 0); for (int kk = 0; kk < 20; kk++) { arrayList.insert("4" + kk, kk + 1); } arrayList.remove(2); Object[] objects = arrayList.getObjects(); for (int i = 0; i < arrayList.getLen(); i++) { System.out.println(objects[i]); } }
private static int binarySearch(MyArrayList list, int start, int end, Object key) { int index = start + (end - start) / 2; int compare = ((Integer) key).compareTo((Integer) list.get(index)); if (compare < 0) { if (start == index) { return -1 * index - 1; } index = binarySearch(list, start, index - 1, key); } if (compare > 0) { if (end == index) { return -1 * (index + 1) - 1; } index = binarySearch(list, index + 1, end, key); } return index; }
public static void main(String[] args) { MyArrayList<String> a = new MyArrayList<>(); for (int i = 0; i < 20; i++) { a.add("a" + i); } }
@Override public T next() { if (!hasNext()) throw new NoSuchElementException(); return hashMap.lookup(keys.get(current++)); }
public static int binarySearch(MyArrayList list, Object key) { if (list.isEmpty()) return -1; return binarySearch(list, 0, list.size() - 1, key); }
public static void quickSortClassic(MyArrayList a, IPivotStrategy pivotStrategy) { quickSortClassic(a, 0, a.size() - 1, pivotStrategy); }