数组已经排序。
然后我要求用户输入一个名称,以便可以在数组中搜索" names"。这样做的目的是让我可以得到名称的索引,这样我就可以从2D数组输出相应的分数"得分"。
这些是我的阵列:
String[] names = new String[10];
int[][] scores = new int[10][3];
这里是二元搜索:
System.out.println("Which student's scores do you want to see?");
String name = scnr.nextLine();
scnr.nextLine();
int index = Arrays.binarySearch(names, name);
System.out.println(index);
我打印索引只是为了检查它是否正确,但我看到它打印出-1。我正确输入输入,没有空格和正确大写。我不确定我做错了什么。
这是我的代码:
import java.util.Scanner;
import java.util.Arrays;
/**
*Write a program that will input 10 students names
* in random order and their corresponding 3 project scores.
* Data will be read into 2 separate arrays: an array of strings
* for the names and a 2D array for the scores.
* Output input data in table form
*Using Binary Search your program will accept user input (name) and output
*the name and its corresponding 3 project scores.
*For each of the 3 projects, output the name of the student(s) who scored
the highest mark.
**/
class NameAndGrades {
public static void main(String[] args) {
//Scanner object to allow user input
Scanner scnr = new Scanner(System.in);
//This array will store the names of the students
String[] names = new String[10];
//This 2D array will store the 3 project scores
int[][] scores = new int[10][3];
//Ask user to input the student names
System.out.println("Enter the 10 student names: ");
for (int index = 0; index < 10; index++) {
System.out.print("Student " + (index + 1) + ": ");
names[index] = scnr.nextLine();
}
//Ask user to enter the corresponding project scores
System.out.println("\nEnter the 3 project grades for each student:\n");
for(int i = 0; i < names.length; ++i)
{
System.out.print(" Enter three project grades for " + names[i] + " : ");
scores[i][0] = scnr.nextInt();
scores[i][1] = scnr.nextInt();
scores[i][2] = scnr.nextInt();
}
System.out.println("Which student's scores do you want to see?");
String name = scnr.nextLine();
scnr.nextLine();
int index = Arrays.binarySearch(names, name);
System.out.println(index);
}
/**Selection sort method made to sort the student names in
* alphabetical order.
@param names names of students
@return the names organized alphabetically
*/
public static String[] selectionSort(String[] names) {
for (int index = 0; index < names.length - 1; ++index) {
int minIndex = index;
for (int j = index + 1; j < names.length; ++j) {
if (names[j].compareTo(names[minIndex]) < 0) {
minIndex = j;
}
}
String temp = names[index];
names[index] = names[minIndex];
names[minIndex] = temp;
}
return (names);
}
以下只是一个可能的替代方案,我的主要问题是为什么我得到-1的指数
除非我可以使用这样的东西进行二进制搜索,否则我从本书中得到了这个例子(但是我需要帮助更改它以便它可以用于String数组):
public static int binarySearch(String[] names, String name)
{
int first = 0;
int last = names.length - 1;
int mid;
int position = -1;
boolean found = false;
while(!found && first <= last)
{
mid = (first + last)/2;
if (names[mid] == name)
{
found = true;
position = mid;
}
else if (names[mid] > name)
last = mid - 1;
else
first = mid - 1;
}
return position;
}
答案 0 :(得分:0)
为什么使用原始数组?您应该使用从学生姓名到学生分数的地图。然后,您可以使用API调用轻松找到它们。例如:
Map<String, List<Integer>> studentMarks = new HashMap(10);
String targetStudent;
//... perform initialization here
System.out.println(studentMarks.get(targetStudent));