java容器的一习题!!初学者!

pangxie887 2009-09-14 02:57:57
初学请教~~
一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
...全文
276 23 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
pangxie887 2009-09-15
  • 打赏
  • 举报
回复
还是很感谢
  • 打赏
  • 举报
回复
mark
dujun3245350 2009-09-15
  • 打赏
  • 举报
回复
接分
tfsict2008 2009-09-15
  • 打赏
  • 举报
回复
class < T extends ?>
cping1982 2009-09-15
  • 打赏
  • 举报
回复
这年头人都变懒了,我这样一直很懒的反倒显得勤快……


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

}
Ostroff 2009-09-14
  • 打赏
  • 举报
回复
你是用的Mac OS吧,
我好像在一本叫做
《Java动画、图形和极富客户端效果开发》
上看过类似代码
舷Kelvin 2009-09-14
  • 打赏
  • 举报
回复
能力不够就不要做这个题啊。从你能理解的开始一步一步来。谁都不是一口吃成一个胖子的。
zjx2388 2009-09-14
  • 打赏
  • 举报
回复
刚下载了,看到你那个目录就有点晕了

自己慢慢写吧,哪里有问题直接贴出来问吧


PS:你用的什么IDE
zjx2388 2009-09-14
  • 打赏
  • 举报
回复
你的集合范围小了

先看下你代码
  • 打赏
  • 举报
回复
楼主在哪国呢
roma505 2009-09-14
  • 打赏
  • 举报
回复
路过学习
yinzisheng 2009-09-14
  • 打赏
  • 举报
回复
支持下拉~~~
dixiasenlin 2009-09-14
  • 打赏
  • 举报
回复
呵呵

支持一下!
lk198186 2009-09-14
  • 打赏
  • 举报
回复
帮顶
pangxie887 2009-09-14
  • 打赏
  • 举报
回复
上面连接没有的话

http://pickup.mofile.com/9969210365180414
从这里下~~~

别砸砖呀~~
我知道要自己写才学得会~~
但是开始总得对着例子写~~~
谢谢拉~
pangxie887 2009-09-14
  • 打赏
  • 举报
回复
如果有谁有时间写的话~~
发到375298170@qq.com
谢谢
owen_008 2009-09-14
  • 打赏
  • 举报
回复
楼主在哪个国家学习啊??~呵呵,加油啊~~
loveofmylife 2009-09-14
  • 打赏
  • 举报
回复
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
里面有很多作者写的包
goodmrning 2009-09-14
  • 打赏
  • 举报
回复
ding!
pangxie887 2009-09-14
  • 打赏
  • 举报
回复
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.
加载更多回复(3)

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧