我有一个具有 90 多个属性的域对象。在所有这些属性中都有 .
,我想将其替换为空字符串。
我现在可以做的:
for (int i = 0; i < list.size(); i++) {
list.get(i).getProp1().replace(".", "");
list.get(i).getProp2().replace(".", "");
//yadadada ...
list.get(i).getProp90().replace(".", "");
}
但是,这非常无聊并且需要编写大量代码。有没有办法更优雅、更快速地做到这一点?
我很欣赏你的想法
最佳答案
在你的域类中创建一个方法 updateProp(因为这是你需要的)怎么样:
public void updateProp(int index) {
prop[index] = prop[index].replace(".", "");
}
然后循环调用
for (int i = 0; i < list.size(); i++) {
for(int j=0; j < prop.length; j++) { /*use a getter, instance or class
reference for obtaining prop.length*/
list.get(i).updateProp(j);
}
}
将 Prop 存储在它自己的对象中也可以解决这个问题,但我猜它会破坏比修复更多的代码。
关于java - 批量替换char,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25505536/