Java-内存不足-堆空间

时间:2020-06-25 22:18:45

标签: java arraylist junit heap space

因此,我正在尝试完成一个练习,要求我实现在对象的ArrayList中执行 binary search 的方法。来自练习:

Binary search

In the Main-class, implement a method public static int binarySearch(ArrayList<Book> books, int searchedId), which searches the list it received as a parameter, for a book with an id variable that matches the value of searchedId variable it received as a parameter. If that book is found the method, should return the index it's located at, in the list it received as a parameter. If the book isn't found, the method should return the value -1.

The method must be implemented as a binary search, which assumes the list is ordered. You should also assume, that the ids towards the beginning of the list, are always smaller than the ids towards the end of the list.

我创建了两种方法,一种用于检查arraylist是否已排序( isItSorted ),另一种将在上述方法评估为true时执行二进制搜索( binarySearch < / strong>)。请看下面:

public static boolean isItSorted(ArrayList<Book> books) {
        ArrayList<String> boo = new ArrayList<>();
        String isItSorted = "";
        for (int i = 0; i < books.size(); i++) {
            for (int j = i + 1; j < books.size(); j++) {
                if (books.get(i).getId() < books.get(j).getId()) {
                    isItSorted = "true";
                    boo.add(isItSorted);
                } else {
                    isItSorted = "false";
                    boo.add(isItSorted);
                }
            }
        }
        if (!(boo.contains("false"))) {
            return true;
        }
        return false;
    }

    public static int binarySearch(ArrayList<Book> books, long searchedId) {
        if (searchedId < 0 || books.isEmpty()) {
            return -1;
        } else if (isItSorted(books)) {
            int start = 0;
            int end = books.size() - 1;
            int middle = (start + end) / 2;

            if (books.get(middle).getId() == searchedId) {
                return middle;
            } else if (books.get(middle).getId() > searchedId) {
                end = middle - 1;
            } else if (books.get(middle).getId() < searchedId) {
                start = middle + 1;
            }
            
            while (start <= end) {
                if (books.get(start).getId() == searchedId) {
                    return start;
                }
                start++;
            }
        }
        return -1;
    }

在这些Java文件中,有一个测试包,用于测试我的解决方案是否正确。虽然95%的测试成功,但是当到达下面的方法(将执行时间与其他方法(线性搜索)进行比较)时,出现错误 Java outOfMemory堆空间。 我使用NetBeans。 我已经尝试了JVM命令。 我的解决方案似乎可以处理所有尝试过的对象,因此下面的测试代码可能有问题吗?

@Test
    @Points("07-05.2")
    public void binarySearchIsFasterThanLinearSearch() throws Throwable {
        ArrayList<Book> books = generateBooks(10000);
        Collections.sort(books, (k1, k2) -> k1.getId() - k2.getId());

        int searched = 1000001;
        long bSearchStart = System.nanoTime();
        int binarySearchId = Searching.binarySearch(books, searched);
        long bSearchEnd = System.nanoTime();
        assertTrue("When binary search does not find what was searched for, it must return -1", binarySearchId == -1);
        long lSearchStart = System.nanoTime();
        int linearSearchId = Searching.linearSearch(books, searched);
        long lSearchEnd = System.nanoTime();
        assertTrue("When linear search does not find what was searched for, it must return -1", linearSearchId == -1);

        long bSearchTime = bSearchEnd - bSearchStart;
        long lSearchTime = lSearchEnd - lSearchStart;

        assertTrue("When there are 10000 books to search, and the searched book is not found, binary search should be a lot faster than linear search. Current this isn't so", bSearchTime * 2 < lSearchTime);
    }

1 个答案:

答案 0 :(得分:1)

        ArrayList<String> boo = new ArrayList<>();
        String isItSorted = "";
        for (int i = 0; i < books.size(); i++) {
            for (int j = i + 1; j < books.size(); j++) {
                if (books.get(i).getId() < books.get(j).getId()) {
                    isItSorted = "true";
                    boo.add(isItSorted);
                } else {
                    isItSorted = "false";
                    boo.add(isItSorted);
                }
            }
        }

向ArrayList boo中添加大约1亿个项目。

如果要检查某项是否已排序,可以使用更简单的代码:

Book prev = books[0];
for (int i = 1; i < books.size(); i++) {
   if (prev.getId() > books[i].getId()) 
      return false;
}
return true;

但是您不需要在binarySearch()中调用它,因为这将破坏binarySearch()的目的并使它变得与linearSearch()一样慢。