【问题标题】:using instanceof to count in java在java中使用instanceof进行计数
【发布时间】:2018-04-24 15:38:12
【问题描述】:
import java.util.ArrayList;

public class Folder extends AbstractFile
{
//Replace previous ArrayLists with a single
//ArrayList of AbstractFile references
private ArrayList<AbstractFile> files = new ArrayList();
       AbstractFile abstractfile;
/**
 * Constructor for objects of class Folder
 */
public Folder(String name)
{
    super();
    this.name = name;
}
// replace previous add methods
// with a single add(AbstractFile fileObject) method
public boolean add(AbstractFile fileObject)
{
    return files.add(fileObject);
}
@Override
public int size()
{
    int size =0; // size holds the running total
    for (AbstractFile file : files){ // for each AbsFile ref
       size+=file.size(); //call size() and update the running total
    }
    return size; // return the final value
}


@Override
public int getNumFiles(){
    int numFiles = 0;
    for(AbstractFile file: files)
    {
        numFiles += file.getNumFiles();


    }

    return numFiles;// default value
}
@Override
public int getNumFolders(){
    int numFolders = 0;

    // for(AbstractFile file: files)
    // {
        // if(file instanceof Folder)
        // {
        // numFolders += file.getNumFolders();
    // }

    // }
   // return numFolders;// default value
    for (Object e : files)
    {
        if (e instanceof Folder)
        {
             numFolders += e.getNumFolders();
        }
    }
      return numFolders;// default value
}
@Override
public AbstractFile find(String name){
    //TODO Later - not in mini assignment
    return null;
}
}

AbstractFile 类

public abstract class AbstractFile
{
// instance variables -
String name;

public abstract int size();
public abstract int getNumFiles();
public abstract int getNumFolders();
public abstract AbstractFile find(String name);

public String getName(){
    return name;
}

}

文件系统类-用于测试

public class FileSystem
{
    public static void main(String[] args)
    {
        FileSystem fileSystem = new FileSystem();
        fileSystem.fileTest1();

    }

    public void fileTest1(){

        Folder documents = new Folder("Documents");
        Folder music = new Folder("Music");
        Folder photos = new Folder("Photos");
        documents.add(music);
        documents.add(photos);
        File assign1  = new File("assign1.doc");
        documents.add(assign1);
        Folder dylan = new Folder("Dylan");
        music.add(dylan);
        Folder band = new Folder("The Band");
        music.add(band);
        File family = new File("family.jpg");
        photos.add(family);
        File tambourine = new File("tambourine.mp3");
        dylan.add(tambourine);
        File dixie = new File("dixie.mp3");
        band.add(dixie);
        File weight = new File("weight.mp3");
        band.add(weight);

        String contents1 = ("Hey, mister, can you tell me "); 
        String contents2 = ("Hey Mr Tambourine Man");
        String contents3 = ("The night they drove old dixie down");
        String contents4 = ("fee fi fo fum");

        weight.setContents(contents1); // add contents to each File
        tambourine.setContents(contents2);
        dixie.setContents(contents3);
        assign1.setContents(contents4);

        //********test for size()****************
        int expected  = contents1.length() + contents2.length() + contents3.length() + contents4.length();
        int result = documents.size();

        if(result==expected){ // test fro equality
            System.out.println("size() works");

        }else{
            System.out.println("size() doesn't work");
        }

        //*****************************************

        //*****************test for getNumFiles()******************

        expected =5;// what value should expected be set to?
        result = documents.getNumFiles();

        if(result==expected){ // test fro equality
            System.out.println("NumFiles() works");

        }else{
            System.out.println("NumFiles() doesn't work");

        }

        //output the results of the test for equality

        // **************************************

        //*****************test for getNumFiles()******************

        expected = 5; // what value should expected be set to?
        result = documents.getNumFolders();  



        if(result==expected){ // test fro equality
            System.out.println("NumFolder() works");

        }else{
            System.out.println("NumFolder() doesn't work");
            System.out.printf("%d",result);
        }

        // **************************************


     }
  }

文件类

  public class File extends AbstractFile
   {
    private String contents;
/**
 * Constructor for objects of class File
 */
public File(String name)
{
    super();
    this.name = name;
}

public String getContents(){
    return contents;
}

public void setContents(String contents){
   this.contents = contents;
}

@Override
public int size()
{
    if(contents==null){ //handle situation where contents may not have been set
       return 0; 
    }
    return contents.length();
}
@Override
public int getNumFiles(){
    return 1; // a File object just returns 1 (it contains one File - itself)
}
@Override
public int getNumFolders(){
    //TODO
    return 0;
}
@Override
public AbstractFile find(String name){
    //TODO Later - not in mini assignment
    return null;
}

}

嗨,所以基本上我在使用 getNumFolders(method) 时遇到了问题,我创建了一个包含 5 个文件夹的测试类,这些文件夹存储在 ArrayList 类型的 AbstractFile 中,称为文件。我试图循环通过这个ArrayList 来计算文件夹,请记住这个ArrayList 也包含文件,所以它们不能被计算在内。 我将不胜感激任何帮助。 非常感谢!

【问题讨论】:

  • 请将其缩减为 minimal reproducible example,确保包含问题 - 我猜这是 Object 不包含 getNumFiles() 的编译时错误。仅仅因为您使用了 instanceof 并不会更改 e 的编译时类型 - 您需要强制转换。
  • 我添加了剩余的代码,谢谢!
  • 但是您没有将其简化为最小示例或显示错误:(
  • 错误在于 e.getNumFolders() ,无法识别 getNumFolders() 方法。
  • 那么这应该是问题所在 - 我已经在第一条评论中解释了原因......

标签: java loops arraylist instanceof


【解决方案1】:

如果您想计算文件夹实例的数量,那么下面应该可以完成工作。

public int getNumFolders() {

int numFolders = 0;

for(AbstractFile file: files) {
    if(file instanceof Folder) {
        numFolders++;
   }
}
 return numFolders;
}

根据您的测试用例,这里的 expectedValue 应该是 2。 在测试用例的 fileTest1() 方法中,

expected = 2; // this should be two because there are two folders called music and photos in documents folder
result = documents.getNumFolders();

【讨论】:

    猜你喜欢
    • 2016-03-18
    • 2011-11-23
    • 2011-09-03
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多