【发布时间】:2015-12-01 07:36:00
【问题描述】:
我正在为一些家庭作业而苦苦挣扎。我已经解决了我所有的问题,除了这个。在搜索了网站之后,我决定只问我自己的问题。
该代码旨在制作一个小菜单,询问用户是否愿意将电子邮件输入数据库、搜索以前输入的电子邮件或退出。
当我尝试进行二进制搜索以确定电子邮件是否已在数据库中时,就会出现问题。
错误出现在 addNewEmail 方法的第二行(最后一行)。 提前感谢您的帮助。
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class project14_Brady {
private static final int ADD = 1;
private static final int SEARCH = 2;
private static final int QUIT = 3;
public static void main(String[] args)
{
ArrayList<String> emailAddresses = new ArrayList<String>();
int size = 0;
Scanner input = new Scanner(System.in);
// Priming read
int menu = menuChoice(input);
while(menu != QUIT)
{
if (menu == ADD)
{
System.out.println("Enter the email address");
String email = input.nextLine();
size = addNewEmail(emailAddresses, size, email);
}
else if (menu == SEARCH)
{
String email = autoComplete(emailAddresses, size, input);
if (email != null)
System.out.println("Found: " + email);
else
System.out.println("No matching email was found");
}
else
{
System.out.println("Unanticipated case");
}
// priming read
menu = menuChoice(input);
} // end while
}
public static String autoComplete(ArrayList<String> data, int size, Scanner input)
{
System.out.println("Enter the first letters, one at a time");
String start = "";
while (true)
{
String read = input.nextLine();
start += read;
System.out.println("DEBUG: " + start);
int count=0;
String result="";
for (int i=0; i<size; ++i)
{
System.out.println("DEBUG: " + data.get(i));
if (data.get(i).startsWith(start))
{
result = data.get(i); // keep just the last one
System.out.println(data.get(i));
++count;
}
}
if (count == 1)
return result;
if (count == 0)
return null;
}
}
public static int menuChoice(Scanner keyboard)
{
System.out.println("Please choose from the following menu of choices:");
System.out.println("1. Enter a new email address");
System.out.println("2. Find an existing email address");
System.out.println("3. Quit.");
System.out.println("What is your choice?");
int choice = keyboard.nextInt();
keyboard.nextLine(); // get rid of newline
// Allow the user to re-enter data
while (choice < ADD || choice > QUIT)
{
System.out.println("You must choose a value between 1 and 3");
System.out.println("Please re-enter your choice");
choice = keyboard.nextInt();
keyboard.nextLine();
}
return choice;
}
public static int addNewEmail(ArrayList<String> data, int size, String insertMe)
{
if (data.binarySearch(data, 0, size, insertMe) > 0)
{
System.out.println("That email address has already been inserted");
return size; // already in array
}
if (size == data.size())
{
System.out.println("Too many addresses are stored");
return size; // the array is full already
}
// This is essentially one inner loop of insertion sort
int index;
for (index = size; index > 0 && data.get(index-1).compareTo(insertMe) > 0; --index)
{
data.add(index, data.get(index-1));
}
data.add(index, insertMe);
return size+1;
}
}
【问题讨论】:
-
你在哪里找到这个
binarySearch方法? -
不要转储整个文件,只发布相关部分
-
ArrayList没有binarySearch方法。ArrayList有其他地方的二分查找方法,但不清楚你是否打算使用这些方法? -
我明白了,我试图使用 collections 类中的 binarySearch,但我知道那不行。
标签: java arraylist binary-search