【问题标题】:Clone a Properties object克隆一个属性对象
【发布时间】:2014-07-07 08:40:12
【问题描述】:

我是 Java 初学者,所以请回答可能很愚蠢的问题。

我想构建一个 Properties 对象,然后克隆它。我还需要将克隆作为 Properties 对象,因为我需要对其应用适用于 Properties 对象的方法。

我写了以下代码:

公共类 TryTask_B2 {

public static void main(String args[]) {

    Properties garList = new Properties();  // create "Properties" obj for 'Gar'
    Set gars;  // def a set for the 'Gar' keys
    String strGar;

    // Fill the "Properties" obj for 'Gar':
    garList.put("Gar_1", "rotura de lunas");
    garList.put("Gar_2", "arbitraje de ley");
    garList.put("Gar_3", "Adaptación del hogar");
    garList.put("Gar_4", "rotura de lunas");

    // Create clone of original "Properties" obj 'Gar':
    Object garList_clone = garList.clone();
    Set gars_clone;  // def a set for the cloned 'Gar' keys
    String strGar_clone;

    gars = garList.keySet();  // get a set-view of the 'Gar' keys
    Iterator itrGar = gars.iterator();
    gars_clone = garList_clone.keySet();  // get a set-view of the cloned 'Gar' keys
    Iterator itr_clone = gars_clone.iterator();

    Iterator itrGar_1 = gars.iterator();
    System.out.println("Original list of Gars: ");
    while(itrGar_1.hasNext()){
        strGar = (String) itrGar_1.next();  
        System.out.println(strGar + " : " + garList.getProperty(strGar) + ".");  
    }
    System.out.println();

    // Compare string-value entries for each and every key in the two lists:
    while(itrGar.hasNext()){
        strGar = (String) itrGar.next();  
        while(itr_clone.hasNext()){
            String str1 = garList.getProperty(strGar);
            strGar_clone = (String) itr_clone.next();  
            String str2 = garList_clone.getProperty(strGar_clone);
            boolean result = str1.equalsIgnoreCase(str2);
            System.out.println(strGar + " : " + str1 + ".");
            System.out.println(strGar_clone + " : " + str2 + ".");
            System.out.println(result);
            System.out.println();
            if(result != true){
            } else {
                Object garList_new = garList.remove(strGar);
                System.out.println("Removed element: " + garList_new);
                System.out.println();
            }
        }
        itr_clone = gars_clone.iterator();
    }

    Iterator itrGar_2 = gars.iterator();
    System.out.println("New list of Gars: ");
    while(itrGar_2.hasNext()){
        strGar = (String) itrGar_2.next();              
        System.out.println(strGar + " : " + garList.getProperty(strGar) + ".");  
    }
    System.out.println();
}

}

但是在我的克隆中应用“keySet()”和“getProperty()”方法时出现错误...为什么?

【问题讨论】:

    标签: java properties hashtable clone


    【解决方案1】:

    你的问题是这一行:

    Object garList_clone = garList.clone();
    

    您可能将克隆分配给Object 类型变量,因为这是clone 方法的返回类型。

    但是您(作为程序员)知道您正在克隆一个Properties 对象。因此将克隆分配给Properties 类型变量是安全的。这也需要演员表:

    Properties garList_clone = (Properties) garList.clone();
    

    之后,您可以调用为类Properties 定义的方法。编译错误的本质是 copmiler 只知道变量的声明类型(此处为:Object)。并且您只能调用以该类型声明的此类变量的方法。

    【讨论】:

    • 谢谢,它有效!但是当我运行它时,我收到以下异常消息:运行:Gars 的原始列表:Gar_4:rotura de lunas。 Gar_3 : Adaptación del hogar。线程“主”java.lang.NullPointerException Gar_2 中的异常:arbitraje de ley。 Gar_1 : rotura de lunas。 Gar_4 : rotura de lunas。 Gar_4 : rotura de lunas。 true 已删除元素:在 trytask_b2.TryTask_B2.main(TryTask_B2.java:56) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)
    • 这个异常信息是什么意思?
    • 这是另一个问题。但是你不应该问这个问题,因为有很多questions dealing with NPEs
    【解决方案2】:

    因为Object garList_clone = garList.clone();Object 而不是Properties

    改变它,

     Object garList_clone = garList.clone();
    

     Properties garList_clone = (Properties) garList.clone();
    

    【讨论】:

      猜你喜欢
      • 2016-08-05
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多