62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
*
* Copyright 2008 - 2009
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* @project loonframework
* @author chenpeng
* @email:ceponline@yahoo.com.cn
* @version 0.1
*/
public class RankBagArray<E extends Comparable<E>> implements RankBag<E> {
// 初始的集合大小
private final static int INITIAL_CAPACITY = 20;
// 保存集合的对象数组
private Object[] elements;
// 集合的预期长度
private int size;
/**
* Constructor
*/
public RankBagArray() {
size = 0;
elements = new Object[INITIAL_CAPACITY];
}
/**
* 互换数据
*
* @param data
* @param x
* @param y
*/
private void swap(Object[] data, int x, int y) {
Object temp = data[x];
data[x] = data[y];
data[y] = temp;
}
/**
* 排序数据
*
* @param src
* @param dest
* @param low
* @param high
* @param off
*/
private void sort(Object[] src, Object[] dest, int low, int high, int off) {
int length = high - low;
if (length < 10) {
for (int i = low; i < high; i++)
for (int j = i; j > low
&& ((Comparable) dest[j - 1]).compareTo(dest[j]) > 0; j--) {
swap(dest, j, j - 1);
}
return;
}
int destLow = low;
int destHigh = high;
low += off;
high += off;
int mid = (low + high) >> 1;
sort(dest, src, low, mid, -off);
sort(dest, src, mid, high, -off);
if (((Comparable) src[mid - 1]).compareTo(src[mid]) <= 0) {
System.arraycopy(src, low, dest, destLow, length);
return;
}
for (int i = destLow, p = low, q = mid; i < destHigh; i++) {
if (q >= high || p < mid
&& ((Comparable) src[p]).compareTo(src[q]) <= 0) {
dest[i] = src[p++];
} else {
dest[i] = src[q++];
}
}
}
/**
* 排序数据
*
* @return
*/
private Object[] sort(Object[] o) {
if (size == 0) {
return null;
}
Object[] dest = new Object[size];
System.arraycopy(o, 0, dest, 0, size);
sort(o, dest, 0, size, 0);
int i = 0, j = 0;
for (i = 0; i < size - 1; i++) {
if (dest[i] != dest[i + 1]) {
dest[i] = dest[i + 1];
j++;
}
}
return dest;
}
/**
* 扩充数组容量
*
* @param obj
* @param index
* @return
*/
private void expandCapacity(int capacity) {
if (elements.length < capacity) {
Object[] bagArray = (Object[]) new Object[capacity];
System.arraycopy(elements, 0, bagArray, 0, size);
elements = bagArray;
}
}
/**
* 压缩数组容量
*
* @param capacity
*/
private void compressCapacity(int capacity) {
if (capacity + this.size < elements.length) {
Object[] newArray = (Object[]) new Object[this.size + 2];
System.arraycopy(elements, 0, newArray, 0, this.size);
elements = newArray;
}
}
/**
* 删除指定数据,可选择是否删除全部同类对象
*
* @param o
* @param isRemoves
* @return
*/
private boolean remove(Object o, boolean isRemoves) {
if (o == null) {
return false;
}
if (elements == null) {
return false;
}
boolean flag = false;
for (int i = size; i > 0; i--) {
if (o.equals(elements[i])) {
flag = true;
size--;
elements[i] = elements[size];
elements[size] = null;
if (size == 0) {
elements = null;
} else {
compressCapacity(2);
}
if (!isRemoves) {
return true;
}
}
}
return flag;
}
/**
* RankBag Adds an object to the bag.
*/
public boolean add(E o) {
if (size == elements.length) {
expandCapacity((size + 1) * 2);
}
return (elements[size++] = o) != null;
}
/**
* RankBag Removes all objects from the bag.
*/
public void clear() {
for (int i = 0; i < elements.length; i++) {
elements[i] = null;
}
size = 0;
}
/**
* RankBag Checks if an object exists in the bag.
*/
public boolean contains(Object o) {
if (o == null) {
return false;
}
if (elements == null) {
return false;
}
for (int i = 0; i < size; i++) {
if (elements[i] != null && o.equals(elements[i])) {
return true;
}
}
return false;
}
/**
*
* Returns the smallest (according to the natural ordering of the of the
* contained objects and the ranking system described in RankBag) in the
* bag.
*
* If more than one object qualifies as the "smallest", then any one of the
* qualifying items is returned.
*/
public E first() {
Object[] first = sort(elements);
return (E) (first == null ? null : first[0]);
}
/**
* RankBag Returns the number of instances of a specified object that have
* been added to the bag.
*/
public int getCount(Object o) {
int index = 0;
for (int i = 0; i < size; i++) {
if (o == elements[i]) {
index++;
}
}
return index;
}
/**
* RankBag Returns if the bag is empty or not.
*/
public boolean isEmpty() {
return size == 0;
}
/**
*
* Returns the kth smallest (according to the natural ordering of the
* contained objects and the ranking system described in RankBag) in the
* bag.
*
* If more than one object qualifies as the "kth smallest", then any one of
* the qualifying items is returned.
*
* In the case of k=1, this is equivalent to RankBag.first(); in the case of
* k=2, this is equivalent to RankBag.second().
*/
public E kth(int k) {
if (k < 1 || k >= size) {
return null;
}
Object[] first = sort(elements);
return (E) (first == null ? null : first[k - 1]);
}
/**
* RankBag Removes all instances of the specified object from the bag.
*/
public boolean remove(Object o) {
return remove(o, true);
}
/**
* Removes a single instance of the specified object from the bag.
*/
public boolean removeOnce(Object o) {
return remove(o, false);
}
/**
* Returns the second smallest (according to the natural ordering of the
* contained objects and the ranking system described in RankBag) in the
* bag.
*
* If more than one object qualifies as the "second smallest", then any one
* of the qualifying items may be returned.
*/
public E second() {
if (size < 2) {
return null;
}
Object[] first = sort(elements);
return (E) (first == null ? null : first[1]);
}
/**
* Returns the cardinality of the bag collection.
*/
public int size() {
return size;
}
/**
* Returns a set containing all objects in the bag without any duplicates.
* Any valid Set from the Java collections API may be returned.
*/
public Set<E> uniqueSet() {
Object[] bagArray = new Object[size];
int i = 0, j = 0;
for (i = 0; i < size - 1; i++) {
while (bagArray[i] == elements[i + 1]) {
i++;
}
bagArray[j++] = elements[i];
}
Object[] tempArray = new Object[i];
System.arraycopy(bagArray, 0, tempArray, 0, i);
return new HashSet(Arrays.asList(tempArray));
}
}
import java.util.*;
public class MyArrayList implements List{
private Object[] data;
private int count;
public MyArrayList(){
this(10);
}
public MyArrayList(int initCap){
data=new Object[initCap];
count=0;
}
public MyArrayList(Collection c){
data=new Object[c.size()];
for(Object obj:c){
data[count++]=obj;
}
}
/**根据指定的下标插入指定的元素
* 如果下标越界则抛出IndexOutOfBoundsException异常
* @param index,element
* @return
*/
public void add(int index, Object element) {
if(count<data.length){
if(index<=count){
for(int i=index+1;i<=count;i++){
data[i+1]=data[i];
}
data[index]=element;
count++;
}else {
throw new IndexOutOfBoundsException("指定下标越界");
}
}else{
Object[] newData=new Object[2*data.length];
System.arraycopy(data, 0, newData, 0, data.length);
this.data=newData;
System.gc();
if(index<=count){
for(int i=index+1;i<=count;i++){
data[i+1]=data[i];
}
data[index]=element;
count++;
}else {
throw new IndexOutOfBoundsException("指定下标越界");
}
}
}
public boolean add(Object e) {
if(count<data.length){
data[count++]=e;
return true;
}else{
Object[] newData=new Object[2*data.length];
System.arraycopy(data, 0, newData, 0, data.length);
this.data=newData;
data[count++]=e;
return true;
}
}
public boolean addAll(Collection c) {
// TODO Auto-generated method stub
return false;
}
public boolean addAll(int index, Collection c) {
// TODO Auto-generated method stub
return false;
}
public void clear() {
data=new Object[10];
count=0;
System.gc();
}
public boolean contains(Object o) {
for(Object obj:data){
if(obj==o){
return true;
}
}
return false;
}
public boolean containsAll(Collection c) {
// TODO Auto-generated method stub
return false;
}
public Object get(int index) {
if(index<count){
Object o=new Object();
o=data[index];
return o;
}else {
throw new IndexOutOfBoundsException("指定下标越界");
}
}
public int indexOf(Object o) {
int i=0;
if(this.contains(o)){
for(Object obj:data){
if(obj==o){
return i;
}
i++;
}
}
return -1;
}
public boolean isEmpty() {
if(count==0){
return true;
}
return false;
}
public Iterator<Object> iterator() {
Iterator<Object> it=new Iterator<Object>(){
public int init=0;
public boolean hasNext() {
if(init<count){
init++;
return true;
}
return false;
}
public Object next() {
if(init<=count){
return data[init-1];
}else {
throw new NoSuchElementException();
}
}
public void remove() {
MyArrayList.this.remove(init-1);
init--;
}
};
return it;
}
public int lastIndexOf(Object o) {
// TODO Auto-generated method stub
return 0;
}
public ListIterator listIterator() {
// TODO Auto-generated method stub
return null;
}
public ListIterator listIterator(int index) {
// TODO Auto-generated method stub
return null;
}
public Object remove(int index) {
if(index<count){
Object obj=data[index];
for(int i=index;i<count-1;i++){
data[i]=data[i+1];
}
data[count-1]=null;
count--;
return obj;
}else{
throw new IndexOutOfBoundsException("指定下标越界");
}
}
public boolean remove(Object o) {
if(this.contains(o)){
int i=this.indexOf(o);
this.remove(i);
return true;
}
return false;
}
public boolean removeAll(Collection c) {
// TODO Auto-generated method stub
return false;
}
public boolean retainAll(Collection c) {
// TODO Auto-generated method stub
return false;
}
public Object set(int index, Object element) {
if(index<count){
Object obj=data[index];
data[index]=element;
return obj;
}else{
throw new IndexOutOfBoundsException("指定下标越界");
}
}
public int size() {
return count;
}
public List subList(int fromIndex, int toIndex) {
// TODO Auto-generated method stub
return null;
}
public Object[] toArray() {
Object[] obj=new Object[count];
for(Object o:data){
int i=0;
obj[i]=o;
i++;
}
return obj;
}
public Object[] toArray(Object[] a) {
// TODO Auto-generated method stub
return null;
}
}