Iv 现在创建了一个数组,我必须从数组中删除重复项并将重复项写入 txt 文件,并将此随机数按从高到低的顺序排列,但如果有人至少可以告诉我如何让这些重复的整数脱离数组并将重复项放入已经完美的变量中。这是我的代码。谢谢!没有数组列表请。
import java.util.Random;
import java.io.*;
import java.lang.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Scanner;
public class MainProg extends GenKeys {
public static void main(String[] args) {
//int x = random();
try {
BufferedWriter out = new BufferedWriter(new FileWriter("file.txt"));
for (int z = 0; z < 500; z++) {
int x = random();
out.write(x + System.getProperty("line.separator"));
}
out.close();
readFromfile();
} catch (IOException e) {
System.out.print(e);
}
}
public static void readFromfile() throws IOException {
int[] numbers = new int[500];
int result, searchValue;
int index = 0;
// Open the file.
File file = new File("file.txt");
Scanner inputFile = new Scanner(file);
int w = 0;
for (int i = 0; i < numbers.length; i++) {
if (i == 0 || numbers[i] != numbers[i - 1]) {
numbers[w++] = numbers[i];
while (inputFile.hasNextInt() && index < numbers.length) {
numbers[index] = inputFile.nextInt();
System.out.println(numbers[index]);
index++;
}
}
}
// Close the file.
inputFile.close();
}
}
GenKeys 方法
import java.util.Random;
public class GenKeys {
static int x;
public static int random(){
for (int i = 0; i < 250; i++) {
x = (int) (Math.random() * 250);
}
return x;
}
}