gpt4 book ai didi

java - 包在Java中作为数组实现

转载 作者:行者123 更新时间:2023-12-04 06:42:16 24 4
gpt4 key购买 nike

我应该实现一个包数据结构(也称为多集),一个无序的
项目的可能有重复的同类值(任何 Java 对象,不包括 null)的集合。我已经在互联网上进行了广泛的搜索,但是很难将我的思想包裹在使用数组而不是 List 之类的东西上,并且不太了解在类中使用数组的语法。

我需要实现所有 java.util.Collection,除非通过抛出 UnsupportedOperationException 进行说明。是的,我必须使用一个数组,当我添加它时,容量必须增加 10。我的问题是我不确定如何处理 contains 方法、clear 方法、addAll 方法和第二个构造函数。希望我添加的所有其他内容也能顺利运行。我已将 API 定义包含在注释 block 中。任何输入都会真正帮助我。

正如马克在下面问的那样,我不明白如何通过包来搜索特定元素。

import java.util.Collection;
import java.util.Iterator;

class Bag<T> implements Collection<T>{
private T[] array;
public int bagSize;


public Bag(){
array=(T[])new Object[10];
}
public Bag(Collection<T> other ){
//Not sure what to put here
//creates a bag containing all of the items passed to it as a Collection<T>
}

public int size() {
return bagSize;
}

public boolean isEmpty() {
if(size()==0)
return true;
else
return false;
}


public boolean contains(Object o) {
//Not sure what to put here
/*Returns true if this collection contains the specified element. More formally,
returns true if and only if this collection contains at least one element e such
that (o==null ? e==null : o.equals(e)). */
return (o.toArray()==null ? this.toArray()==null : o.toArray() == this.toArray());
}

}


public Iterator<T> iterator() {
throw new UnsupportedOperationException("not implemented.");
}

public Object[] toArray() {
return array;

}

public <T> T[] toArray(T[] a) {
throw new UnsupportedOperationException("not implemented.");
}

public boolean add(T e) {
if(bagSize>=array.length)
return false;
else
{
ensureCapacity(bagSize+10);
array[bagSize]=e;
bagSize++;
return true;
}

}

public boolean remove(Object o) {
for(int i=0; i<bagSize; i++)
if(array[i].equals(o)){
for(int j=i; j<bagSize-1; j++)
array[j]=array[j+1];
bagSize--;
return true;
}
return false;

}

public boolean containsAll(Collection<?> c) {
throw new UnsupportedOperationException("not implemented.");
}

public boolean addAll(Collection<? extends T> c) {
//Not sure what to put here
/*Adds all of the elements in the specified collection to this collection
(optional operation). The behavior of this operation is undefined if the specified
collection is modified while the operation is in progress. (This implies that the
behavior of this call is undefined if the specified collection is this collection,
and this collection is nonempty.) */
}

public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException("not implemented.");
}

public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException("not implemented.");
}

public void clear() {
//Not sure what to put here
/*Removes all of the elements from this collection (optional operation). The
collection will be empty after this call returns (unless it throws an exception).*/
}

@Override
public int hashCode(){
throw new UnsupportedOperationException("not implemented.");
}

@Override
public boolean equals(Object e) {
if (e == null) {
return false;
}
if (getClass() != e.getClass()) {
return false;
}
final Bag<T> other = (Bag<T>) e;
return true;
}

public void ensureCapacity(int minCapacity){
T[] biggerArray;
if(array.length<minCapacity){
biggerArray=(T[]) new Object[minCapacity];
System.arraycopy(array, 0, biggerArray, 0, bagSize);
array=biggerArray;
}
}

最佳答案

我对你里面的东西感到困惑contains ...你调用 toArray()Object ,它没有 toArray()方法。这表明您对您正在尝试做的事情存在一些基本的误解。尽管如此,您实际上似乎确实知道如何检查集合是否包含给定对象,因为您必须找到该对象才能 remove它。您的 remove方法返回完全相同的 booleancontains如果用相同的对象调用就会有。我认为你可以从中工作。

(顺便说一下,您的 remove 方法有一个可能导致内存泄漏的错误......当它将数组中的对象向左移动 1 时,它不会设置不再包含在集合到 null 。)
addAll很简单...给你一个Collection的所有元素都需要添加,你有一个 add可以添加元素的方法。这些一起去。 ( addAll 也是实现第二个构造函数所需的全部内容。)
clear也很简单。调用它之后,你的数组需要没有对任何对象的引用,并且你的包的大小需要为 0。想想你怎么能做到这一点。
iterator() 的工作实现会帮助你很多Collection方法(包括 clear )可以通过使用集合的 Iterator 来实现(方便的抽象类 AbstractCollection 可以做到这一点),但是实现它比实现一个基本的 clear 要困难一些。那可能不会使用它。

另外,一个小笔记。

public boolean isEmpty() {
if(size()==0)
return true;
else
return false;
}

最好写成:
public boolean isEmpty() {
return size() == 0;
}

由于 size() == 0已经是 boolean表达式, if/ else是多余的。

关于java - 包在Java中作为数组实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4093229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com