ランダムな整数を取り込んだ配列から重複を削除するのに問題があります。乱数を生成する Java クラスを作成し、メイン プログラムでこれらの乱数を呼び出して、これらを .txt ファイルに書き込みました。次に、この .txt ファイルから読み取り、重複を削除して新しい配列に保存します。次に、乱数の新しいセットを新しい .txt ファイルに書き直さなければなりません。最初の行に最小の数値、最後に最大の数値を指定します。したがって、新しいリストでの順序は重要ではありません。
私の問題は、重複を削除する方法がわからないことです。投稿された他の質問から、人々がセットまたはハッシュセットを使用するように言っていることがわかりますが、私はこれらをまだ調査しています。では、配列などをループしてそれらを削除する別の方法はありますか?
import java.io.*;
class MainProg{
public static void main (String[]args){
GenKeys keys = new GenKeys();
//System.out.println(keys.getrandom());
//System.out.println(keys.getrandom());
try{
String f = "keys.txt";
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
for (int i=1; i<=500; i++){
//bw.write(i+ System.getProperty("line.separtor"));
bw.write(keys.getrandom() + "\r\n");
}
// close the file after all the writing has taken place
bw.close ();
} catch (IOException e){
System.out.println ("Error writing to file" + e.toString());
}
// declare a place to store each line as it is read in
String str;
String myArray[] = new String [500];
int i = 0;
try{
FileReader fr = new FileReader("keys.txt");
BufferedReader in = new BufferedReader(fr);
// read in the first line from the file
str = in.readLine();
while(str!=null){
myArray[i] = str;
str = in.readLine();
i++;
}
// close the file
in.close();
}catch(IOException e){
System.out.print(e.toString());
System.out.print("Non-Existant File");
}
int [] mySortedArray = new int [500];
for(int k = 0; k<mySortedArray.length;k++){
for(int j = 0;j<mySortedArray.length;j++){
if(mySortedArray[j] != k){
mySortedArray[k] = j;
System.out.print(mySortedArray[k]);
}
}
}
}
}
}