Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 替换特定编号-用于循环_Java_For Loop - Fatal编程技术网

Java 替换特定编号-用于循环

Java 替换特定编号-用于循环,java,for-loop,Java,For Loop,我有以下代码: List<String> l1_0 = new ArrayList<String>(), l2_0 = new ArrayList<String>(),.....; List<Integer> l1_1 = new ArrayList<Integer>(), l2_1 = new ArrayList<Integer>()......; int lines1 = 0, lines2 = 0, lines3 =

我有以下代码:

List<String> l1_0 = new ArrayList<String>(), l2_0 = new ArrayList<String>(),.....;
List<Integer> l1_1 = new ArrayList<Integer>(), l2_1 = new ArrayList<Integer>()......;
int lines1 = 0, lines2 = 0, lines3 = 0 .....;

        Scanner s1 = new Scanner(new FileReader("file/path//t1.txt"));


    while (s1.hasNext()) {
        l1_0.add(s1.next());
        l1_1.add(s1.nextInt());
        lines1++;
    }
    s1.close();

    func1(l1_0,l1_1,lines);
List l1_0=new ArrayList(),l2_0=new ArrayList(),。。。。。;
列表l1_1=新建ArrayList(),l2_1=新建ArrayList()。。。。。。;
int lines1=0,lines2=0,lines3=0。。。。。;
Scanner s1=新的扫描仪(新的文件读取器(“file/path//t1.txt”);
while(s1.hasNext()){
l1_0.add(s1.next());
l1_1.添加(s1.nextInt());
lines1++;
}
s1.关闭();
func1(l1_0,l1_1,行);
我必须对40个文件执行相同的操作

我们可以创建一个for循环来实现它吗? 我在想一件类似的事情

for (int i=1; i<= 40 ; i++)
{
    Scanner s[i] = new Scanner(new FileReader("file/path//t[i].txt"));
    while (s[i].hasNext()) {
        l[i]_0.add(s[i].next());
        l[i]_1.add(s[i].nextInt());
        lines[i]++;
    }
    s[i].close();
    func1(l[i]_0,l[i]_1,lines[i]);
}

for(int i=1;i如果我理解正确,您希望将数据循环40次。每个文件循环一次

for (int i=0; i< 40 ; i++)
{
    // Initializers for this one file
    List<String> strings = new ArrayList<>();
    List<Integer> nums = new ArrayList<>();
    int lineCount = 0;

    String filename = "t" + i;

    try (Scanner s = new Scanner(new FileReader("file/path/" + filename + ".txt"))) {

        while (s.hasNext()) {
            strings.add(s.next());
            if (s.hasNextInt()) {
                nums.add(s.nextInt());
            }
            lineCount++;
        }
    }

    func1(strings,nums,lineCount);
}
for(int i=0;i<40;i++)
{
//此文件的初始值设定项
列表字符串=新的ArrayList();
List nums=new ArrayList();
int lineCount=0;
字符串filename=“t”+i;
尝试(Scanner s=new Scanner(新文件读取器(“file/path/”+filename+“.txt”)){
而(s.hasNext()){
strings.add(s.next());
如果(s.hasNextInt()){
nums.add(s.nextInt());
}
lineCount++;
}
}
func1(字符串、NUM、行数);
}
或者您可以使用
String.format()


是的,您可以使用循环。请注意,数组是零索引的,您不需要扫描数组。作为一名新的Java程序员,您应该遵循以下Java约定,包括命名变量的约定。而不是
l1\u 0
它应该是
file1line
或类似的约定(例如,没有蛇形格、描述性名称等)@Roddyoffrozenpeas:谢谢:)好的,我承认我可以在我的评论中添加更多细节来解释这个问题需要改进。由于用户是Java新手,我建议通过try/finally或try-with-resources来演示关闭输入流的最佳实践。谢谢@cricket_007。。成功了:)我意识到这是一个愚蠢的问题!!谢谢你的帮助:)
for (int i=1; i<= 40 ; i++){
    Scanner s[i] = new Scanner(new FileReader("file/path//t[i].txt"));
}
"file/path//t" + i + ".txt"
String.format("file/path//t%d.txt",i)