简体   繁体   中英

How can I make this into a loop?

Writing an LSH program in Java, during this first part I read through 5 different text files and pulled out all the unique words. Then created 5 different shuffling of the words. Now, the code I came up with works, obviously, but I know anytime you copy paste the same block of code a bunch of times, you generally could have done it cleaner using a loop. I just, in this instance, can't figure out how. I feel like its bad practice to do it the way I did, so in the future I'd love to avoid this. Can someone help me figure out how to loop Java variable names? (or some similiar fix for copy/pasting blocks of code like this)

UPDATE: I need to be able to access each uniquely shuffled List later in my program, so I can't simply throw it in a for loop that iterates 5 times overwriting the previous List.

I also do not need to output the lists, that was just for testing the shuffles, sorry that wasn't clear. I updated my comments.

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

}

So, you can simplify it with a single shuffle() method as follow. Like @fge says, why use raw datatype and not generic?

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

Edit: if you want to access it later, declare a variable before entering loop

    //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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM