我如何使这成为一个循环? [英] How can I make this into a loop?

查看:129
本文介绍了我如何使这成为一个循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中编写一个LSH程序,在第一部分中我通过5个不同的文本文件读出所有独特的单词。然后创造了5个不同的混乱的话。现在,代码我想出了作品,显然,但我知道,无论何时你复制粘贴相同的代码块一次,你一般可以做到使用一个循环更清洁。我只是,在这种情况下,不能弄清楚如何。我觉得它的坏习惯做到我的方式,所以在将来我想避免这样。有人可以帮我找出如何循环Java变量名? (或类似这样的复制/粘贴代码块的一些类似修复)

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)

更新:我需要能够在我的程序中访问每个唯一的shuffled列表,所以我不能简单地将它放在迭代5次的for循环中覆盖之前的列表。

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

}


推荐答案

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

编辑:如果您想稍后再访问,一个变量在进入循环之前

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

这篇关于我如何使这成为一个循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆