首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将数组从另一个方法作为参数传递

将数组从另一个方法作为参数传递
EN

Stack Overflow用户
提问于 2015-11-12 19:48:18
回答 2查看 616关注 0票数 1

我正在尝试读取一个文本文件,然后将这些单词与字典文本文件进行比较,并打印出dictionary.txt文件中找不到的任何单词。我的问题是,我无法知道如何将字典数组从dictionaryRead()方法作为参数传递给binarySearch()方法。

代码语言:javascript
运行
复制
    public class spellChecker {
    public static void main(String[] args) throws Exception
    {
        spellChecker spellcheck = new spellChecker();
        spellcheck.textFileRead(spellcheck.dictionaryRead());
    }

    private String[] dictionaryArray; 
    public String[] dictionaryRead() throws Exception
    {
        // Find and read the file into array
        String token = "";

        // Use scanner for input file
        Scanner dictionaryScan = new Scanner(new File("dictionary.txt")).useDelimiter(",\\s*"); 

        List<String> dictionary = new ArrayList<String>();

        //Check for next line in text file
        while (dictionaryScan.hasNext()) 
        {
          token = dictionaryScan.next();
          dictionary.add(token);
        }
        dictionaryScan.close();

        dictionaryArray = dictionary.toArray(new String[0]);

        return dictionaryArray;
    }

    public void textFileRead(String [] dictionaryArray) throws Exception
    {
        //Get input file path from user
        System.out.println("Please enter a file path: ");
        Scanner scan = new Scanner(System.in);
        String inputFile = scan.nextLine();
        scan.close();

        // Find and read the file
        String word = "";
        // Use scanner for input file
        Scanner inputFileScan = new Scanner(new File(inputFile + ".txt")).useDelimiter(",\\s*");

        //Check for next line in text file
        while (inputFileScan.hasNext()) 
        {
          word = inputFileScan.nextLine();
          binarySearch(word);
        }

        inputFileScan.close();
    }

    public void binarySearch(String word)
    {
        int first = 0;
        int last  = dictionaryArray.length;

        while (first < last) {
          int mid = first + ((last - first) / 2);
          if (word.compareTo(dictionaryArray[mid]) < 0) {
              last = mid;
          } else if (word.compareTo(dictionaryArray[mid]) > 0) {
              first = mid + 1;
          } else {
              if(word != dictionaryArray[mid]){
                System.out.println(word);
              }
          }
        }  
    }
} 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-12 19:51:25

有许多方法可以这样做,因为您正在创建类的实例,因此可以在类中将dictionaryArray作为字段:

代码语言:javascript
运行
复制
private String[] dictionaryArray; 

public void dictionaryRead() throws FileNotFoundException {
    ...
    // Note how String[] is removed on the line below:
    dictionaryArray = dictionary.toArray(new String[0]);
}

现在,dictionaryArray对类的所有实例方法都是可见的,因此您只需在binarySearch方法中按名称引用它:

代码语言:javascript
运行
复制
public void binarySearch(String word) {
    // Use dictionaryArray instead of arr
    ...
}
票数 1
EN

Stack Overflow用户

发布于 2015-11-12 19:56:53

稍微改变一下你的结构

代码语言:javascript
运行
复制
public class spellChecker {
    public static void main(String[] args) throws IOException 
    {
        spellChecker spellcheck = new spellChecker();

        spellcheck.textFileRead(spellcheck.dictionaryRead());
    }

    public String [] dictionaryRead() throws FileNotFoundException
    {
        // Find and read the file into array
        String token = "";

        // Use scanner for input file
        Scanner dictionaryScan = new Scanner(new File("dictionary.txt")).useDelimiter(",\\s*");

        List<String> dictionary = new ArrayList<String>();

        //Check for next line in text file
        while (dictionaryScan.hasNext()) 
        {
          token = dictionaryScan.next();
          dictionary.add(token);
        }
        dictionaryScan.close();

        String[] dictionaryArray = dictionary.toArray(new String[0]);

        return dictionaryArray;
    }

    public void textFileRead(String [] dictionaryArray) throws IOException
    {
        //Get input file path from user
        System.out.println("Please enter a file path: ");
        Scanner scan = new Scanner(System.in);
        String inputFile = scan.nextLine();
        scan.close();

        // Find and read the file
        String word = "";
        // Use scanner for input file
        Scanner inputFileScan = new Scanner(new File(inputFile + ".txt")).useDelimiter(",\\s*");

        //Check for next line in text file
        while (inputFileScan.hasNext()) 
        {
          word = inputFileScan.next();
          binarySearch(dictionaryArray[], word);
        }

        inputFileScan.close();
    }

    public void binarySearch(String[] arr, String word)
    {
        int first = 0;
        int last  = arr.length;

        while (first < last) {
          int mid = first + ((last - first) / 2);
          if (word.compareTo(arr[mid]) < 0) {
              last = mid;
          } else if (word.compareTo(arr[mid]) > 0) {
              first = mid + 1;
          } else {
              if(word != arr[mid]){
                System.out.println(arr[mid]);
              }
          }
        }  
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33680052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档