2017-04-30 19 views
1

コードを配列にコピーしました(実際には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); 
     } 
    } 
} 
} 

私がしようとしている方法がでアレイにアクセスするために乱数を作ることですが、それは非常に多くの衝突で終わります。 2番目のループは、ランダム変数が機能することを確認することでした。だから私の質問です:どのように乱数ジェネレータを使用している間、私は衝突なしで配列のデータをランダム化するのですか?しかし、Java APIで事前に定義されたアルゴリズムを使用することはできません。

答えて

0

各行に国名とインターネットの使用を含むファイルがあり、同じデータを(各行に)含まれているが異なるファイルライン(無作為化)、あなたはこれを見て持っていることがあります。

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 
1

はたぶんこれは、コードのいくつかの余分なラインを必要としますが、

  • は、必要に応じてCollections.shuffle
  • がシャッフルリストから配列を作成し、使用
  • 配列に基づいてリストを作成することができます。
+0

だけなら...私の教授は、私たちは、Java APIからアルゴリズムを使用することはできませんと述べました。それはデータ構造とアルゴリズムのクラスとして意味があります –

+0

あなたの質問にそのような制約を加えてください;) –

+0

別のアプローチ:0と配列サイズ-1の間にある2つの乱数を生成します。次に、tmpフィールドを使用して2つの配列値を交換します(位置aの内容をtempにコピーし、位置bの内容をaとtempをbにコピーします)。これを数回行います。 –

関連する問題