使用for循环迭代数组 - 保持失败?

时间:2016-09-09 09:04:09

标签: java arrays iteration

(Hello world level tester) 我有一个java应用程序来删除测试后的一堆文件,以保持一切清洁,但问题是我似乎无法让它工作,这是我第一次触及一个阵列而且它& #39;比他们在教程中显示的那些稍微复杂一点,任何帮助都将非常感激。

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

2 个答案:

答案 0 :(得分:1)

您应该为数组中的每个元素连接新的文件路径,因此需要在for正文中使用文件。因此,在每次迭代中,您都会获得数组的变量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循环 - for (int i = 0; i < fileArray.length; i++)

答案 1 :(得分:0)

以这种方式试试

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