如何将此代码转换为循环?我已将top定义为NodeString类型的实例变量
NodeString temp1 = top.getNext().getNext().getNext().getNext().getNext();
NodeString temp2 = top.getNext().getNext().getNext().getNext();
NodeString temp3 = top.getNext().getNext().getNext();
NodeString temp4 = top.getNext().getNext();
NodeString temp5 = top.getNext();
NodeString temp6 = top;
result.add(temp1.getData());
result.add(temp2.getData());
result.add(temp3.getData());
result.add(temp4.getData());
result.add(temp5.getData());
result.add(temp6.getData());
最佳答案
您可以构建一个数组,然后向后迭代。就像是,
NodeString[] arr = { top, arr[0].getNext(), arr[1].getNext(),
arr[3].getNext(), arr[4].getNext(), arr[5].getNext() };
for (int i = arr.length - 1; i >= 0; i--) {
result.add(arr[i].getData());
}