简体   繁体   English

使用 listIterator 遍历和替换 Char 值的双向链接列表

[英]Traversing and Replacing an Doubly-Linked List of Char values using a listIterator

I need to traverse a Doubly-linked list of characters.我需要遍历一个双向链接的字符列表。 I need to replace every captial 'Y' in my list with a '!'我需要用“!”替换列表中每个大写的“Y” and after, traversing backwards, replace every 'M' with a ' ' in the same list iterator created.之后,向后遍历,在创建的同一列表迭代器中将每个 'M' 替换为 ' '。 If yo need more context to the question I will add more, but for now thats the not-to-long explanation.如果你需要更多的上下文来解决这个问题,我会添加更多,但现在这就是不长的解释。

My code so far:到目前为止我的代码:

public static void main(String[] args)
{
    KWLinkedList<Character> list = new KWLinkedList<Character>() ;
    String linkString = "ZekqmDXJGfaos3MPaSl8p1La.9rXEt43a=Cn#Ds72Y";

    for(int i = 0;  i < linkString.length();i++)
    {
        list.add(linkString.charAt(i));
    }

    ListIterator myIterator = list.listIterator(0) ;

    int i = 0;
    while(!myIterator.hasNext())
    {


        myIterator.next();
        if(myIterator.equals('Y'))
        {
            list.set(list.get(i), 'Y' );
            i = list.get(0);
        }
        if(myIterator.equals('M'))
        {
            list.set(list.get(i - 1), ' ');

        }
        i++;
    }

    System.out.println(list.toString()); // just to see what comes out

Combined your attempt with Andreas solution将您的尝试与 Andreas 解决方案相结合

public static void main(String[] args) {
    LinkedList<Character> list = new LinkedList<>();
    String linkString = "ZekqmDXJGfaos3MPaSl8p1La.9rXEt43a=Cn#Ds72Y";

    for (int i = 0; i < linkString.length(); i++) {
        list.add(linkString.charAt(i));
    }

    ListIterator<Character> myIterator = list.listIterator(0);

    while (myIterator.hasNext()) {
        char c =  myIterator.next();
        if (c == 'Y') {
            myIterator.set('!');
        }
    }
    while (myIterator.hasPrevious()) {
        char c = myIterator.previous();
        if (c == 'M') {
            myIterator.set(' ');
        }
    }

    System.out.println(list.toString()); // just to see what comes out
}

output looks like below输出如下所示
[Z, e, k, q, m, D, X, J, G, f, a, o, s, 3, , P, a, S, l, 8, p, 1, L, a, ., 9, r, X, E, t, 4, 3, a, =, C, n, #, D, s, 7, 2, !] [Z, e, k, q, m, D, X, J, G, f, a, o, s, 3, , P, a, S, l, 8, p, 1, L, a, ., 9, r, X, E, t, 4, 3, a, =, C, n, #, D, s, 7, 2, !]

To use a ListIterator to traverse forward and replace some items, using the set() method:要使用ListIterator向前遍历并替换某些项目,请使用set()方法:

ListIterator<Character> iter = list.listIterator();
while (iter.hasNext()) {
    char c = iter.next(); // gets next value and auto-unboxes the Character to a char
    if (c == 'A')         // primitives are compared using `==`
        iter.set('B');    // auto-boxes the char literal to a Character and replaces the value
}

At this point, the iterator is at the end of the list, so you can use it to traverse backwards:此时,迭代器位于列表的末尾,因此您可以使用它向后遍历:

while (iter.hasPrevious()) {
    char c = iter.previous(); // gets previous value, ie. moves backwards
    if (c == 'C')
        iter.set('D');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM