使用 ListIterator 反转列表并跳过字符的特定位置 (Java)

Reverse a list using ListIterator and skip certain position of character (Java)

我有一个任务要求我打印出给定的字符串列表,每隔一个字符串就跳过一次。然后,以相反的顺序打印字符串列表,每隔一个字符串跳过一次。所有输出都应打印在同一行上。

例如,如果字符串列表是 ["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 反转列表以及如何return 将字符串放在一起[=13​​=]

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]>

这些是我的错误。感谢您的帮助/提示。

试试这个。

public static void printStrings(List<String> strings) {
    ListIterator<String> i = strings.listIterator();
    while (i.hasNext()) {
        System.out.print(i.next());
        if (i.hasNext())
            i.next();
    }
    while (i.hasPrevious()) {
        System.out.print(i.previous());
        if (i.hasPrevious())
            i.previous();
    }
    System.out.println();
}

public static void main(String[] args) {
    printStrings(List.of("a", "b", "c", "d"));
    printStrings(List.of("a", "b", "c"));
}

输出:

acdb
acca