UnitedStates类是二进制搜索类
public class UnitedStates
{
// instance variables
private ArrayList <State> states;
public UnitedStates()
{
states = new ArrayList <State> ();
readFile();
printStates();
searchStates();
}
public void searchStates()
{
Scanner keyboard = new Scanner(System.in);
String ans = "y";
System.out.println();
System.out.println("=====================");
System.out.println(" Searching");
System.out.println("=====================");
while(ans.equalsIgnoreCase("y"))
{
System.out.println();
System.out.print("Enter state -->");
String stateName = keyboard.nextLine();
State state = binarySearch(states, stateName);
if(state == null)
System.out.println("State not found");
else
{
System.out.println();
System.out.println("State Name = " + state.getName());
System.out.println("State Capital = " + state.getCapital());
System.out.println("State Nickname = " + state.getNickname());
System.out.println("State Population = " + state.getPopulation());
System.out.println();
}
System.out.println();
System.out.print("Search again[Y/N]?");
ans = keyboard.nextLine();
}
}
// Performs a binarySearch on states searching for key
// If key is found it returns the State object that
// corresponds to key; otherwise it returns null
public int binarySearch(ArrayList<State> states, String key)
{
int left = 0;
int right = states.size() - 1;
while(left <= right)
{
int midpoint = (left + right) / 2;
int result = states.get(midpoint).compareTo(key);
if(result == 0)
{
return midpoint;
}
else if(result < 0)
{
left = midpoint + 1;
}
else
{
right = midpoint - 1;
}
}
return -1;
}
public void printStates()
{
for(State s : states)
{
System.out.printf("%-15s", s.getName());
System.out.printf("%-15s", s.getCapital());
System.out.printf("%-25s", s.getNickname());
System.out.printf("%10s\n", s.getPopulation());
}
}
public void readFile()
{
Scanner scan = null;
try
{
scan = new Scanner(new File("states.txt"));
}
catch(FileNotFoundException ex)
{
System.out.println("File not Found!");
}
String name;
String capital;
String nickname;
int population;
while(scan.hasNextLine())
{
name = scan.nextLine();
capital = scan.nextLine();
nickname = scan.nextLine();
population = scan.nextInt();
if(scan.hasNextLine())
{
String dummy = scan.nextLine();
}
State state = new State(name, capital, nickname, population);
states.add(state);
}
}
}
这是州级
public class State implements Comparable
{
// instance variables
private String name;
private String capital;
private String nickname;
private int population;
public State(String n, String c, String nn, int p)
{
name = n;
capital = c;
nickname = nn;
population = p;
}
public String getName()
{
return name;
}
public String getCapital()
{
return capital;
}
public String getNickname()
{
return nickname;
}
public int getPopulation()
{
return population;
}
// Comparable Interface method
// Casts obj to a String, then calls the String class's
// compareTo method to compare the name of this state
// to the name passed in the parameter list. It returns
// either a positive number, negative number, or zero.
@Override
public int compareTo(Object stateName)
{
State state = (State)stateName;
String otherStateName = state.getName();
return name.compareTo(otherStateName);
}
}
我目前正在开发一个二进制搜索项目,我遇到了解决我知道如何解决的错误的问题。我试图在UnitedStates类中的行(&#34; State state = binarySearch(states,stateName);&#34;)中调用binarySearch方法,但它给出的错误是&#34;不兼容的类型:int不能转换为州&#34;。我不知道如何将int类型转换为类,如果有人能告诉我如何解决这个错误,我真的很感激。
答案 0 :(得分:1)
binarySearch
每
int
public int binarySearch(ArrayList<State> states, String key)
binarySearch
的评论错误。
// If key is found it returns the State object that
// corresponds to key; otherwise it returns null
也许应该是
// If key is found it returns the index of the State
// object that corresponds to key; otherwise it returns -1.
您尝试将结果int
分配给州
State state = binarySearch(...);
也许分配
int stateIndex = binarySearch(...);
State state = null;
if (stateIndex >= 0) {
state = states.get(stateIndex);
...
} else {
// deal with error.
}