克隆一个Properties对象

时间:2014-07-07 08:40:12

标签: java properties hashtable clone

我是一名Java初学者,所以请尽可能地提出愚蠢的问题。

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

我写了以下代码:

public class 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()”方法应用到我的克隆中时出错...为什么?

2 个答案:

答案 0 :(得分:3)

你的问题就在这一行:

Object garList_clone = garList.clone();

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

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

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

之后,您可以调用为类Properties定义的方法。编译错误的本质是copmiler只知道变量的声明类型(这里:Object)。而且你只能在这种类型中声明的变量上调用方法。

答案 1 :(得分:1)

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

从中更改

 Object garList_clone = garList.clone();

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