Java 将文本文件输入数组,将数组中文本文件的数据随机化,将随机化数据输出到单独的文本文件
我已经成功地将代码复制到数组中(数组实际上是所有201个国家及其互联网使用水平的列表)。列表是按字母顺序排列的,我想随机化数据,这样数组中的数据就没有顺序了。这是我的密码:Java 将文本文件输入数组,将数组中文本文件的数据随机化,将随机化数据输出到单独的文本文件,java,arrays,random,data-structures,Java,Arrays,Random,Data Structures,我已经成功地将代码复制到数组中(数组实际上是所有201个国家及其互联网使用水平的列表)。列表是按字母顺序排列的,我想随机化数据,这样数组中的数据就没有顺序了。这是我的密码: import java.util.*; import java.io.*; public class Unsort { public static void main(String[] args) throws IOException { String[] countries = new String[201];
import java.util.*;
import java.io.*;
public class Unsort {
public static void main(String[] args) throws IOException {
String[] countries = new String[201];
String[] percentages = new String[201];
String[] line = new String[201];
Scanner keyboard = new Scanner(System.in);
Scanner fileIN = new Scanner(new File("F:/CountrySortedAlpha.txt"));
PrintWriter out = new PrintWriter("F:/CountryUnsortedAlpha.txt");
while(fileIN.hasNext()){
for(int i = 0; i < 201; i++){
line[i] = fileIN.nextLine();
System.out.print(line[i] + " " + i + "\n");
}
for(int i = 0; i < 201; i ++){
int randomize = (int) (Math.random() * 201);
System.out.print("\n" + randomize);
}
}
}
}
import java.util.*;
导入java.io.*;
公共类不排序{
公共静态void main(字符串[]args)引发IOException{
字符串[]国家=新字符串[201];
字符串[]百分比=新字符串[201];
字符串[]行=新字符串[201];
扫描仪键盘=新扫描仪(System.in);
Scanner fileIN=new Scanner(新文件(“F:/CountrySortedAlpha.txt”);
PrintWriter out=新的PrintWriter(“F:/CountryUnsortedAlpha.txt”);
while(fileIN.hasNext()){
对于(int i=0;i<201;i++){
行[i]=fileIN.nextLine();
System.out.print(第[i]+“”+i+“\n”行);
}
对于(int i=0;i<201;i++){
int randomize=(int)(Math.random()*201);
系统输出打印(“\n”+随机);
}
}
}
}
我一直在尝试的方法是创建一个随机数来访问位于的数组,但它最终会导致很多冲突。第二个循环只是为了确保随机变量起作用。所以我的问题是:在使用随机数生成器时,如何在不发生冲突的情况下随机化数组中的数据?但是,我不能使用java API预先定义的算法。也许这需要一些额外的代码行,但您可以
- 基于数组创建列表
- 使用
- 如果需要,从无序列表中创建一个数组
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class RandomizeData {
public static void main(String[] args){
ArrayList<String> fileData = new ArrayList<String>(); // arraylist is more dynamic
Scanner fileIN = null;
PrintWriter out = null;
try {
fileIN = new Scanner(new File("C:\\Users\\Yahya\\Desktop\\SortedData.txt")); // for example
out = new PrintWriter(new File("C:\\Users\\Yahya\\Desktop\\UnSortedData.txt")); // for example
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while(fileIN.hasNext()){ // read the entire file of sorted data
fileData.add(fileIN.nextLine());// add each line to the arraylist
}
List<Integer> indexRef = new ArrayList<Integer>(); // create arraylist of integers to be as indices reference
for(int i=0; i<fileData.size(); i++){
indexRef.add(i); // populate it
}
Random rnd = new Random();
for(int i=0; i<fileData.size(); i++){ // for every index (line from the file)
int rndIndex = indexRef.get(rnd.nextInt(indexRef.size())); // create random index
out.println(fileData.get(rndIndex)); // get the data at that index in the arraylist
indexRef.remove(indexRef.indexOf(rndIndex)); // then remove the index from the indexRef arraylist in order not to use it again
}
out.close(); // close the printwriter
}
}
输出:取消排序数据文件:
Canada 200
Ireland 500
Syria 100
U.K 400
U.S.A 300
U.K 400
Ireland 500
U.S.A 300
Syria 100
Canada 200
要是…就好了我的教授说我们不能使用JavaAPI中的算法。这很有意义,因为它是一个数据结构和算法类。请在您的问题中添加此类约束;)另一种方法:生成两个介于0和数组大小-1之间的随机数。然后使用tmp字段交换两个数组值(将位置a的内容复制到temp,将位置b的内容复制到a和temp复制到b)。这样做几次。