Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.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 使用for循环在数组中迭代失败?_Java_Arrays_Iteration - Fatal编程技术网

Java 使用for循环在数组中迭代失败?

Java 使用for循环在数组中迭代失败?,java,arrays,iteration,Java,Arrays,Iteration,(您好,这里是世界级测试仪) 我有一个java应用程序,可以在测试后删除一堆文件,以保持一切整洁,但问题是我似乎无法让它工作,这是我第一次接触数组,它比教程中显示的数组稍微复杂一些,任何帮助都将不胜感激 String[] fileArray; fileArray = new String[8]; fileArray[0] = "/Downloads/file1.csv"; fileArray[1] = "/Downloads/file2.csv"; fi

(您好,这里是世界级测试仪) 我有一个java应用程序,可以在测试后删除一堆文件,以保持一切整洁,但问题是我似乎无法让它工作,这是我第一次接触数组,它比教程中显示的数组稍微复杂一些,任何帮助都将不胜感激

    String[] fileArray;
    fileArray = new String[8];

    fileArray[0] = "/Downloads/file1.csv";
    fileArray[1] = "/Downloads/file2.csv";
    fileArray[2] = "/Downloads/file3.csv";
    fileArray[3] = "/Downloads/file4.csv";
    fileArray[4] = "/Downloads/file5.csv";
    fileArray[5] = "/Downloads/file6.csv";
    fileArray[6] = "/Downloads/file7.csv";
    fileArray[7] = "/Downloads/file8.csv";

    String home = System.getProperty("user.home");
    File filePath = new File(home+fileArray); 
    System.out.println(filePath);

    for (String count: fileArray) {
    if (filePath.exists()) {
        filePath.delete();
        System.out.println("Deleted");
    }
    else
    {
        System.out.println("failed");
        Assert.fail();
    }
    System.out.println(count);
    }

您应该为数组中的每个元素指定新的文件路径,因此需要在
for
body中使用文件。因此,在每次迭代中,您都会进入变量
filePath
数组的下一个元素,然后需要将此变量连接到基本路径
home+filePath
。现在您正在查看所需的文件,您可以创建
file
对象并使用它

String[] fileArray;
fileArray = new String[8];

fileArray[0] = "/Downloads/file1.csv";
fileArray[1] = "/Downloads/file2.csv";
fileArray[2] = "/Downloads/file3.csv";
fileArray[3] = "/Downloads/file4.csv";
fileArray[4] = "/Downloads/file5.csv";
fileArray[5] = "/Downloads/file6.csv";
fileArray[6] = "/Downloads/file7.csv";
fileArray[7] = "/Downloads/file8.csv";

String home = System.getProperty("user.home");


for (String filePath: fileArray) {
    File file = new File(home + filePath); 
    System.out.println(filePath);
    if (file.exists()) {
        file.delete();
        System.out.println("Deleted");
    } else {
        System.out.println("failed");
        Assert.fail();
    }
}
似乎您希望在变量
count
中可以看到许多迭代文件。在这种情况下,它不是这样工作的。这种形式的
for
的作用如下:
for(String arrayElement:arrayToWorkWith)
-意味着在变量
arrayElement
的每次迭代中,都会将数组
arrayToWorkWith
中的下一个元素放入。如果您需要在迭代过程中计算元素的数量,您可以引入单独的变量并将其递增,或者使用另一种形式的
for
cycle-
for(int i=0;i

这样试试看

 String[] fileArray;
    fileArray = new String[8];

    fileArray[0] = "/Downloads/file1.csv";
    fileArray[1] = "/Downloads/file2.csv";
    fileArray[2] = "/Downloads/file3.csv";
    fileArray[3] = "/Downloads/file4.csv";
    fileArray[4] = "/Downloads/file5.csv";
    fileArray[5] = "/Downloads/file6.csv";
    fileArray[6] = "/Downloads/file7.csv";
    fileArray[7] = "/Downloads/file8.csv";

    String home = System.getProperty("user.home");
    //File filePath = new File(home+fileArray);  thats wrong here and will give you a invalid file anyway as you concatenating a string with an object


    for (String file: fileArray) {
    File filePath = new File(home+file); //here you need to define the file
    if (filePath.exists()) {
        filePath.delete();
        System.out.println("Deleted");
    }
    else
    {
        System.out.println("failed");
        Assert.fail();
    }
    System.out.println(file);
    }

在try-catch中包装您的代码,并请输入您遇到的异常或错误您认为是什么
File-filePath=new-File(home+fileArray)
Dos?@Chief我在想我可以用它作为目录并用它进行迭代,我错了:当你删除
count
变量但仍然输出它时,DIt不会编译。@s谢谢,这是错误的分解。我现在重命名了变量。