Java ListIterator交换当前和下一个

Java ListIterator交换当前和下一个,java,swap,listiterator,Java,Swap,Listiterator,我需要交换ListIterator“nodeListIterator”中的node和node.getNext(),我已经试了几个小时了 if (node instanceof LdcInsnNode && (node.getNext() instanceof FieldInsnNode && node.getNext().getOpcode() == Opcodes.GETSTATIC) && node.getNext().get

我需要交换ListIterator“nodeListIterator”中的node和node.getNext(),我已经试了几个小时了

 if (node instanceof LdcInsnNode && (node.getNext() instanceof FieldInsnNode && node.getNext().getOpcode() == Opcodes.GETSTATIC)
        && node.getNext().getNext().getOpcode() == Opcodes.IMUL) {
        // code
 }

谢谢你。

我想出来了,它很粗糙,但很管用

final ListIterator<AbstractInsnNode> nodeListIterator = methodNode.instructions.iterator();
while (nodeListIterator.hasNext()) {
    AbstractInsnNode node = nodeListIterator.next();
    if (node instanceof LdcInsnNode && (node.getNext() instanceof FieldInsnNode && node.getNext().getOpcode() == Opcodes.GETSTATIC)
            && node.getNext().getNext().getOpcode() == Opcodes.IMUL) {
        AbstractInsnNode temp1 = node;
        nodeListIterator.remove();
        node = nodeListIterator.next();
        AbstractInsnNode temp2 = node;
        nodeListIterator.remove();
        nodeListIterator.next();
        nodeListIterator.previous();
        nodeListIterator.add(temp1);
        nodeListIterator.previous();
        nodeListIterator.add(temp2);
        nodeListIterator.previous();
    }
}
final list迭代器nodeListIterator=methodNode.instructions.iterator();
while(nodeListIterator.hasNext()){
AbstractInsnNode节点=节点编辑器.next();
if(节点实例of LdcInsnNode&&(node.getNext()实例of FieldInsnNode&&node.getNext().getOpcode()==操作码.GETSTATIC)
&&node.getNext().getNext().getOpcode()==Opcodes.IMUL){
AbstractInSNode temp1=节点;
nodeListIterator.remove();
node=nodeListIterator.next();
AbstractInSNode temp2=节点;
nodeListIterator.remove();
nodeListIterator.next();
nodeListIterator.previous();
nodeListIterator.add(temp1);
nodeListIterator.previous();
nodeListIterator.add(temp2);
nodeListIterator.previous();
}
}

我想出来了,虽然很粗糙,但很有效

final ListIterator<AbstractInsnNode> nodeListIterator = methodNode.instructions.iterator();
while (nodeListIterator.hasNext()) {
    AbstractInsnNode node = nodeListIterator.next();
    if (node instanceof LdcInsnNode && (node.getNext() instanceof FieldInsnNode && node.getNext().getOpcode() == Opcodes.GETSTATIC)
            && node.getNext().getNext().getOpcode() == Opcodes.IMUL) {
        AbstractInsnNode temp1 = node;
        nodeListIterator.remove();
        node = nodeListIterator.next();
        AbstractInsnNode temp2 = node;
        nodeListIterator.remove();
        nodeListIterator.next();
        nodeListIterator.previous();
        nodeListIterator.add(temp1);
        nodeListIterator.previous();
        nodeListIterator.add(temp2);
        nodeListIterator.previous();
    }
}
final list迭代器nodeListIterator=methodNode.instructions.iterator();
while(nodeListIterator.hasNext()){
AbstractInsnNode节点=节点编辑器.next();
if(节点实例of LdcInsnNode&&(node.getNext()实例of FieldInsnNode&&node.getNext().getOpcode()==操作码.GETSTATIC)
&&node.getNext().getNext().getOpcode()==Opcodes.IMUL){
AbstractInSNode temp1=节点;
nodeListIterator.remove();
node=nodeListIterator.next();
AbstractInSNode temp2=节点;
nodeListIterator.remove();
nodeListIterator.next();
nodeListIterator.previous();
nodeListIterator.add(temp1);
nodeListIterator.previous();
nodeListIterator.add(temp2);
nodeListIterator.previous();
}
}

你真的能包含代码并描述你看到的具体问题吗?@hooda代码完全不相关,这是一个算法问题。它到达给定的if语句,if语句正在验证,我只需要对ListIterator进行重新排序………@JordFlo请解释您的代码试图在算法方面实现什么,而不是在编码方面。例如,您说要交换
节点
节点.getNext()
,但我不确定您的意思是要交换第一个和第二个节点,还是要交换每一个其他节点,等等。这有点让人困惑。@Mathew Cliatt我希望节点在列表迭代器中有node.getNext()的索引,我想要node.getNext()在列表迭代器中有节点的索引。你真的能包含代码并描述你看到的具体问题吗?@hooda代码完全无关,这是一个算法问题。它到达给定的if语句,if语句正在验证,我只需要对ListIterator进行重新排序………@JordFlo请解释您的代码试图在算法方面实现什么,而不是在编码方面。例如,您说要交换
节点
节点.getNext()
,但我不确定您的意思是要交换第一个和第二个节点,还是要交换每一个其他节点,等等。这有点让人困惑。@Mathew Cliatt我希望节点在列表迭代器中有node.getNext()的索引,我想要node.getNext()在列表迭代器中具有节点的索引。