Tag: binary search

Collections.binarySearch难度大

我是JAVA和Netbeans的新手,这就是我要做的事情: 用户可以在输入框中写入CD标题,然后按“删除”按钮从列表中删除CD。 如果集合中不存在CD,则可以在发件箱中显示消息以说明此情况。 我必须使用Collections.binarySearch()来执行此操作。 这只是整个计划的一部分,但我已经弄明白了其余部分。 这就是我所做的: ArrayList songs = new ArrayList(); Collections.addAll(songs, “Metric – Fantasies”, “\nBeatles – Abbey Road”, “\nPearl Jam – Ten”, “\nDoors – Alive”, “\nThe Rolling Stones – Gimme Shelter\n”); Collections.sort(songs, String.CASE_INSENSITIVE_ORDER); Collections.binarySearch(songs,””,String.CASE_INSENSITIVE_ORDER); String delete=songs.remove(songs.size()-1); String out=””; String Out = null; for (int i = 0; i < songs.size(); i++) Out=out + songs;{ […]

二进制搜索计算平方根(Java)

我需要帮助编写一个使用二进制搜索的程序来递归计算输入非负整数的平方根(向下舍入到最接近的整数)。 这是我到目前为止: import java.util.Scanner; public class Sqrt { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print(“Enter A Valid Integer: “); int value = console.nextInt(); calculateSquareRoot(value); } public static int calculateSquareRoot(int value) { while (value > 0) { double sqrt = (int) Math.sqrt(value); System.out.println(sqrt); } return -1; } } 它必须使用二进制搜索来计算平方根这一事实让我感到困惑。 如果有人对如何做到这一点有任何建议,将不胜感激。 谢谢