【问题标题】:Inputting text file to an array, randomizing data from text file in array, outputting randomized data to separate text file将文本文件输入到数组中,从数组中的文本文件中随机化数据,将随机化的数据输出到单独的文本文件中
【发布时间】:2017-04-30 18:26:21
【问题描述】:

我已成功将代码复制到数组中(实际上是所有 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);
        }
    }
}
}

我一直在尝试的方法是创建一个随机数来访问数组,但最终会产生如此多的冲突。第二个循环只是为了确保随机变量有效。所以我的问题是:如何在使用随机数生成器时随机化数组中的数据而不会发生冲突?但是,我不能使用 java API 预先定义的算法。

【问题讨论】:

    标签: java arrays random data-structures


    【解决方案1】:

    也许这需要一些额外的代码行,但你可以

    • 根据数组创建列表
    • 使用Collections.shuffle
    • 如果需要,从混洗列表中创建一个数组。

    【讨论】:

    • 如果……我的教授说我们不能使用来自 java api 的算法。这是有道理的,因为它是一个数据结构和算法类
    • 请在您的问题中添加此类限制;)
    • 另一种方法:生成两个介于 0 和数组大小 -1 之间的随机数。然后使用 tmp 字段交换两个数组值(将位置 a 的内容复制到 temp,将位置 b 的内容复制到 a 和 temp 到 b)。这样做几次。
    【解决方案2】:

    如果我正确理解了您的问题,即您在每一行都有一个包含国家名称和互联网使用情况的文件,并且您想创建一个包含相同数据(在每一行)但在不同行(随机),你可以看看这个:

    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          
    
            }           
    }
    

    示例 我在 SortedData 文件中:

    Canada 200
    Ireland 500
    Syria 100
    U.K 400
    U.S.A 300
    

    输出: UnSortedData 文件:

    U.K 400
    Ireland 500
    U.S.A 300
    Syria 100
    Canada 200
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-02
      • 1970-01-01
      • 2023-04-03
      • 2018-08-08
      • 1970-01-01
      • 1970-01-01
      • 2014-04-13
      • 1970-01-01
      相关资源
      最近更新 更多