我正在運行一個代碼,該代碼需要一個int列表和一個字符串列表,並將該數組的大小分別增加到適當的大小,然後使用不同的方法對數組進行排序同時還會找到重複的實例。代碼很好,直到我運行排序我的數組並查找重複的方法。我知道正確的輸出應該是什麼,並且應該沒有在intList中發現重複,並且在索引45788處的wordList中找到重複。我已經向其他人請求了執行這個相同簡單任務的幫助,並且與他們具有相同的代碼。我必須離開某個地方,但我找不到位置。我在命令提示符的輸出旁邊附加了兩個方法的照片。感謝您的幫助運行代碼時出現Java運行錯誤
import java.io.*;
import java.util.*;
public class Lab4
{
static final int INITIAL_CAPACITY = 10;
static final int NOT_FOUND = -1; // indexOfFirstDupe returns this value if no dupes found
public static void main (String[] args) throws Exception
{
// ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE
if (args.length < 1)
{
System.out.println("\nusage: C:\\> java Lab4 <numbers file> <words filename>\n\n"); // i.e. C:\> java Lab4 10000ints.txt 172822words.txt
System.exit(0);
}
String[] wordList = new String[INITIAL_CAPACITY];
int[] intList = new int[INITIAL_CAPACITY];
int wordCount = 0, intCount=0;
Scanner intFile = new Scanner(new File(args[0]));
BufferedReader wordFile = new BufferedReader(new FileReader(args[1]));
// P R O C E S S I N T F I L E
while (intFile.hasNextInt()) // i.e. while there are more ints in the file
{
if (intCount == intList.length)
intList = upSizeArr(intList);
intList[intCount++] = intFile.nextInt();
} //END WHILE intFile
//close intfile
intFile.close();
//output text with variables
System.out.format("%s loaded into intList array. size=%d, count=%d\n",args[0],intList.length,intCount);
int dupeIndex = indexOfFirstDupe(intList, intCount);
if (dupeIndex == NOT_FOUND)
{
System.out.format("No duplicate values found in intList\n");
}
else
{
System.out.format("First duplicate value in intList found at index %d\n",dupeIndex);
}
// P R O C E S S S T R I N G F I L E
while (wordFile.ready()) // i.e. while there is another line (word) in the file
{
if (wordCount == wordList.length)
wordList = upSizeArr(wordList);
wordList[wordCount++] = wordFile.readLine();
} //END WHILE wordFile
//closing wordfile
wordFile.close();
//output text again with variables
System.out.format("%s loaded into word array. size=%d, count=%d\n",args[1],wordList.length,wordCount);
dupeIndex = indexOfFirstDupe(wordList, wordCount);
if (dupeIndex == NOT_FOUND)
{
System.out.format("No duplicate values found in wordList\n");
}
else
{
System.out.format("First duplicate value in wordList found at index %d\n",dupeIndex);
}
}
// --------------------------------------------------------------------------------------------------------------------------------
// method to double size of string array
static String[] upSizeArr(String[] fullArr)
{
int length = fullArr.length;
//creating a new array of double size
String[] upsizearr = new String[length*2];
//this for loop assigns each old variable in fullArr
//and assigns it to the new larger array, upsizearr
for(int i = 0; i<length-1; i++)
{
upsizearr[i] = fullArr[i];
}
return upsizearr;
}
// method to double size of int array
static int[] upSizeArr(int[] fullArr)
{
int length = fullArr.length;
//creating new array of double size
int[] upsizearr = new int[length*2];
//this loop does the same as in upSizeArr method,
//assigning all values to new bigger array
for(int i = 0; i<length-1; i++)
{
upsizearr[i] = fullArr[i];
}
return upsizearr;
}
// use Arrays.sort() before scanning for dupe
static int indexOfFirstDupe(int[] arr, int count)
{
Arrays.sort(arr);
int value = NOT_FOUND;
for(int i = (arr.length - count); i < count; i++)
{
if(arr[i] == arr[i-1])
{
value = i;
break;
}
}
return value;
}
// use Array.sort() before scanning for dupe
static int indexOfFirstDupe(String[] arr, int count)
{
Arrays.sort(arr);
int value = NOT_FOUND;
for(int i = (arr.length - count); i < count; i++)
{
if(arr[i] == arr[i-1])
{
value = i;
break;
}
}
return value;
}
} // END CLASS
[] [] 2
如果它「運行錯誤」,它不能是「代碼的作品」和副相反 – Antoniossss
添加您的代碼,而不是圖片的問題。 – TimeToCode
您顯示了異常(在圖片中而不是複製和粘貼),然後甚至沒有顯示它引用的行(第144行,位於indexOfFirstDupe內)... – jonhopkins