2017-06-27 70 views
-1

試圖執行一個程序,執行以下操作:與多鍵部分的Java的線性搜索匹配

- 程序可以由用戶輸入重新運行(while循環)

- 允許用戶輸入期望的陣列大小,創建該尺寸的陣列, 使用for循環進入元件到陣列,並輸入從其中進行線性搜索方法

我想修改線性搜索所期望的按鍵 方法返回,作爲一個數組,其中arrayName [i] ==鍵值爲的所有實例的索引值;這是我陷入困境的部分。

例如,如果用戶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; 
    } 
} 
+0

您能否澄清一下您的卡住的確切位置? –

回答

0

的問題是分配適當的大小(匹配的數)的數組。此解決方案使用大陣列並在最後調整它的大小:

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; 
    } 
+0

真棒,這就是我一直在尋找的感謝! – Andrew