英文:
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<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()+" ");
}
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<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);
}
//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<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);
}
//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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论