Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-outOfMemory-Heap空间_Java_Arraylist_Junit_Heap_Space - Fatal编程技术网

Java-outOfMemory-Heap空间

Java-outOfMemory-Heap空间,java,arraylist,junit,heap,space,Java,Arraylist,Junit,Heap,Space,因此,我正在尝试完成一个练习,其中要求我实现一个方法,在对象的ArrayList中执行二进制搜索。从练习中: 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 wi

因此,我正在尝试完成一个练习,其中要求我实现一个方法,在对象的ArrayList中执行二进制搜索。从练习中:

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.
二进制搜索
在Main类中,实现一个publicstaticintbinarysearch(arraylistbooks,intsearchedd)方法,该方法搜索它作为参数接收的列表,查找id变量与它作为参数接收的searchedd变量值匹配的书籍。如果找到该书,则该方法应返回其所在的索引,并将其作为参数接收到的列表中。如果找不到该书,则该方法应返回值-1。
该方法必须实现为二进制搜索,它假定列表是有序的。您还应该假设,列表开头的ID总是小于列表末尾的ID。
我创建了两个方法,一个用于检查arraylist是否已排序(isItSorted),另一个用于在上述方法的计算结果为true时执行二进制搜索(binarySearch)。请参阅下文:

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;
    }
publicstaticbooleanistsorted(arraylistbooks){
ArrayList boo=新的ArrayList();
字符串isItSorted=“”;
对于(int i=0;isearchedd){
结束=中间-1;
}else if(books.get(middle.getId()
ArrayList boo=new ArrayList();
字符串isItSorted=“”;
对于(int i=0;i
在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;
Book-prev=books[0];
for(inti=1;ibooks[i].getId())
返回false;
}
返回true;

但是您不需要在binarySearch()内部调用它,因为这样会破坏binarySearch()的功能,使其与linearSearch()一样慢。

请不要将字符串用于isItSorted。请使用布尔值。更高效、更干净的代码。我还怀疑您的isItSorted()实际上,就目前的情况而言,调用isItSorted(books)是实现过程中最慢的部分。您的“binarySearch”最肯定的不是比线性搜索快……在这里实现。@ AminM谢谢你的建议。我将把它改为BooLurn。最简单的方法是二进制搜索:1。看中间,这是你要找的项目吗?2。如果不是,那么决定你想在左边还是右边搜索3。当然,你的二进制搜索看起来没有得到很好的实现。按照我给你的指令从头开始。递归更容易。我明白了。我去掉了for循环。但问题是,用你的isItSorted方法,我看不出ID是否真的被排序了,因为如果第一个(
books.get(0)
)小于数组中的最后一个id,但在这两个id之间,其余的数字没有排序?我的意思是:
books.add(新书(1,“书1”);books.add(新书(2,“书2”);books.add(新书(4,“书4”);books.add(新书(3,“书3”);books.add(新书(7,“书7”))
我想看看整个ID列表是否已订购。有什么建议吗?谢谢!!“订购”这意味着对于列表中的每个x,我现在明白了。谢谢!它仍然失败,因为binarySearch比较慢,因为正如你所说,我正在检查arrayList是否在方法内部排序。我如何使它更快?我不能使用递归方法
        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);
                }
            }
        }
Book prev = books[0];
for (int i = 1; i < books.size(); i++) {
   if (prev.getId() > books[i].getId()) 
      return false;
}
return true;