java - 复制 ArrayBag 中的所有对象

标签 java arrays generics bag

我目前正在学习 Java 中的不同 DataStructures,其中一种是 Array Bags 和 Linked Bags。我理解数据结构如此重要的原因,以及泛型如何让我们更轻松地以统一的方式处理对象,无论它们的类型如何(如果由于使用数据结构背后的原因,我对该语句的想法或理解是错误的,请告诉我)。

话虽如此,我还没有完全理解泛型和数据结构的某些方面。这个线程没有特别提到那些,因为我将继续单独查找这些东西,但它可能会影响我为什么在正确实现一种方法时遇到问题,该方法将允许我复制给定包的所有项目。

以下是我正在研究的实验室的代码。实验室要求我完成此文件中的某些方法,以便在编译和运行驱动程序时,将适当的答案返回到命令提示符。我相信,当我运行驱动程序时,到目前为止我所做的都是正确的。但是,当我尝试完成 duplicateAll() 方法(下面文件中的倒数第二个方法)时,无论我如何尝试更改它,我都会继续遇到相同的错误。请记住,我的最后一个方法 removeDuplicates() 不完整,但我还没有进入实验室的那个部分。

这是代码:

import java.util.Arrays;
import java.util.Random;

public final class ArrayBag<T> implements BagInterface<T> {

    private final T[] bag;
    private int numberOfEntries;
    private static final int DEFAULT_CAPACITY = 25;

    private boolean initialized = false;
    private static final int MAX_CAPACITY = 10000;

    /** Creates an empty bag whose initial capacity is 25. */
    public ArrayBag() {
        this(DEFAULT_CAPACITY);
    } // end default constructor

    /**
     * Creates an empty bag having a given initial capacity.
     *
     * @param desiredCapacity The integer capacity desired.
     */
    public ArrayBag(int desiredCapacity) {
        if (desiredCapacity <= MAX_CAPACITY) {

            // The cast is safe because the new array contains null entries.
            @SuppressWarnings("unchecked")
            T[] tempBag = (T[]) new Object[desiredCapacity]; // Unchecked cast
            bag = tempBag;
            numberOfEntries = 0;
            initialized = true;
        }
        else
            throw new IllegalStateException("Attempt to create a bag " +
                                            "whose capacity exceeds " +
                                            "allowed maximum.");
    } // end constructor

    /** Adds a new entry to this bag.
    @param newEntry The object to be added as a new entry.
    @return True if the addition is successful, or false if not. */
    public boolean add(T newEntry) {
        checkInitialization();
        boolean result = true;
        if (isArrayFull()) {
            result = false;
        } else { // Assertion: result is true here
            bag[numberOfEntries] = newEntry;
            numberOfEntries++;
        } // end if
        return result;

    } // end add

    /** Throws an exception if this object is not initialized.
     * 
     */
    private void checkInitialization()
    {
        if (!initialized)
             throw new SecurityException("ArrayBag object is not initialized " +
                                        "properly.");
    }

    /** Retrieves all entries that are in this bag.
    @return A newly allocated array of all the entries in the bag. */
    public T[] toArray() {

        // the cast is safe because the new array contains null entries
        @SuppressWarnings("unchecked")
        T[] result = (T[]) new Object[numberOfEntries]; // unchecked cast
        for (int index = 0; index < numberOfEntries; index++) {
            result[index] = bag[index];
        } // end for
        return result;
    } // end toArray

    /** Sees whether this bag is full.
    @return True if the bag is full, or false if not. */
    private boolean isArrayFull() {
        return numberOfEntries >= bag.length;
    } // end isArrayFull

    /** Sees whether this bag is empty.
    @return True if the bag is empty, or false if not. */
    public boolean isEmpty() {
        return numberOfEntries == 0;
    } // end isEmpty

    /** Gets the current number of entries in this bag.
    @return The integer number of entries currently in the bag. */
    public int getCurrentSize() {
        return numberOfEntries;
    } // end getCurrentSize

    /** Counts the number of times a given entry appears in this bag.
    @param anEntry The entry to be counted.
    @return The number of times anEntry appears in the bag. */
    public int getFrequencyOf(T anEntry) {
        checkInitialization();
        int counter = 0;
        for (int index = 0; index < numberOfEntries; index++) {
            if (anEntry.equals(bag[index])) {
                counter++;
            } // end if
        } // end for
        return counter;
    } // end getFrequencyOf

    /** Tests whether this bag contains a given entry.
    @param anEntry The entry to locate.
    @return True if the bag contains anEntry, or false if not. */
    public boolean contains(T anEntry) {
        checkInitialization();
        return getIndexOf(anEntry) > -1;
    } // end contains

    /** Removes all entries from this bag. */
    public void clear() {
        while (!isEmpty()) {
            remove();
        }
    } // end clear

    /** Removes one unspecified entry from this bag, if possible.
    @return Either the removed entry, if the removal was successful,
    or null if otherwise. */
    public T remove() {
        checkInitialization();

        // MODIFY THIS METHOD TO REMOVE A RANDOM ITEM FROM THE BAG
        Random randomNum = new Random();
        if(numberOfEntries > 0){
        int randomKey = randomNum.nextInt(numberOfEntries);
        T result = removeEntry(randomKey);
        return result;
        }else{
            return null;
        }
    } // end remove

    /** Removes one occurrence of a given entry from this bag.
    @param anEntry The entry to be removed.
    @return True if the removal was successful, or false if not. */
    public boolean remove(T anEntry) {
        checkInitialization();
        int index = getIndexOf(anEntry);
        T result = removeEntry(index);
        return anEntry.equals(result);
    } // end remove

    // Removes and returns the entry at a given array index within the array bag.
    // If no such entry exists, returns null.
    // Preconditions: 0 <= givenIndex < numberOfEntries;
    //                  checkInitialization has been called.
    private T removeEntry(int givenIndex) {
        T result = null;
        if (!isEmpty() && (givenIndex >= 0)) {
            result = bag[givenIndex];                   // entry to remove
            bag[givenIndex] = bag[numberOfEntries - 1]; // Replace entry with last entry
            bag[numberOfEntries - 1] = null;            // remove last entry
           numberOfEntries--;
         } // end if
        return result;
    } // end removeEntry

    // Locates a given entry within the array bag.
    // Returns the index of the entry, if located, or -1 otherwise.
    // Precondition: checkInitialization has been called.
    private int getIndexOf(T anEntry) {
        int where = -1;
        boolean stillLooking = true;
        int index = 0;
        while ( stillLooking && (index < numberOfEntries)) {
            if (anEntry.equals(bag[index])) {
                stillLooking = false;
                where = index;
            } // end if
            index++;
        } // end for
    // Assertion: If where > -1, anEntry is in the array bag, and it
    // equals bag[where]; otherwise, anEntry is not in the array
        return where;
    } // end getIndexOf


    /** Override the equals method so that we can tell if two bags contain the same items
     * the contents in the bag.
     * @return a string representation of the contents of the bag */
    public String toString() {

        String result = "Bag{Size:" + numberOfEntries + " ";


        for (int index = 0; index < numberOfEntries; index++) {
            result += "[" + bag[index] + "] ";
        } // end for

        result += "}";
        return result;
    } // end toArray

    /*********************************************************************
     * 
     * METHODS TO BE COMPLETED
     * 
     * 
         ************************************************************************/

    /** Check to see if two bags are equals.  
     * @param aBag Another object to check this bag against.
     * @return True the two bags contain the same objects with the same frequencies.
     */
    public boolean equals(ArrayBag<T> aBag) {
        boolean result = false; // result of comparison of bags
        boolean sameLength = false;

        T[] thisBag = this.toArray();
        T[] otherBag = aBag.toArray();

        Arrays.sort(thisBag);
        Arrays.sort(otherBag);

        if (thisBag.length == otherBag.length){
            sameLength = true;
        }

        if(sameLength){
            for(int index = 0; index < otherBag.length; index++)
            {
                if(thisBag[index].equals(otherBag[index])){
                    result = true;
                }
            }
        }

        return result;
    }  // end equals

    /** Duplicate all the items in a bag.
     * @return True if the duplication is possible.
     */
    public boolean duplicateAll() {
        checkInitialization();
        boolean success = false;
        T[] thisBag = this.toArray();
        if(!thisBag.isEmpty()){
            int originalTotalEntries = numberOfEntries;
            for(int index = 0; index < originalTotalEntries; index++){
                success = thisBag.add(thisBag[index]);
                numberOfEntries++;
            }
        }
        return success;
    }  // end duplicateAll

        /** Remove all duplicate items from a bag
     */
    public void removeDuplicates() {
        checkInitialization();

        // COMPLETE THIS METHOD 

        return;
    }  // end removeDuplicates

} // end ArrayBag

当我尝试编译代码时,出现以下错误:
./ArrayBag.java:260: error: cannot find symbol
      if(!thisBag.isEmpty()){
                 ^
symbol:   method isEmpty()
location: variable thisBag of type T[]
where T is a type-variable:
T extends Object declared in class ArrayBag
./ArrayBag.java:263: error: cannot find symbol
            success = thisBag.add(thisBag[index]);
                             ^
symbol:   method add(T)
location: variable thisBag of type T[]
where T is a type-variable:
T extends Object declared in class ArrayBag
2 errors

我试过 this. 代替 thisBag 如下所示(仅粘贴 duplicateAll() 方法:
public boolean duplicateAll() {
    checkInitialization();
    boolean success = false;
    T[] thisBag = this.toArray();  //not needed anymore
    if(!this.isEmpty()){
        int originalTotalEntries = this.numberOfEntries;
        for(int index = 0; index < originalTotalEntries; index++){
            success = this.add(this[index]);
            numberOfEntries++;
        }
    }
    return success;
}  // end duplicateAll   

我在编译时收到以下错误:
./ArrayBag.java:263: error: array required, but ArrayBag<T> found
            success = this.add(this[index]);
                                   ^
where T is a type-variable:
T extends Object declared in class ArrayBag
1 error

所以我知道,由于我没有正确转换 this,我直接在上面收到错误。但是最后一个上面发布的两个错误是我被挂断的地方。

我的问题是:

为什么我会收到这些错误?我在上一堂 Java 类(class)中收到了关于 cannot find symbol 的类似错误,那是我认为我没有正确实例化的时候?我认为我的方法是正确的,因为我转换了 T[] thisBag = this.toArray(); ,然后尝试使用 thisBag 和方法中代码中的条目。由于这是错误的,我不确定如何使用 generics 和 ArrayBag/Bag DataStructures 清除这些。

另外,我是在适本地处理通用包中条目的重复,还是有我不知道的更好的方法?我正在尝试在此文件中使用 add() 方法对我有利,但显然有困难。在在线查看 Java 的 API 文档时,我发现可能有几种方法可以实现这种复制:
arrays.fillarrays.setAll(T[] array, IntFunction<? extends T> generator)arrays.clone
我想我必须首先确保数组中有足够的固定空间来在内存中复制它。如果没有,我将不得不使用动态数组并将空间加倍并将内容复制两次到一个新数组中。这对我来说是新的,并没有亲自深入研究这种方法(刚刚学习了这个概念)。

与往常一样,我期待听到你们如何解决类似的问题!并告诉我我做错了什么!

ETA:添加提供的答案以及我的修复程序遇到的另一个问题

在@gar 在下面提出建议后,我实现了以下内容:
public boolean duplicateAll() {
    checkInitialization();
    boolean success = false;
    T[] thisBag = this.toArray();  //not needed anymore
    if(!this.isEmpty()){
        int originalTotalEntries = this.numberOfEntries;
        for(int index = 0; index < originalTotalEntries; index++){
            success = this.add(thisBag[index]);
            numberOfEntries++;
        }
    }
    return success;
}  // end duplicateAll

当我这样做时,我开始收到以下错误:
    Exception in thread "main" java.lang.NullPointerException
    at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:325)
    at java.util.ComparableTimSort.sort(ComparableTimSort.java:188)
    at java.util.Arrays.sort(Arrays.java:1246)
    at ArrayBag.equals(ArrayBag.java:234)
    at BagExtensionsTest.checkDuplicateAll(BagExtensionsTest.java:720)
    at BagExtensionsTest.main(BagExtensionsTest.java:52)

我在这个网站上做了一些挖掘,读到这是由数组中的 null 个条目引起的(因此是 java.lang.NullPointerException 。添加了一些 System.out.println 行以查看复制它们时数组大小的变化,并注意到第一个数组(其中包含一个条目) ) 以三个条目结束。我从 numberOfEntries++; 方法的末尾删除了 duplicateAll() 并纠正了我的问题。

只是想我会分享这些信息让其他人知道。

一如既往,很棒的信息!

最佳答案

您在对象和数组上的方法之间感到困惑。您提供的第二次尝试似乎非常接近。 isEmpty方法在您的 ArrayBag上课时 [...]访问器仅用于数组。修改您的代码片段以从数组而不是对象中获取要添加的条目,如下所示:

public boolean duplicateAll() {
    checkInitialization();
    boolean success = false;
    T[] thisBag = this.toArray();  //not needed anymore
    if(!this.isEmpty()){
        int originalTotalEntries = this.numberOfEntries;
        for(int index = 0; index < originalTotalEntries; index++){
            success = this.add(thisBag[index]);
            numberOfEntries++;
        }
    }
    return success;
}  // end duplicateAll

我尚未对此进行测试,因此可能存在其他错误,但希望它能让您走得更远。

关于java - 复制 ArrayBag 中的所有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35181717/

相关文章:

java - 如何从 Google App Engine 读取 RSS?获取 JDOM 无法创建 SAX 解析器

java服务器/客户端保存下载的文件而不是HDD

arrays - Rust 中 array::len 的运行时复杂度是多少?

java - Java 数组与 C++ 数组的性能和内存使用情况

ios - Swift 通用 TableView 数据源和委托(delegate),协议(protocol)支持 NSManagedObjects,无法获取实体属性,运行时错误

java - 在 Selenium Webdriver 出现 Element Not Visible Exception 后继续运行后续步骤

Java 抽象工厂 - 单例

JAVA - 验证二维数组上的对角线

ios - RxSwift - 绑定(bind)到 tableView 的可选问题

java - 使用 gson 反序列化嵌套的泛型类