我怎样才能把它变成一个循环?

时间:2014-02-26 07:59:43

标签: java coding-style

用Java编写LSH程序,在第一部分中,我通读了5个不同的文本文件,并取出了所有独特的单词。然后创建了5个不同的洗牌单词。现在,我提出的代码很明显,但是我知道,无论何时你复制粘贴相同的代码块,你通常可以使用循环更干净。在这种情况下,我只是无法弄清楚如何。我觉得这样做是不好的做法,所以将来我很想避免这种做法。有人可以帮我弄清楚如何循环Java变量名吗? (或者像这样复制/粘贴代码块的类似修复)

更新:我需要能够在我的程序中稍后访问每个唯一的混乱List,所以我不能简单地将它抛入一个迭代5次覆盖前一个List的for循环。

我也不需要输出列表,这只是用于测试shuffle,抱歉不清楚。我更新了我的评论。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;


public class LSH {

    public static void main(String[] args) throws FileNotFoundException {

        //Find all unique words in the Files in dir /filestxt
        HashSet words = new HashSet();
        File dir = new File("filestxt");
        for (File f : dir.listFiles()) {
            Scanner in = new Scanner(f);

            while(in.hasNextLine()) {
                String line = in.nextLine();
                words.add(line);
            }//end while
        }//end for

        //Create 5 different shufflings of the words
        LinkedList shuffle1 = new LinkedList();
        LinkedList shuffle2 = new LinkedList();
        LinkedList shuffle3 = new LinkedList();
        LinkedList shuffle4 = new LinkedList();
        LinkedList shuffle5 = new LinkedList();
        for (Object s : words) {
            shuffle1.add(s.toString());
        }//end for
        for (Object s : words) {
            shuffle2.add(s.toString());
        }//end for
        for (Object s : words) {
            shuffle3.add(s.toString());
        }//end for
        for (Object s : words) {
            shuffle4.add(s.toString());
        }//end for
        for (Object s : words) {
            shuffle5.add(s.toString());
        }//end for
        Collections.shuffle(shuffle1);
        Collections.shuffle(shuffle2);
        Collections.shuffle(shuffle3);
        Collections.shuffle(shuffle4);
        Collections.shuffle(shuffle5);

        //This block for testing purposes only
        System.out.println(shuffle1);
        System.out.println(shuffle2);
        System.out.println(shuffle3);
        System.out.println(shuffle4);
        System.out.println(shuffle5);

    }//end main

}

2 个答案:

答案 0 :(得分:0)

因此,您可以使用单个shuffle()方法简化它,如下所示。就像@fge所说,为什么要使用原始数据类型而不是通用?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class LSH {

   public static List<String> shuffle(Set<String> words) {
      List<String> list = new LinkedList<String>();
      for (String word : words) {
         list.add(word);
      }
      return list;
   }

   public static void main(String[] args) throws FileNotFoundException {
      //Find all unique words in the Files in dir /filestxt
      Set<String> words = new HashSet<String>();
      File dir = new File("filestxt");
      for (File f : dir.listFiles()) {
         Scanner in = new Scanner(f);

         while (in.hasNextLine()) {
            String line = in.nextLine();
            words.add(line);
         }//end while
      }//end for

      //Create 5 different shufflings of the words
      List<String> shuffle1 = shuffle(words);
      List<String> shuffle2 = shuffle(words);
      List<String> shuffle3 = shuffle(words);
      List<String> shuffle4 = shuffle(words);
      List<String> shuffle5 = shuffle(words);

      Collections.shuffle(shuffle1);
      Collections.shuffle(shuffle2);
      Collections.shuffle(shuffle3);
      Collections.shuffle(shuffle4);
      Collections.shuffle(shuffle5);

      System.out.println(shuffle1);
      System.out.println(shuffle2);
      System.out.println(shuffle3);
      System.out.println(shuffle4);
      System.out.println(shuffle5);
   }//end main
}

答案 1 :(得分:0)

public static void main(String[] args) throws FileNotFoundException {
    HashSet<String> words = new HashSet<String>();
    File dir = new File("filestxt");
    for (File f : dir.listFiles()) {
        Scanner in = new Scanner(f);
        while(in.hasNextLine()) {
            words.add(in.nextLine());
        }//end while
    }//end for

    //Create 5 different shufflings of the words
    for (int i = 0; i < 5; i++) {
        List<String> shuffle = new ArrayList<String>();
        for (String s : words) {
            shuffle.add(s);
        }//end for
        Collections.shuffle(shuffle);
        System.out.println(shuffle);
     }
}

编辑:如果您想稍后访问它,请在进入循环之前声明变量

    //Create 5 different shufflings of the words
    List<ArrayList<String>> listOlists = new ArrayList<ArrayList<String>>();
    for (int i = 0; i < 5; i++) {
        ArrayList<String> shuffle = new ArrayList<String>();
        for (String s : words) {
            shuffle.add(s);
        }//end for
        Collections.shuffle(shuffle);
        listOlists.add(shuffle);
        System.out.println(shuffle);
    }
    //access it later
    for (List<String> arrayList : listOlists) {
        System.out.println(arrayList);
    }