/** * 获取index索引位置的元素 * 时间复杂度 : O(1) * * @param index 元素的位置 * @return index索引位置的元素 */ public E get(int index) { if (index < 0 || index >= size) { thrownewIllegalArgumentException("Get failed. Index is illegal"); } return data[index]; }
/** * 获取数组中第一个元素的值 * * @return 数组中第一个元素的值 */ public E getFirst() { return get(0); }
/** * 获取数组中最后一个元素的值 * * @return 数组中最后一个元素的值 */ public E getLast() { return get(size - 1); }
/** * 将index位置上的元素修改为element * 时间复杂度 : O(1) * * @param index 要修改元素的位置 * @param element 修改后元素的值 */ publicvoidset(int index, E element) { if (index < 0 || index >= size) { thrownewIllegalArgumentException("Get failed. Index is illegal"); } data[index] = element; }
/** * 判断数组中是否包含element元素 * 时间复杂度 : O(n) * * @param element 待判断的元素 * @return 当数组中包含element返回true */ publicbooleancontains(E element) { for (inti=0; i < size; i++) { if (data[i] == element) { returntrue; } } returnfalse; }
/** * 查询数组中是否包含element * 时间复杂度 : O(n) * * @param element 待查找的元素 * @return 包含返回element所在的索引, 不包含则返回-1 */ publicintfind(E element) { for (inti=0; i < size; i++) { if (data[i] == element) { return i; } } return -1; }
/** * 删除index位置上的元素,返回删除的元素 * 时间复杂度 : O(n) * * @param index */ public E remove(int index) { if (index < 0 || index >= size) { thrownewIllegalArgumentException("Remove failed. index >=0 and index < size"); } Eelement= data[index]; // 将index位置后的元素向前移动一位
for (inti= index + 1; i < size; i++) { data[i - 1] = data[i]; } /* Otherwise, if any of the following is true, an IndexOutOfBoundsException is thrown and the destination is not modified: The srcPos argument is negative. srcPos参数为负数。 The destPos argument is negative. destPos参数是负数。 The length argument is negative. 长度参数是负数。 srcPos+length is greater than src.length, the length of the source array. srcPos + length大于src.length,即源数组的长度。 destPos+length is greater than dest.length, the length of the destination array. destPos + length大于dest.length,即目标数组的长度。 */ /* 使用System.arraycopy()方法出现异常 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException at com.holelin.array.Array.remove(Array.java:235) at com.holelin.array.Array.removeFirst(Array.java:253) at com.holelin.queue.ArrayQueue.dequeue(ArrayQueue.java:31) at com.holelin.queue.QueueEfficiencyTest.testQueue(QueueEfficiencyTest.java:34) at com.holelin.queue.QueueEfficiencyTest.main(QueueEfficiencyTest.java:20) */ // if (size - index + 1 >= 0) { // //public static void (Object src, // int srcPos, // Object dest, // int destPos, // int length) // // src:源数组; srcPos:源数组要复制的起始位置; // // dest:目的数组; destPos:目的数组放置的起始位置; // // length:复制的长度。 // // 注意:src and dest都必须是同类型或者可以进行转换类型的数组. // System.arraycopy(data, index + 1, data, index, size - index + 1); // } size--; data[size] = null; // 当数组中元素个数等于容量的4分之一时,进行缩容 if (size == data.length / 4 && data.length / 2 != 0) { resize(data.length / 2); } return element; }
/** * 删除数组中第一个元素,并返回元素的值 * 时间复杂度 : O(n) * * @return 返回删除元素的值 */ public E removeFirst() { return remove(0); }
publicstaticintindexOf(int[] a, int key) { intlo=0; inthi= a.length - 1; while (lo <= hi) { // Key is in a[lo..hi] or not present. intmid= lo + (hi - lo) / 2; if (key < a[mid]) hi = mid - 1; elseif (key > a[mid]) lo = mid + 1; elsereturn mid; } return -1; }
找出数组中最大的元素
1 2 3 4 5 6
doublemax= a[0]; for(inti=1; i < a.length; i++){ if(a[i] > max){ max = a[i]; } }
计算数组元素的平均值
1 2 3 4 5 6
intN= a.length; doublesum=0.0; for(inti=0; i < N; i++){ sum += a[i]; } doubleaverage= sum / N;
复制数组
1 2 3 4 5
intN= a.length; double[] b = newdouble[N]; for(inti=0; i < N; i++){ b[i] = a[i] }