/**
* CompositeResourceManager
*/
public class CompositeResourceManager implements ResourceManager {
protected ResourceManager[] resourceManagers = new ResourceManager[0];
public void add(ResourceManager resourceManager) {
List<ResourceManager> list = new ArrayList<ResourceManager>();
for (ResourceManager rm : resourceManagers) {
list.add(rm);
}
list.add(resourceManager);
this.resourceManagers = list.toArray(new ResourceManager[list.size()]);
}
}
1.先初始化1个数组
ResourceManger[] resourceManagers = new ResourceManager[0];
2.新建集合
List<ResourceManager> list = new ArrayList<>();
3.遍历数组,添加到集合
for(ResourceManager rm : resourceManagers){
list.add(rm);
}
4.将新增加的值,添加进集合
list.add(resourceManager);
5.根据集合,重新创建数组
this.resourceManagers = list.toArray(new ResourceManager[list.size()]);
6.总结:利用的集合的灵活性;数组的有序,查询性能高;数组初始化耗费最小资源。











网友评论