从另一个方法传递数组作为参数
Passing array from another method as parameter
我正在尝试读取文本文件,然后将单词与字典文本文件进行比较,并打印出 dictionary.txt
文件中未找到的所有单词。我的问题是我不知道如何将字典数组从 dictionaryRead()
方法作为参数传递给 binarySearch()
方法。
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);
}
}
}
}
}
有很多方法可以做到这一点,因为您正在创建 class 的实例,您可以在 class:
中创建一个字段 dictionaryArray
private String[] dictionaryArray;
public void dictionaryRead() throws FileNotFoundException {
...
// Note how String[] is removed on the line below:
dictionaryArray = dictionary.toArray(new String[0]);
}
现在 dictionaryArray
对 class 的所有实例方法可见,因此您可以在 binarySearch
方法中简单地通过名称引用它:
public void binarySearch(String word) {
// Use dictionaryArray instead of arr
...
}
稍微改变一下你的结构
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]);
}
}
}
}
}
我正在尝试读取文本文件,然后将单词与字典文本文件进行比较,并打印出 dictionary.txt
文件中未找到的所有单词。我的问题是我不知道如何将字典数组从 dictionaryRead()
方法作为参数传递给 binarySearch()
方法。
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);
}
}
}
}
}
有很多方法可以做到这一点,因为您正在创建 class 的实例,您可以在 class:
中创建一个字段dictionaryArray
private String[] dictionaryArray;
public void dictionaryRead() throws FileNotFoundException {
...
// Note how String[] is removed on the line below:
dictionaryArray = dictionary.toArray(new String[0]);
}
现在 dictionaryArray
对 class 的所有实例方法可见,因此您可以在 binarySearch
方法中简单地通过名称引用它:
public void binarySearch(String word) {
// Use dictionaryArray instead of arr
...
}
稍微改变一下你的结构
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]);
}
}
}
}
}