intのリストと文字列のリストを取得し、配列のサイズを適切なサイズに個別に増やして別の方法で配列をソートするコードを実行していますまた、重複のインスタンスを見つけることもできます。コードは、配列をソートして重複を探すメソッドを実行するポイントまでは問題ありません。私は適切な出力が何であるべきかを知っています。intListに重複はなく、wordListには重複が45788というインデックスがあります。私はこの同じ単純な作業をしている他の人の助けを借りて、同じコードを持っています。私はどこかにいる必要がありますが、私はどこで見つけることができません。私は、コマンドプロンプトの出力の隣に2つの方法の写真を添付しました。おかげで任意の助けコード実行時にJava Run Errorが発生する
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