use*_*787 5 java clone properties hashtable
我是一名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();
}
Run Code Online (Sandbox Code Playgroud)
}
但它给我一个错误,将方法"keySet()"和"getProperty()"应用到我的克隆...为什么?
因为Object garList_clone = garList.clone();
是 anObject
而不是 a Properties
。
改变它,
Object garList_clone = garList.clone();
Run Code Online (Sandbox Code Playgroud)
到
Properties garList_clone = (Properties) garList.clone();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2489 次 |
最近记录: |