代码之家  ›  专栏  ›  技术社区  ›  Waleed Amjad

Java ArrayList中的对象不会得到更新

  •  0
  • Waleed Amjad  · 技术社区  · 14 年前

    解决了的:

    这就是问题所在:

        current.addFolder(folder); (in the final else clause of the if statement)
    

    添加了一个新文件夹,但不能保证传递的文件夹就是添加的文件夹,如果文件夹已经存在,它可能什么也不做,所以为了克服这个问题,我将addFolder改为返回实际的文件夹(例如,如果它已经存在),并将folder指定给该返回值。这就成功了,所以现在我有了:

        folder = current.addFolder(folder);
        current = folder;
    


    这将是一个很长的职位,希望你能理解我在说什么,我感谢任何帮助。谢谢


    基本上,我已经创建了一个可以读取ZIP和RAR文件的个人非商业项目(我不打算发布)。它只能读取存档中的内容、文件夹中的文件夹、文件夹中的文件及其属性(如上次修改日期、上次修改时间、CRC校验和、未压缩大小、压缩大小和文件名)。它也不能提取文件,所以如果可以的话,它实际上是一个ZIP/RAR查看器。

    不管怎样,这和我的问题有点无关,但我想我应该给你一些背景资料。

    现在我的问题是:

    我可以成功地列出一个ZIP存档中的所有文件夹和文件,所以现在我想获取原始输入并以某种有用的方式将其链接在一起。我创建了两个类:ArchiveFile(表示ZIP中的文件)和ArchiveFolder(表示ZIP中的文件夹)。它们都有一些有用的方法,比如getLastModifiedDate、getName、getPath等等。 (将其视为文件夹中的文件和文件夹)。

    现在我想把我的原始输入填充到一个 ArchiveFolder,它将在ArchiveFile的ArrayList中的ZIP根目录中包含所有文件,在ArchiveFolder的ArrayList中的ZIP根目录中包含任何其他文件夹(这个过程可以像连锁反应一样继续进行(该ArchiveFolder中有更多的文件/文件夹等)。

    所以我想出了以下代码:

    while (archive.hasMore()) {
        String path = ""; 
        ArchiveFolder current = root; 
        String[] contents = archive.getName().split("/");
    
        for (int x = 0; x < contents.length; ++x) {
            if (x == (contents.length - 1) && !archive.getName().endsWith("/")) { // If on last item and item is a file 
                path += contents[x]; // Update final path ArchiveFile 
                file = new ArchiveFile(path, contents[x], archive.getUncompressedSize(), archive.getCompressedSize(), archive.getModifiedTime(), archive.getModifiedDate(), archive.getCRC());
    
                current.addFile(file); // Create and add the file to the current ArchiveFolder
            }
            else if (x == (contents.length - 1)) { // Else if we are on last item and it is a folder
                path += contents[x] + "/"; // Update final path
                ArchiveFolder folder = new ArchiveFolder(path, contents[x], archive.getModifiedTime(), archive.getModifiedDate());
    
                current.addFolder(folder); // Create and add this folder to the current ArchiveFile
            }
            else { // Else if we are still traversing through the path
                path += contents[x] + "/"; // Update path
                ArchiveFolder folder = new ArchiveFolder(path, contents[x]);
    
                current.addFolder(folder); // Create and add folder (remember we do not know the modified date/time as all we know is the path, so we can deduce the name only)
                current = folder; // Update current ArchiveFolder to the newly created one for the next iteration of the for loop
            }
        }
    
        archive.getNext();
    }
    

    假设 是根ArchiveFolder(最初为空)。

    拜托 通读以上代码中的注释以熟悉它。还假设ArchiveFile中的addFolder方法仅在文件夹不存在时添加该文件夹(因此没有多个文件夹),并且如果现有文件夹为空(即它是一个我们只知道名称的中间文件夹,但现在我们知道其详细信息)也会更新其时间和日期。addFolder的代码是(非常自我解释):

    public void addFolder(ArchiveFolder folder) {
        int loc = folders.indexOf(folder); // folders is the ArrayList containing ArchiveFolder's
    
        if (loc == -1) {
            folders.add(folder);
        }
        else {
            ArchiveFolder real = folders.get(loc);
    
            if (real.getTime() == null) {
                real.setTime(folder.getTime());
                real.setDate(folder.getDate());
            }
        }
    }
    

    所以我看不出代码有什么问题,它可以工作,完成后,根ArchiveFolder包含ZIP根目录中的所有文件,它包含根文件夹中的所有direcories。因此,您可能认为它按预期工作,但根文件夹中的ArchiveFolder不包含那些“子”文件夹中的数据,它只是一个空白文件夹,没有其他文件和文件夹(当在WinZip中查看时,它确实包含更多的文件/文件夹)。

    在使用Eclipse进行调试之后,for循环确实会遍历所有文件(甚至是上面没有包含的文件),因此我认为这行代码有问题:

                current = folder;
    

    它所做的是,将当前文件夹(用作循环的中间文件夹)更新为新添加的文件夹。

    提前谢谢。

    3 回复  |  直到 14 年前
        1
  •  1
  •   josefx    14 年前

    因为您使用eclipse,所以设置一个断点并逐步遍历该方法,这可能需要时间,但它有助于查找bug(例如,检查对象ID以查看引用是否已更改)。

        2
  •  1
  •   Esko    14 年前

    指针 在调用者或被调用者一侧不会更改另一侧的指针。

    希望我清楚?

        3
  •  0
  •   Sbodd    14 年前

    我怀疑您没有在ArchiveFolder类上正确重载equals()和hashCode(),因此

    folders.indexOf(folder)
    

    in addFolder()总是返回-1。