[英]enhancing java code to be more efficient
I am trying to expand a GWT tree using selenium and i did successfully with that code 我正在尝试使用硒扩展GWT树,并且我成功地使用了该代码
for (int i = 0; i < elem.size(); i++) {
if (!elem.get(i).isExpanded() || !elem.get(i).isLeaf()) {
elem.get(i).toggleExpand();
}
for (int j = 0; j < elem.get(i).getChildren().size(); j++) {
if (!elem.get(i).getChildren().get(j).isExpanded() ||
!elem.get(i).getChildren().get(j).isLeaf()) {
elem.get(i).getChildren().get(j).toggleExpand();
}
for (int k = 0; k < elem.get(i).getChildren().get(j).getChildren().size(); k++) {
if (!elem.get(i).getChildren().get(j).getChildren().get(k).isExpanded() ||
!elem.get(i).getChildren().get(j).getChildren().get(k).isLeaf()) {
elem.get(i).getChildren().get(j).getChildren().get(k).toggleExpand();
}.....
}
}
}
My question is how to enhance that code or how to put in recursive method ? 我的问题是如何增强该代码或如何放入递归方法?
To make that code more readable there are a few things you could do (: 为了使代码更具可读性,您可以做一些事情(:
Reuse method results, eg that of elem.get(i)
or use an extended for-loop (if elem
etc. support that): 重用方法结果,例如
elem.get(i)
结果,或使用扩展的for循环(如果elem
等支持):
for (int i = 0; i < elem.size(); i++) {
ElementClass element = elem.get(i);
if (!element.isExpanded() || !element.isLeaf()) {
element.toggleExpand();
}
...
}
Additionally since you seem to do the same for the element and it's children you could provide a method eg: 另外,由于您似乎对元素及其子元素都执行相同的操作,因此可以提供一种方法,例如:
public void expandIfNeeded(ElementClass element) {
if (!element.isExpanded() || !element.isLeaf()) {
element.toggleExpand();
}
}
Then you loop could look like this: 然后,循环看起来像这样:
for (int i = 0; i < elem.size(); i++) {
ElementClass element = elem.get(i);
expandIfNeeded( element );
... //handle children here and call the same method for those (could also be using recursion)
}
Additionally, if you don't need the index i
and elem
is iterable you could use the extended for: 另外,如果您不需要索引
i
并且elem
是可迭代的,则可以将扩展名用于:
for ( ElementClass element : elem ) {
expandIfNeeded( element );
... //handle children here and call the same method for those (could also be using recursion)
}
And of course recursion would make it more readable, especially if you don't know the depth of the tree: 当然,递归将使其更具可读性,尤其是在您不知道树的深度的情况下:
//I'm assuming "elem" is a List<ElementClass> here
public void expandElements( List<ElementClass> elem ) {
for ( ElementClass element : elem ) {
expandIfNeeded( element );
expandElements( element.getChildren() );
}
}
Unreadable code. 无法读取的代码。
Try something like this: 尝试这样的事情:
for (int i = 0; i < elem.size(); i++) {
if (!elem.get(i).isExpanded()) {
elem.get(i).toggleExpand();
}
// I don't know what this method returns; no time to research for you
List<Element> children = elem.get(i).getChildren();
for (int j = 0; j < children.size(); j++) {
// don't know what you call it; must be a void method of some kind
yourMethodName(children.get(i));
}
}
You can recursively toggled the element by using following code. 您可以使用以下代码来递归切换元素。
public static void toggleElement(Element elem){
if(elem.isExpand() || elem.isLeaf()){
return;
}else{
elem.toggledExpand();
}
List<Element> children=elem.getChildren();
for (int i = 0; i < children.size(); i++) {
toggleElement(children.get(i));
}
}
Hopefully it will resolve your problem. 希望它能解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.