初学请教~~
一java容器,Array Implementation 的习题~~~ps:初学者,又在国外学习,不要人身攻击!!!
自己写了点~~但是还是晕晕的~~~谁有时间帮忙写看看,给偶学习一下~~The interface that you will be implementing is a bag (or multiset) collection used for
storing a set of objects—including duplicates.
The collection will be called a “rank” bag, as it will have some additional methods
for accessing the smallest, second-smallest, and kth-smallest item stored in it. Your
implementation for part 1:
 will implement the RankBag interface in the RankBagArray class;
 will use an unsorted array for storing the objects; and
 will not use any built-in collections to work with the objects (with the exception of
the uniqueSet() method).附件如下~
http://www.javatx.cn/upfiles/club/20099/2009091402284953380.zip

解决方案 »

  1.   

    如果有谁有时间写的话~~
    发到[email protected]
    谢谢
      

  2.   

    上面连接没有的话http://pickup.mofile.com/9969210365180414
    从这里下~~~别砸砖呀~~
    我知道要自己写才学得会~~
    但是开始总得对着例子写~~~
    谢谢拉~
      

  3.   

    刚下载了,看到你那个目录就有点晕了自己慢慢写吧,哪里有问题直接贴出来问吧
    PS:你用的什么IDE
      

  4.   

    你是用的Mac OS吧,
    我好像在一本叫做
    《Java动画、图形和极富客户端效果开发》
    上看过类似代码
      

  5.   

    这是我blog里的一段代码,虽然不能完全符合楼主的要求,但是我觉得有一点启发意义吧...希望对楼主有帮助...
    class StackOverflowException extends Exception{ 
        public StackOverflowException(){ 
        } 
    } class StackEmptyException extends Exception{ 
        public StackEmptyException(){ 
        } 
    } public class StackClass { 
        private int stackSize; 
        private int stackTop; 
        private String[] array;  
         
        public StackClass(){ 
            System.out.println("The current Stack capacity is 100."); 
             
            stackSize = 100; 
            stackTop = 0; 
            array = new String[stackSize]; 
        } 
         
        public StackClass(int size){ 
            if (size <= 0){ 
                System.out.println("The current Stack capacity is 100."); 
                stackSize = 100; 
            } 
             
            stackSize = size; 
            stackTop = 0; 
            array = new String[stackSize]; 
        } 
         
        public boolean isFullStack(){ 
            return stackSize == stackTop; 
        } 
         
        public boolean isEmptyStack(){ 
            return stackTop == 0; 
        } 
         
        public void push(String str) throws StackOverflowException{ 
            if (isFullStack()) 
                throw new StackOverflowException(); 
             
            array[stackTop] = str; 
            stackTop ++; 
        } 
         
        public String pop() throws StackEmptyException{ 
            if (isEmptyStack()) 
                throw new StackEmptyException(); 
             
            stackTop --; 
            return array[stackTop]; 
                 
        } 
         
        public static void main(String args[]){ 
            StackClass stack = new StackClass(); 
             
            try{ 
                stack.push("a"); 
                stack.push("aa"); 
                stack.push("aaa"); 
            } 
            catch(StackOverflowException e){ 
                e.printStackTrace(); 
            } 
             
            try{ 
                while (!stack.isEmptyStack()) 
                    System.out.println(stack.pop()); 
            } 
            catch(StackEmptyException e){ 
                e.printStackTrace(); 
            } 
             
             
        } 
    }
    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/justinavril/archive/2008/10/19/3104032.aspx
      

  6.   

    Most of the methods you will need to implement are described below; for more detailed
    information, see the JavaDoc of the RankBag interface in the BlueJ project provided.
    add(E o) Adds o to the bag.
    contains(Object o) Returns true if the bag contains o.
    getCount(Object o) Returns how many times o has been added to the
    bag.
    remove(Object o) Removes all instances of o from the bag.
    removeOnce(Object o) Removes one instance of o from the bag.
    size() Returns the number of elements in the bag (the cardinality).
    uniqueSet() Returns a new Set of all elements in this bag with no
    duplicate entries.
    first() Returns the first (lowest) element currently in the
    bag.
    second() Returns the second smallest item in the bag.
    kth() Returns the kth smallest item in the bag. For example,
    k = 2 is the same as the second-smallest.
    You should test your implementation using the supplied JUnit tests attached to the
    RankBagArray class (where you will write your implementation). You can run these
    tests by right-clicking on the RankBagArrayTest class and choosing Test All.
      

  7.   

    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;
    }

    }
    自己以前写的MyArrayList,希望能给LZ点帮助,其实集合类很好写,LZ也可以先赵本数据结构的书,上面都有很多的例子
    给LZ推荐一本http://book.csdn.net/hi/BookClub_BookDetails.aspx?id=30400 
    里面有很多作者写的包
      

  8.   

    这年头人都变懒了,我这样一直很懒的反倒显得勤快……
    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:[email protected]
     * @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));
    }}
      

  9.   

    class < T extends ?>