【问题标题】:Use the same method to add elements to ArrayList of String or other object使用相同的方法将元素添加到 String 或其他对象的 ArrayList
【发布时间】:2016-09-06 08:43:12
【问题描述】:

我,我想用同样的方法给StringArrayList或者其他对象的ArrayList添加元素。所以ArrayList 是一种类型,但我可以将不同的arrayList 传递给这个方法 问题是 add 方法,如何使用此方法添加 String 或 FleetInfo(我的对象)? 如果我使用 ArrayList treeFolders 我在此方法中没有错误,但我无法通过 ArrayList

private <T> void addStatisticalFiles (String fleetName, ArrayList<T> treeFolders, boolean isFleetInfo){
        String fleetPath = env.getRequiredProperty(PROPERTY_NAME_FILESYSTEM_BASEPATH) + fleetName + File.separator + "statistics";
        File statisticalFolder = new File(fleetPath);
        if (statisticalFolder != null && statisticalFolder.exists()){
            if (!isFleetInfo){
                treeFolders.add(fleetPath);
                for(String statisticalFile : statisticalFolder.list()){
                    treeFolders.add(fleetPath + File.separator + statisticalFile);
                }
            }else{
                treeFolders.add(new FleetInfo("dir:" + fleetPath, null, null, null));
                for(String statisticalFile : statisticalFolder.list()){
                    treeFolders.add(new FleetInfo("file:" + fleetPath + File.separator + statisticalFile, null, FleetType.file, null));
                }
            }
        }
    }

【问题讨论】:

  • 如果您将任何旧类型的 Object 添加到您的 ArrayList,请不要使用泛型,只需执行 ArrayList treeFolders
  • ArrayList 的一个实例只有一种类型,但我想将不同的 ArrayList 传递给我的 addStatisticalFiles 方法
  • (答案已删除,我以为是C#问题。抱歉,请删除此评论)

标签: java generics arraylist parameters


【解决方案1】:

由于泛型是编译时间决定的,编译器不会知道你要在列表中插入什么样的对象,所以当你添加 T 以外的对象时,它只是通过错误作为类型安全的一部分

解决方案 1:我认为泛型类型 T 在您的情况下没有太多要求,您可以使用非泛型数组列表作为方法参数。

解决方案 2: 您需要将对象转换为 T 如下,但在这种情况下,您必须确保传递与您的布尔参数同步的正确类对象。

private  void addStatisticalFiles (String fleetName, ArrayList<T> treeFolders, boolean isFleetInfo){
        String fleetPath = env.getRequiredProperty(PROPERTY_NAME_FILESYSTEM_BASEPATH) + fleetName + File.separator + "statistics";
        File statisticalFolder = new File(fleetPath);
        if (statisticalFolder != null && statisticalFolder.exists()){
            if (!isFleetInfo){
                T t =(T)fleetPath;
                treeFolders.add(fleetPath);
                for(String statisticalFile : statisticalFolder.list()){
                    treeFolders.add(fleetPath + File.separator + statisticalFile);
                }
            }else{
              FleetInfo fleetInfo=  new FleetInfo("dir:" + fleetPath, null, null, null);
                T tfleetInfo =(T)fleetInfo;
                treeFolders.add(tfleetInfo );
                for(String statisticalFile : statisticalFolder.list()){
                    treeFolders.add(new FleetInfo("file:" + fleetPath + File.separator + statisticalFile, null, FleetType.file, null));
                }
            }
        }
    }

【讨论】:

  • 但 add 不起作用:ArrayList 类型中的方法 add(capture#1-of ?) 不适用于参数 (String)
  • 然后我们可以使用不安全的arraylist查找更新的代码
  • 显然它可以工作,但我想知道是否可以使用类型化数组,因为例如有一次我用 ArrayList 调用这个函数,另一次我可以使用 ArrayList,但是添加方法给了我错误。我不想要一个 ArrayList 里面有任何类型的对象
猜你喜欢
  • 2020-08-27
  • 1970-01-01
  • 2011-12-24
  • 2020-09-29
  • 2011-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多