2017-10-19 43 views
0

如何在每个额外的循环中添加next.在每个额外的for循环方法调用之前添加类名称

第一个循环:

for(int i=0; i<=index; i++){ 
    retValue = getValue(); 
} 

第二环:

for(int i=0; i<=index; i++){ 

    retValue = next.getValue(); 

三环路:

for(int i=0; i<=index; i++){ 

    retValue = next.next.getValue(); 
} 

等。

+1

U se临时变量?这是Java,而不是JavaScript。了解差异并正确标记您的问题。 – Xufox

+1

看起来你不是在问你想要达到什么目的,而是错误地为你的问题假设一个解决方案,如果不可能解决不了你的问题。 此外,您并未在代码示例中添加类名,而是添加了变量名。 –

+1

你可以显示一些周围的代码来给出一些上下文吗? –

回答

1

如何做你想做

您可以使用一个临时变量来保存的值(在我的例子也有T型)

T temp = this; 
for(int i=0; i<=index; i++){ 
    retValue = this.getValue(); 
    temp = temp.next; 
} 

使用迭代器什么

如果您一个迭代器,它会返回如上所示的下一个值,然后您可以迭代这样的项目:

for (T item : getIterator) { 
    retValue = item.getValue(); 
} 
相关问题