ソートアルゴリズム(バブル)を作成し、10000個の一意の値を使用しました
int BubArray[] = new int[]{#10000 unique unsorted values#};
コード内の10000個の整数の代わりに、整数をファイルに入れ、ファイルを呼び出す方法を考えていました。
また、それらはどの形式になりますか (カンマ、スペースを使用しますか?) よくわかりません。
どんな助けでも感謝します、ありがとう。
これは正解ではありません。ファイルの使い方をヒントにするだけですが、必要に応じてコードを変更して使用できるようにすることができます。
try {
String str;
String[] temp;
BufferedReader br = new BufferedReader(new FileReader("your filepath"));
while ((str= br.readLine()) != null) {
temp = str.split(";"); // seperator words bye;
System.out.println(str);
for(int i = 0; i<temp.lenght; i++)
System.out.println(temp[i]);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
Put it in a File
separated by line breaks and use a Scanner
to read line by line, putting them into an array.
An example taken from Scanner documentation page:
Scanner sc = new Scanner(new File("myNumbers"));
while (sc.hasNextLong()) {
long aLong = sc.nextLong();
}
You can easily modify it to get int
instead of long
using hasNextInt()
and nextInt()
methods.
ソート方法をテストするためにいくつかの乱数が必要であると仮定します。
int amount = 1000;
int[] array = new int[amount];
Random rand = new Random();
for (int i = 0; i < array.length; i++) {
array[i] = rand.nextInt();
}
入力に1000個の異なる一意の値を与えたい場合、ランダムはお勧めできません。ランダムは、同じ値を複数回与えることができます。