Java 具有多个键匹配的数组线性搜索
Java Linear Search of Array with Multiple Key Matches
正在尝试执行执行以下操作的程序:
-可以通过用户输入重新运行的程序(while循环)
-允许用户输入所需的数组大小,创建该大小的数组,
使用 for 循环将元素输入到数组中,并输入所需的键
从中进行线性搜索方法
我想将线性搜索方法修改为return,作为一个数组,arrayName[的所有实例的索引值i] == 键值;这是我被卡住的部分。
例如,如果作为用户我输入 4 作为数组大小; 1、2、2、3为元素; 2 作为线性搜索所需的键,然后它将打印数组 {1, 2} 作为键与元素值匹配的索引。
到目前为止,这是我的代码:
import java.util.Scanner; //Imports Scanner from java.util package
public class LinearSearch2 {
public static void main (String[] args) {
//Creates Scanner object for user to input string whether to run program
Scanner input1 = new Scanner(System.in);
System.out.print("Run linear search on an array? ");
System.out.print("(Y = Yes; Type any other character to exit program): ");
String s = input1.next();
char runProgram = s.charAt(0);
/*While loop (this allows for re-running the program with a different
set of inputs in the same run depending on the string Scanner object
input...see while loop-continuation condition below)*/
while (runProgram == 'y' || runProgram == 'Y') {
//Creates another Scanner object for entering the array size as integer
Scanner input2 = new Scanner(System.in);
//Scans in user input of array size
System.out.print("Enter desired array size: ");
int arraySize = input2.nextInt();
//Creates array based on size input
double [] numberArray = new double[arraySize];
//Creates another Scanner object for entering the array numbers as doubles
Scanner input3 = new Scanner(System.in);
//Loop to read in input numbers into created array
for (int i = 0; i < numberArray.length; i++) {
System.out.print("Enter a number: ");
numberArray[i] = input3.nextDouble();
}
//Creates another Scanner object for entering the key as a doubles
Scanner input4 = new Scanner(System.in);
//Scans in user desired key
System.out.print("Enter desired key: ");
double arrayKey = input4.nextDouble();
//Invokes linear search method
int [] keyIndices = linearSearch(numberArray,arrayKey);
//Prints keyIndices array
for (int i = 0; i < keyIndices.length; i++) {
System.out.print(keyIndices[i] + " ");
}
//Requests if user would like to re-run the program
System.out.println("Do another linear search on an array? ");
System.out.print("(Y = Yes; Type any other character to exit program): ");
//Takes new result of string scanner object to determine if to run the program again or exit
String s2 = input1.next();
runProgram = s2.charAt(0);
}
}
//Revised linear search method
public static int [] linearSearch(double[] list, double key) {
int [] resultKeyIndices = new int[];
for (int i = 0; i < list.length; i++) {
if (key == list[i]){
return i;
}
}
return -1;
}
}
问题是分配一个大小合适的数组(匹配数)。此解决方案使用大型数组并在最后调整其大小:
public static int [] linearSearch(double[] list, double key) {
int[] indices = int[list.length];
int n = 0;
for (int i = 0; i < list.length; i++) {
if (key == list[i]){
îndices[n++] = i;
}
}
int[] result = new int[n];
for (int i = 0; i < n; ++i) {
result[i] = indices[i];
}
return result;
}
正在尝试执行执行以下操作的程序:
-可以通过用户输入重新运行的程序(while循环)
-允许用户输入所需的数组大小,创建该大小的数组, 使用 for 循环将元素输入到数组中,并输入所需的键 从中进行线性搜索方法
我想将线性搜索方法修改为return,作为一个数组,arrayName[的所有实例的索引值i] == 键值;这是我被卡住的部分。
例如,如果作为用户我输入 4 作为数组大小; 1、2、2、3为元素; 2 作为线性搜索所需的键,然后它将打印数组 {1, 2} 作为键与元素值匹配的索引。
到目前为止,这是我的代码:
import java.util.Scanner; //Imports Scanner from java.util package
public class LinearSearch2 {
public static void main (String[] args) {
//Creates Scanner object for user to input string whether to run program
Scanner input1 = new Scanner(System.in);
System.out.print("Run linear search on an array? ");
System.out.print("(Y = Yes; Type any other character to exit program): ");
String s = input1.next();
char runProgram = s.charAt(0);
/*While loop (this allows for re-running the program with a different
set of inputs in the same run depending on the string Scanner object
input...see while loop-continuation condition below)*/
while (runProgram == 'y' || runProgram == 'Y') {
//Creates another Scanner object for entering the array size as integer
Scanner input2 = new Scanner(System.in);
//Scans in user input of array size
System.out.print("Enter desired array size: ");
int arraySize = input2.nextInt();
//Creates array based on size input
double [] numberArray = new double[arraySize];
//Creates another Scanner object for entering the array numbers as doubles
Scanner input3 = new Scanner(System.in);
//Loop to read in input numbers into created array
for (int i = 0; i < numberArray.length; i++) {
System.out.print("Enter a number: ");
numberArray[i] = input3.nextDouble();
}
//Creates another Scanner object for entering the key as a doubles
Scanner input4 = new Scanner(System.in);
//Scans in user desired key
System.out.print("Enter desired key: ");
double arrayKey = input4.nextDouble();
//Invokes linear search method
int [] keyIndices = linearSearch(numberArray,arrayKey);
//Prints keyIndices array
for (int i = 0; i < keyIndices.length; i++) {
System.out.print(keyIndices[i] + " ");
}
//Requests if user would like to re-run the program
System.out.println("Do another linear search on an array? ");
System.out.print("(Y = Yes; Type any other character to exit program): ");
//Takes new result of string scanner object to determine if to run the program again or exit
String s2 = input1.next();
runProgram = s2.charAt(0);
}
}
//Revised linear search method
public static int [] linearSearch(double[] list, double key) {
int [] resultKeyIndices = new int[];
for (int i = 0; i < list.length; i++) {
if (key == list[i]){
return i;
}
}
return -1;
}
}
问题是分配一个大小合适的数组(匹配数)。此解决方案使用大型数组并在最后调整其大小:
public static int [] linearSearch(double[] list, double key) {
int[] indices = int[list.length];
int n = 0;
for (int i = 0; i < list.length; i++) {
if (key == list[i]){
îndices[n++] = i;
}
}
int[] result = new int[n];
for (int i = 0; i < n; ++i) {
result[i] = indices[i];
}
return result;
}