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

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

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

问题

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

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

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

我必须使用迭代器。

英文:

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:

 No two evens. Print the original list. If you find two even numbers then add a -1 between them. Print the new list.      
    	    */   
        	ListIterator&lt;Integer&gt; lt5 = x.listIterator();
        	System.out.println();
        	System.out.println(&quot;N O E V E N S &quot;);
        	printArrays(x); 
        	while(lt5.hasNext()) {
        	if(lt5.next() %2 ==0 &amp;&amp; lt5.next()%2==0) {
        		lt5.previous();
        		lt5.add(-1);
        	}
        	
        	}
        	
        	System.out.println();
        	ListIterator&lt;Integer&gt; lt6 = x.listIterator(); 
            while(lt6.hasNext()) {
            	System.out.print(lt6.next()+&quot; &quot;);
            }

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,可以使用以下代码:

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

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

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

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

public void modifyList(List<Integer> list){
    System.out.println(list);
    ListIterator<Integer> it = list.listIterator();
    while(it.hasNext()){
        if(it.next() % 2 == 0 && it.hasNext() && it.next() % 2 == 0){
            it.previous();
            it.add(-1);
        }
    }
    System.out.println(list);
}

输入:[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:

public void modifyList(List&lt;Integer&gt; list){
    System.out.println(list);
	ListIterator&lt;Integer&gt; it = list.listIterator();
	while(it.hasNext()){
		if(it.next()%2==0 &amp;&amp; it.hasNext() &amp;&amp; it.next()%2==0){
			it.add(-1);
		}
	}
	System.out.println(list);
}

//Input: [1,2,3,4]
//Output:[1,2,3,4]

//Input: [1,2,4,5,6,8]
//Output:[1,2,4,-1,5,6,8,-1]

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

public void modifyList(List&lt;Integer&gt; list){
    System.out.println(list);
	ListIterator&lt;Integer&gt; it = list.listIterator();
	while(it.hasNext()){
		if(it.next()%2==0 &amp;&amp; it.hasNext() &amp;&amp; it.next()%2==0){
			it.previous();
			it.add(-1);
		}
	}
	System.out.println(list);
}

//Input: [1,2,3,4]
//Output:[1,2,3,4]

//Input: [1,2,4,5,6,8]
//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:

确定