可迭代列表是否会因操作而改变?

时间:2017-06-04 08:32:17

标签: java list iterator

我有自己编码的可迭代类,我在演示部分创建了一个可迭代列表。这些方法都包含在我的课程中。

public void add(E e);
public Node<E> next();
public void hasNext();
public Node<E> previous();

这是我的列表上的操作,以便迭代。

list.add("2");
list.add("11");
list.add("8");
list.add("9");
list.add("20");

//list is 2 -- 11 -- 8 -- 9 -- 20

现在,我们有一个可迭代的列表。

OwnIterator ite = list.ownIterator();

然后,我继续在列表上进行操作。 (这里光标指出0.索引)

ite.next(); // by this method cursor points out the element at the index 1
ite.next(); // cursor shifts to 2. index
ite.add("7");

//list is 2 -- 11 -- 7 -- 8 -- 9 -- 20 // when we call next() after add(), it returns 8 not 7;

1-它应该返回7而不是8吗?

2-当光标指向8时,如果我调用previous()怎么办?它会返回7或11吗?我的主要问题是,如果可迭代列表上的操作影响当前的可迭代列表?总是

2 - 11 - 8 - 9 - 20

或新添加的元素发生在可迭代列表中?

1 个答案:

答案 0 :(得分:2)

好吧,既然你正在实现自己的迭代器,你可以做任何对你有用的事情。

如果您希望迭代器符合expected <- structure(list(ID = c(33021L, 33029L, 34001L, 32010L, 33023L, 45012L, 47001L, 48010L, 50001L, 49004L, 9002L, 67008L, 40011L, 42003L, 42011L, 55023L, 40012L, 43007L, 47011L, 52004L, 52005L, 52006L, 52007L, 19001L, 57008L, 57010L, 58006L, 58008L, 59001L, 58008L, 66001L, 68001L, 54057L, 33021L, 33029L, 34001L, 32010L, 33023L, 45012L, 47001L, 48010L, 50001L, 49004L, 9002L, 67008L, 40011L, 42003L, 42011L, 55023L, 40012L, 43007L, 47011L, 52004L, 52005L, 52006L, 52007L, 32010L, 33023L, 45012L, 47001L, 48010L, 50001L, 49004L, 9002L, 67008L, 40011L, 42003L, 42011L, 55023L, 40012L, 43007L, 47011L, 52004L, 52005L, 52006L, 52007L, 19001L, 57008L, 57010L, 58006L, 58008L, 59001L, 65004L, 49004L, 9002L, 67008L, 40011L, 42003L, 42011L, 55023L, 40012L, 43007L, 47011L, 52004L, 52005L, 52006L, 52007L, 19001L, 57008L, 57010L, 58006L, 58008L, 59001L, 65004L, 75003L, 76007L, 77002L, 77003L, 78003L, 48007L, 48011L, 58008L, 66001L, 68001L, 54057L, 33021L, 33029L, 34001L, 32010L, 33023L, 45012L, 47001L, 48010L, 50001L, 49004L, 9002L, 67008L, 40011L, 42003L, 42011L, 55023L, 40012L, 43007L, 47011L, 54057L, 33021L, 33029L, 34001L, 32010L, 33023L, 45012L, 47001L, 48010L, 50001L, 49004L, 9002L, 67008L, 40011L, 42003L, 42011L, 55023L, 40012L, 43007L, 47011L, 52004L, 52005L, 52006L, 52007L, 19001L, 45012L, 47001L, 48010L, 50001L, 49004L, 9002L, 67008L, 40011L, 42003L, 42011L, 55023L, 40012L, 43007L, 47011L, 52004L, 52005L, 52006L, 52007L, 19001L, 57008L, 57010L, 58006L, 58008L, 59001L, 65004L, 75003L, 9002L, 67008L, 40011L, 42003L, 42011L, 55023L, 40012L, 43007L, 47011L, 52004L, 52005L, 52006L, 52007L, 19001L, 57008L, 57010L, 58006L, 58008L, 59001L, 65004L, 75003L, 76007L, 77002L, 77003L, 78003L, 74001L, 39093L, 41006L, 48007L, 48011L, 58008L, 66001L, 68001L, 54057L, 33021L, 33029L, 34001L, 32010L, 33023L, 45012L, 47001L, 48010L, 50001L, 49004L, 9002L, 67008L, 40011L, 42003L, 42011L, 55023L, 40011L, 42003L, 42011L, 55023L, 40012L, 43007L, 47011L, 52004L, 52005L, 52006L, 52007L, 19001L, 57008L, 57010L, 58006L, 58008L, 59001L, 65004L, 75003L, 76007L, 77002L, 77003L, 78003L, 74001L, 42003L, 42011L, 55023L, 40012L, 43007L, 47011L, 52004L, 52005L, 52006L, 52007L, 19001L, 57008L, 57010L, 58006L, 58008L, 59001L, 65004L, 75003L, 76007L, 77002L, 77003L, 78003L, 74001L, 33021L, 55023L, 40012L, 43007L, 47011L, 52004L, 52005L, 52006L, 52007L, 19001L, 57008L, 57010L, 58006L, 58008L, 59001L, 65004L, 75003L, 76007L, 77002L, 77003L, 78003L, 74001L, 33021L, 40012L, 43007L, 47011L, 52004L, 52005L, 52006L, 52007L, 19001L, 57008L, 57010L, 58006L, 58008L, 59001L, 65004L, 75003L, 76007L, 77002L, 77003L, 78003L, 74001L, 33021L, 52004L, 52005L, 52006L, 52007L, 19001L, 57008L, 57010L, 58006L, 58008L, 59001L, 65004L, 75003L, 76007L, 77002L, 77003L, 78003L, 74001L, 33021L, 19001L, 57008L, 57010L, 58006L, 58008L, 59001L, 65004L, 75003L, 76007L, 77002L, 77003L, 78003L, 74001L, 33021L, 57008L, 57010L, 58006L, 58008L, 59001L, 65004L, 75003L, 76007L, 77002L, 77003L, 78003L, 74001L, 33021L, 65004L, 75003L, 76007L, 77002L, 77003L, 78003L, 74001L, 33021L, 75003L, 76007L, 77002L, 77003L, 78003L, 74001L, 33021L, 76007L, 77002L, 77003L, 78003L, 74001L, 33021L, 74001L, 33021L, 33021L), Day = c(9535L, 9535L, 9535L, 9534L, 9534L, 9533L, 9533L, 9533L, 9533L, 9532L, 9531L, 9531L, 9530L, 9529L, 9529L, 9528L, 9527L, 9527L, 9527L, 9526L, 9526L, 9526L, 9526L, 9525L, 9524L, 9524L, 9524L, 9524L, 9524L, 9537L, 9537L, 9537L, 9536L, 9535L, 9535L, 9535L, 9534L, 9534L, 9533L, 9533L, 9533L, 9533L, 9532L, 9531L, 9531L, 9530L, 9529L, 9529L, 9528L, 9527L, 9527L, 9527L, 9526L, 9526L, 9526L, 9526L, 9534L, 9534L, 9533L, 9533L, 9533L, 9533L, 9532L, 9531L, 9531L, 9530L, 9529L, 9529L, 9528L, 9527L, 9527L, 9527L, 9526L, 9526L, 9526L, 9526L, 9525L, 9524L, 9524L, 9524L, 9524L, 9524L, 9523L, 9532L, 9531L, 9531L, 9530L, 9529L, 9529L, 9528L, 9527L, 9527L, 9527L, 9526L, 9526L, 9526L, 9526L, 9525L, 9524L, 9524L, 9524L, 9524L, 9524L, 9523L, 9522L, 9521L, 9521L, 9521L, 9521L, 9538L, 9538L, 9537L, 9537L, 9537L, 9536L, 9535L, 9535L, 9535L, 9534L, 9534L, 9533L, 9533L, 9533L, 9533L, 9532L, 9531L, 9531L, 9530L, 9529L, 9529L, 9528L, 9527L, 9527L, 9527L, 9536L, 9535L, 9535L, 9535L, 9534L, 9534L, 9533L, 9533L, 9533L, 9533L, 9532L, 9531L, 9531L, 9530L, 9529L, 9529L, 9528L, 9527L, 9527L, 9527L, 9526L, 9526L, 9526L, 9526L, 9525L, 9533L, 9533L, 9533L, 9533L, 9532L, 9531L, 9531L, 9530L, 9529L, 9529L, 9528L, 9527L, 9527L, 9527L, 9526L, 9526L, 9526L, 9526L, 9525L, 9524L, 9524L, 9524L, 9524L, 9524L, 9523L, 9522L, 9531L, 9531L, 9530L, 9529L, 9529L, 9528L, 9527L, 9527L, 9527L, 9526L, 9526L, 9526L, 9526L, 9525L, 9524L, 9524L, 9524L, 9524L, 9524L, 9523L, 9522L, 9521L, 9521L, 9521L, 9521L, 9520L, 9539L, 9539L, 9538L, 9538L, 9537L, 9537L, 9537L, 9536L, 9535L, 9535L, 9535L, 9534L, 9534L, 9533L, 9533L, 9533L, 9533L, 9532L, 9531L, 9531L, 9530L, 9529L, 9529L, 9528L, 9530L, 9529L, 9529L, 9528L, 9527L, 9527L, 9527L, 9526L, 9526L, 9526L, 9526L, 9525L, 9524L, 9524L, 9524L, 9524L, 9524L, 9523L, 9522L, 9521L, 9521L, 9521L, 9521L, 9520L, 9529L, 9529L, 9528L, 9527L, 9527L, 9527L, 9526L, 9526L, 9526L, 9526L, 9525L, 9524L, 9524L, 9524L, 9524L, 9524L, 9523L, 9522L, 9521L, 9521L, 9521L, 9521L, 9520L, 9518L, 9528L, 9527L, 9527L, 9527L, 9526L, 9526L, 9526L, 9526L, 9525L, 9524L, 9524L, 9524L, 9524L, 9524L, 9523L, 9522L, 9521L, 9521L, 9521L, 9521L, 9520L, 9518L, 9527L, 9527L, 9527L, 9526L, 9526L, 9526L, 9526L, 9525L, 9524L, 9524L, 9524L, 9524L, 9524L, 9523L, 9522L, 9521L, 9521L, 9521L, 9521L, 9520L, 9518L, 9526L, 9526L, 9526L, 9526L, 9525L, 9524L, 9524L, 9524L, 9524L, 9524L, 9523L, 9522L, 9521L, 9521L, 9521L, 9521L, 9520L, 9518L, 9525L, 9524L, 9524L, 9524L, 9524L, 9524L, 9523L, 9522L, 9521L, 9521L, 9521L, 9521L, 9520L, 9518L, 9524L, 9524L, 9524L, 9524L, 9524L, 9523L, 9522L, 9521L, 9521L, 9521L, 9521L, 9520L, 9518L, 9523L, 9522L, 9521L, 9521L, 9521L, 9521L, 9520L, 9518L, 9522L, 9521L, 9521L, 9521L, 9521L, 9520L, 9518L, 9521L, 9521L, 9521L, 9521L, 9520L, 9518L, 9520L, 9518L, 9518L), Count = c(3L, 3L, 3L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 5L, 5L, 5L, 5L, 5L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 5L, 5L, 5L, 5L, 5L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 5L, 5L, 5L, 5L, 5L, 1L, 1L, 4L, 4L, 4L, 4L, 2L, 2L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 2L, 2L, 1L, 2L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 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 6L, 6L, 6L, 6L, 6L, 6L, 2L, 2L, 1L), Episode_ID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 21L)), .Names = c("ID", "Day", "Count", "Count_sum", "Episode_ID"), row.names = c(NA, -395L), class = "data.frame") 的约定(您必须选择java.util.ListIterator,因为ListIterator在迭代集合时添加元素时有未指定的行为),可以遵循这个逻辑:

  

void java.util.ListIterator.add(E e)

     

将指定的元素插入列表(可选操作)。元素将紧接在next将返回的元素之前插入(如果有),并且在之前返回的元素之后插入(如果有)。 (如果列表中不包含任何元素,则新元素将成为列表中的唯一元素。)新元素在隐式游标之前插入:对next的后续调用不受影响,后续调用previous将返回新元素。 (此调用将调用nextIndex或previousIndex返回的值增加1。)