Java 最频繁的方法包adt(抽象数据类型)
我试图在一个包中实现一个名为mostFrequent的方法,该方法在一个包中查找最频繁的对象,例如,如果B={Bob,Joe,Bob,Ned,Bob,Bob},那么该方法 鲍勃回答。提示:方法是O(n^2)Java 最频繁的方法包adt(抽象数据类型),java,bag,Java,Bag,我试图在一个包中实现一个名为mostFrequent的方法,该方法在一个包中查找最频繁的对象,例如,如果B={Bob,Joe,Bob,Ned,Bob,Bob},那么该方法 鲍勃回答。提示:方法是O(n^2) public E最频繁(B包){ //在这里实现 } 行李的adt如下所示: package edu.uprm.ece.icom4035.bag; import java.util.Iterator; import java.util.NoSuchElementException; pu
public E最频繁(B包){
//在这里实现
}
行李的adt如下所示:
package edu.uprm.ece.icom4035.bag;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class StaticBag implements Bag {
private int currentSize;
private Object elements[];
private class BagIterator implements Iterator {
private int currentPosition;
public BagIterator(){
this.currentPosition = 0;
}
@Override
public boolean hasNext() {
return this.currentPosition < currentSize;
}
@Override
public Object next() {
if (hasNext()){
return elements[this.currentPosition++];
}
else {
throw new NoSuchElementException();
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
public StaticBag(int maxSize){
if (maxSize < 1){
throw new IllegalArgumentException("Max size must be at least 1.");
}
this.currentSize = 0;
this.elements = new Object[maxSize];
}
@Override
public void add(Object obj) {
if (obj == null){
throw new IllegalArgumentException("Value cannot be null.");
}
else if (this.size() == this.elements.length){
throw new IllegalStateException("Bag is full.");
}
else {
this.elements[this.currentSize++] = obj;
}
}
@Override
public boolean erase(Object obj) {
int target = -1;
for (int i=0; i < this.size(); ++i){
if (this.elements[i].equals(obj)){
target = i;
break;
}
}
if (target == -1){
return false;
}
else {
this.elements[target] = this.elements[this.currentSize-1];
this.elements[this.currentSize-1] = null;
this.currentSize--;
return true;
}
}
@Override
public int eraseAll(Object obj) {
int copies = 0;
while(this.erase(obj)){
copies++;
}
return copies;
}
@Override
public int count(Object obj) {
int counter = 0;
for (int i=0; i < this.size(); ++i){
if (elements[i].equals(obj)){
counter++;
}
}
return counter;
}
@Override
public void clear() {
for (int i=0; i < this.size(); ++i){
this.elements[i] = null;
}
this.currentSize = 0;
}
@Override
public boolean isEmpty() {
return this.size() == 0;
}
@Override
public int size() {
return this.currentSize;
}
@Override
public boolean isMember(Object obj) {
return this.count(obj) > 0;
}
@Override
public Iterator iterator() {
return new BagIterator();
}
}
public E MostFrequent(Bag<E> B){
for(E e : B){
int counter = B.count(e)
}
}
包edu.uprm.ece.icom4035.bag;
导入java.util.Iterator;
导入java.util.NoSuchElementException;
公共类静态包{
私有int-currentSize;
私有对象元素[];
私有类BagIterator实现了迭代器{
私人职位;
公共迭代程序(){
此.currentPosition=0;
}
@凌驾
公共布尔hasNext(){
返回此值。currentPosition0;
}
@凌驾
公共迭代器迭代器(){
返回新的迭代函数();
}
}
必须以最有效的方式实施该方法,如有可能,使用bag adt中已经给出的方法
我一直在尝试的是:
package edu.uprm.ece.icom4035.bag;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class StaticBag implements Bag {
private int currentSize;
private Object elements[];
private class BagIterator implements Iterator {
private int currentPosition;
public BagIterator(){
this.currentPosition = 0;
}
@Override
public boolean hasNext() {
return this.currentPosition < currentSize;
}
@Override
public Object next() {
if (hasNext()){
return elements[this.currentPosition++];
}
else {
throw new NoSuchElementException();
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
public StaticBag(int maxSize){
if (maxSize < 1){
throw new IllegalArgumentException("Max size must be at least 1.");
}
this.currentSize = 0;
this.elements = new Object[maxSize];
}
@Override
public void add(Object obj) {
if (obj == null){
throw new IllegalArgumentException("Value cannot be null.");
}
else if (this.size() == this.elements.length){
throw new IllegalStateException("Bag is full.");
}
else {
this.elements[this.currentSize++] = obj;
}
}
@Override
public boolean erase(Object obj) {
int target = -1;
for (int i=0; i < this.size(); ++i){
if (this.elements[i].equals(obj)){
target = i;
break;
}
}
if (target == -1){
return false;
}
else {
this.elements[target] = this.elements[this.currentSize-1];
this.elements[this.currentSize-1] = null;
this.currentSize--;
return true;
}
}
@Override
public int eraseAll(Object obj) {
int copies = 0;
while(this.erase(obj)){
copies++;
}
return copies;
}
@Override
public int count(Object obj) {
int counter = 0;
for (int i=0; i < this.size(); ++i){
if (elements[i].equals(obj)){
counter++;
}
}
return counter;
}
@Override
public void clear() {
for (int i=0; i < this.size(); ++i){
this.elements[i] = null;
}
this.currentSize = 0;
}
@Override
public boolean isEmpty() {
return this.size() == 0;
}
@Override
public int size() {
return this.currentSize;
}
@Override
public boolean isMember(Object obj) {
return this.count(obj) > 0;
}
@Override
public Iterator iterator() {
return new BagIterator();
}
}
public E MostFrequent(Bag<E> B){
for(E e : B){
int counter = B.count(e)
}
}
public E最常去的地方(B包){
(E:B){
int计数器=B计数(e)
}
}
但是我似乎没有想到一种方法来返回循环中频率更高的对象好吧,那么你的问题是什么?我需要一个最频繁方法的实现,我们不只是给你。你认为stackoverflow是什么?嘿,伙计,如果你不想,我只是在这里寻求一点帮助,我很好,我已经花了好几个小时试图解决这个问题,我需要的更少的是你的态度,就像你拥有这个页面一样,就像任何其他人在这个论坛上都没有得到答案一样阅读和关注。你现在的问题似乎是想让我们为你做工作。我们不会的。如果这不是你想要的,请改写你的问题,并向我们展示你的尝试,以及为什么没有成功。