使用ListIterator,在ArrayList中的两个偶数之间添加”-1″。

huangapple go评论89阅读模式
英文:

Using a ListIterator, add a "-1" in between 2 even integers in an arraylist

问题

这是我的第一篇帖子,如果不完美,我道歉。我正在进行一个项目,需要使用ListIterator遍历一个整数的ArrayList。在遍历过程中,我需要找到所有的偶数对,并在它们之间添加一个“-1”来分隔这些偶数。以下是我目前的代码:

  1. /*
  2. 没有两个偶数。打印原始列表。如果你找到两个偶数,就在它们之间添加一个-1。打印新列表。
  3. */
  4. ListIterator<Integer> lt5 = x.listIterator();
  5. System.out.println();
  6. System.out.println("N O E V E N S ");
  7. printArrays(x);
  8. while(lt5.hasNext()) {
  9. if(lt5.next() % 2 == 0 && lt5.next() % 2 == 0) {
  10. lt5.previous();
  11. lt5.add(-1);
  12. }
  13. }
  14. System.out.println();
  15. ListIterator<Integer> lt6 = x.listIterator();
  16. while(lt6.hasNext()) {
  17. System.out.print(lt6.next() + " ");
  18. }

我确定问题很简单,但我无法弄清楚。对此有什么想法吗?

我必须使用迭代器。

英文:

So this is my first post so I apologize if it is not perfect. I am working on a project and I need to traverse an arraylist of integers with a ListIterator. Within this traversal, I will need to find all pairs of even numbers and add a "-1" in between to separate the evens. This is my code as of now:

  1. No two evens. Print the original list. If you find two even numbers then add a -1 between them. Print the new list.
  2. */
  3. ListIterator&lt;Integer&gt; lt5 = x.listIterator();
  4. System.out.println();
  5. System.out.println(&quot;N O E V E N S &quot;);
  6. printArrays(x);
  7. while(lt5.hasNext()) {
  8. if(lt5.next() %2 ==0 &amp;&amp; lt5.next()%2==0) {
  9. lt5.previous();
  10. lt5.add(-1);
  11. }
  12. }
  13. System.out.println();
  14. ListIterator&lt;Integer&gt; lt6 = x.listIterator();
  15. while(lt6.hasNext()) {
  16. System.out.print(lt6.next()+&quot; &quot;);
  17. }

I am sure it is something simple but I can't figure it out. Any ideas on this?

I am required to use an iterator

答案1

得分: 0

以下是你想要的翻译内容:

如果你想在两个连续的偶数之后插入 -1,可以使用以下代码:

  1. public void modifyList(List<Integer> list){
  2. System.out.println(list);
  3. ListIterator<Integer> it = list.listIterator();
  4. while(it.hasNext()){
  5. if(it.next() % 2 == 0 && it.hasNext() && it.next() % 2 == 0){
  6. it.add(-1);
  7. }
  8. }
  9. System.out.println(list);
  10. }

输入:[1,2,3,4]
输出:[1,2,3,4]

输入:[1,2,4,5,6,8]
输出:[1,2,4,-1,5,6,8,-1]

如果你想在两个连续的偶数之间插入 -1,可以使用以下代码:

  1. public void modifyList(List<Integer> list){
  2. System.out.println(list);
  3. ListIterator<Integer> it = list.listIterator();
  4. while(it.hasNext()){
  5. if(it.next() % 2 == 0 && it.hasNext() && it.next() % 2 == 0){
  6. it.previous();
  7. it.add(-1);
  8. }
  9. }
  10. System.out.println(list);
  11. }

输入:[1,2,3,4]
输出:[1,2,3,4]

输入:[1,2,4,5,6,8]
输出:[1,2,-1,4,5,6,-1,8]

英文:

You can use below code if you want -1 after two consecutive evens:

  1. public void modifyList(List&lt;Integer&gt; list){
  2. System.out.println(list);
  3. ListIterator&lt;Integer&gt; it = list.listIterator();
  4. while(it.hasNext()){
  5. if(it.next()%2==0 &amp;&amp; it.hasNext() &amp;&amp; it.next()%2==0){
  6. it.add(-1);
  7. }
  8. }
  9. System.out.println(list);
  10. }
  11. //Input: [1,2,3,4]
  12. //Output:[1,2,3,4]
  13. //Input: [1,2,4,5,6,8]
  14. //Output:[1,2,4,-1,5,6,8,-1]

You can use below code if you want -1 between two consecutive evens:

  1. public void modifyList(List&lt;Integer&gt; list){
  2. System.out.println(list);
  3. ListIterator&lt;Integer&gt; it = list.listIterator();
  4. while(it.hasNext()){
  5. if(it.next()%2==0 &amp;&amp; it.hasNext() &amp;&amp; it.next()%2==0){
  6. it.previous();
  7. it.add(-1);
  8. }
  9. }
  10. System.out.println(list);
  11. }
  12. //Input: [1,2,3,4]
  13. //Output:[1,2,3,4]
  14. //Input: [1,2,4,5,6,8]
  15. //Output:[1,2,-1,4,5,6,-1,8]

huangapple
  • 本文由 发表于 2020年9月22日 10:04:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64002103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定