线性搜索功课与文本文件

时间:2017-12-05 03:37:37

标签: java loops search text-files linear

这是我的作业问题之一,我必须在包含1000000个元素的文本文件中找到编号8675309。我对如何使用线性搜索感到困惑,以使其工作。我的Java文件夹中的文本文件也必须被称为csc210hw5A-datafile?

import java.util.Scanner;
import java.util.NoSuchElementException;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class SKELcsc210hw5A {

// linearSearch: return index of matching element.
//               If not found, return -1.
private static int linearSearch(int[] arr, int N, int query) {
    int idx = -1;
    for(int i = 0; i < N; i++){
                if( arr[i]==query);{
                //stuck here
            }

            }  
    return idx;

}

private static int[] loadFile(String fileName) {
    int[] vals = null;
    int sz = -1;
    try {
        Scanner sc = new Scanner(new FileInputStream(fileName));
        if(sc.hasNextInt())
            sz = sc.nextInt();
        if (sz < 0) {
            System.out.println("File not the right format!");
            return vals;
        }
        vals = new int[sz];
        for(int ii = 0; ii < sz; ii++) {
            vals[ii] = sc.nextInt();
        }
    } catch (FileNotFoundException excptn) {
        System.out.println( excptn.getMessage() );
    } catch (NoSuchElementException excptn) {
        System.out.println("Unexpected end of file!" );
        vals = null;
    }
    return vals;
}

public static void main (String[] args) {
    String fName = "csc210hw5A-datafile.txt";
    if(args.length > 0) {
        fName = args[0]; 
    }
    int[] values;

    System.out.println("Loading... " + fName);
    if((values = loadFile(fName)) == null) {
        System.out.println("Failed to read " + fName + ": Exiting.");
        return;
    }
    System.out.println("Loaded " + fName + " successfully.");

    int query = 8675309;
    if(args.length > 1) {
        query = Integer.parseInt(args[1]);
    }
    System.out.println("Query to search: " + query);
    System.out.println("Total elements to search: " + values.length);

    int idx = linearSearch(values, values.length, query);

    if(idx != -1)
        System.out.println("Query found on line " + (idx+1));
    else
        System.out.println("Query not found");
   }
}

loadFile完成后的所有内容。

2 个答案:

答案 0 :(得分:1)

来自:http://www.geeksforgeeks.org/linear-search/

class LinearSearch {
// This function returns index of element x in arr[]
static int search(int arr[], int n, int x) {
    for (int i = 0; i < n; i++) {
        // Return the index of the element if the element is found
        if (arr[i] == x) { return i; }
    }

    // return -1 if the element is not found
    return -1;
}
} 

这个想法是:扫描你的阵列,直到你发现它为止。当你这样做时,立即返回它的位置。如果你没找到,请返回-1。

一定要引用这个S.O.页面,以及我链接的页面。

答案 1 :(得分:0)

private static int linearSearch(int[] arr, int N, int query) {
    int idx = -1;
    boolean isFound = false;
    for(int i = 0; i < N && !isFound; i++)
    {
       if( arr[i]==query)
       {
          isFound = true;
          idx = i
       }

    }  
    return idx; 
}