Java 我怎样才能把它做成一个循环?
在用Java编写LSH程序的第一部分中,我通读了5个不同的文本文件,并提取了所有独特的单词。然后创建了5种不同的单词混洗。现在,我提出的代码显然是有效的,但我知道,只要你复制粘贴同一代码块很多次,你通常都可以使用循环更干净地完成它。我只是,在这种情况下,不知道怎么做。我觉得像我这样做是不好的做法,所以将来我希望避免这种情况。有人能帮我弄清楚如何循环Java变量名吗?(或者类似于复制/粘贴这样的代码块的修复程序) 更新:我需要能够在以后的程序中访问每个经过唯一洗牌的列表,所以我不能简单地将其放入一个for循环中,该循环将覆盖上一个列表5次 我也不需要输出列表,那只是为了测试洗牌,很抱歉不清楚。我更新了我的评论Java 我怎样才能把它做成一个循环?,java,coding-style,Java,Coding Style,在用Java编写LSH程序的第一部分中,我通读了5个不同的文本文件,并提取了所有独特的单词。然后创建了5种不同的单词混洗。现在,我提出的代码显然是有效的,但我知道,只要你复制粘贴同一代码块很多次,你通常都可以使用循环更干净地完成它。我只是,在这种情况下,不知道怎么做。我觉得像我这样做是不好的做法,所以将来我希望避免这种情况。有人能帮我弄清楚如何循环Java变量名吗?(或者类似于复制/粘贴这样的代码块的修复程序) 更新:我需要能够在以后的程序中访问每个经过唯一洗牌的列表,所以我不能简单地将其放入一
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
}
因此,您可以使用一个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
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.*;
公共类LSH{
公共静态列表洗牌(设置字){
列表=新建LinkedList();
for(字符串字:字){
列表。添加(word);
}
退货清单;
}
公共静态void main(字符串[]args)引发FileNotFoundException{
//在dir/filestxt中查找文件中的所有唯一单词
Set words=新HashSet();
File dir=新文件(“filestxt”);
对于(文件f:dir.listFiles()){
扫描仪输入=新扫描仪(f);
while(在.hasNextLine()中){
String line=in.nextLine();
添加(行);
}//结束时
}//结束
//创建5个不同的单词混洗
列表shuffle1=无序排列(单词);
列表shuffle2=无序排列(单词);
List shuffle3=洗牌(单词);
列表shuffle4=无序排列(单词);
List shuffle5=随机播放(单词);
收藏。洗牌(洗牌1);
收藏。洗牌(shuffle2);
收藏。洗牌(洗牌3);
收藏。洗牌(洗牌4);
收藏。洗牌(洗牌5);
System.out.println(shuffle1);
System.out.println(shuffle2);
System.out.println(shuffle3);
System.out.println(shuffle4);
System.out.println(shuffle5);
}//端干管
}
公共静态void main(字符串[]args)引发FileNotFoundException{
HashSet words=新的HashSet();
File dir=新文件(“filestxt”);
对于(文件f:dir.listFiles()){
扫描仪输入=新扫描仪(f);
while(在.hasNextLine()中){
words.add(在.nextLine()中);
}//结束时
}//结束
//创建5个不同的单词混洗
对于(int i=0;i<5;i++){
List shuffle=new ArrayList();
for(字符串s:单词){
洗牌。添加(s);
}//结束
收藏。洗牌(洗牌);
System.out.println(洗牌);
}
}
编辑:如果您想稍后访问它,请在进入循环之前声明一个变量
//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);
}
//创建5种不同的单词混洗
List listOlists=new ArrayList();
对于(int i=0;i<5;i++){
ArrayList shuffle=新的ArrayList();
for(字符串s:单词){
洗牌。添加(s);
}//结束
收藏。洗牌(洗牌);
添加(洗牌);
System.out.println(洗牌);
}
//稍后再访问它
对于(列表阵列列表:列表列表列表列表){
System.out.println(arrayList);
}
为什么你的集合和列表是原始的而不是通用的?因为这是我学Java的第二年,我很容易懒惰。我们将记住在将来不要使用原始类型,除非这样做有特定的优势。谢谢这很好,如果我只想输出洗牌的话。我现在意识到我的问题不清楚。我需要在我的程序中访问并使用所有5个无序列表,所以我需要能够引用它们。(我只做了一个System.out.println()用于测试目的)如果我这样做,我将如何分别引用它们?
//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);
}