【发布时间】:2021-10-24 08:05:26
【问题描述】:
我有一个任务要求我打印出给定的字符串列表,每隔一个字符串就跳过一次。然后,以相反的顺序打印字符串列表,每隔一个字符串跳过一次。所有输出应打印在同一行。
例如,如果字符串列表是 ["a", "b", "c", "d"],则输出应该是 "acdb"。如果字符串列表是 ["a", "b", "c"],则输出应该是 "acca"。
import java.util.List;
import java.util.ListIterator;
public class ListPrintStrings {
public static void printStrings(List<String> strings) {
// write your code here
ListIterator<String> stringWithIterator = strings.listIterator(strings.size());
while(stringWithIterator.nextIndex() == 1){
stringWithIterator.next();
stringWithIterator.remove();
}
for(String s: strings){
System.out.print(s);
}
}
}
我不知道如何使用 ListIterator 反转列表以及如何将字符串一起返回
Failures (3):
=> org.junit.ComparisonFailure: The ArrayList had an odd number of elements. Check that your solution can handles an odd number of elements. expected:<a[ceeca]> but was:<a[bcde]>
=> org.junit.ComparisonFailure: expected:<a[cdb]> but was:<a[bcd]>
=> org.junit.ComparisonFailure: expected:<hello[learningisfunjavaworld]> but was:<hello[worldlearningjavaisfun]>
这些是我的错误。感谢任何帮助/提示。
【问题讨论】:
标签: java list collections iterator