如何更改链表中的元素?

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

How to change an element in a linked list?

问题

public static void main(String[] args) {
    // 创建空链表
    SimpleLinkedList list = new SimpleLinkedList(null);

    // 将一些元素按照逆序添加到链表中,使链表有序
    list.addElement("W");
    list.addElement("T");
    list.addElement("P");
    list.addElement("L");
    list.addElement("K");
    list.addElement("B");
    list.printList();

    System.out.println("将 W 改为 X");
    list.changeElem("W", "X");
}

我应该在另一个类中创建一个名为 "changeElem" 的新方法,其形式为:void changeElem(String oldStr, String newStr),但是我不确定如何做到这一点。

英文:

What is the code that goes into: void changeElem(String oldStr, String newStr)?

I have a driver's class with:

public static void main(String[] args) {
    // create empty list
    SimpleLinkedList list = new SimpleLinkedList(null);

    // add some elements in the list in
    // reverse order so that list is sorted
    list.addElement("W");
    list.addElement("T");
    list.addElement("P");
    list.addElement("L");
    list.addElement("K");
    list.addElement("B");
    list.printList();

    System.out.println("change W to X");
    list.changeElem("W", "X");
}

I am supposed to make a new method called “changeElem” in another class where
void changeElem(String oldStr, String newStr) but I am not sure how.

答案1

得分: 1

The answer by manikanta is right; however, changeElement(List list, String string, String replacement) requires you to pass the List as a parameter, which means you can't use it in the context list.changeElement("W", "X").

I'm assuming SimpleLinkedList is a Java class that you have implemented.

To use the method as list.changeElement("W", "X") instead of changeElement(list, "W", "X"), you need to add the method changeElement to the SimpleLinkedList class (which I think is actually your question).

Since the actual implementation of the class isn't known, something like this:

public class SimpleLinkedList {
    public void add(String str) {
        //This should be an existing method, for example
    }

    //Assuming this is the implementation
    private class Node {
        String data;
        Node nextNode;
    }

    private Node firstNode; //Should be set/unset in your add/remove method

    //New method. To keep the order of your list, iterate and replace individually
    public void changeElement(String str, String replacement) {
        for (Node n = firstNode; n != null; n = n.nextNode) {
            if (str.equals(n.data)) {
                n.data = replacement;
                break; //If you want to replace only one instance
            }
        }
    }
}

Also, when in doubt: Look at Java's source code. What you basically want to do is find the place where str is and then replace it.

That's almost the Java LinkedList#indexOf method, except instead of returning an index when you find str, you're replacing it. Java's LinkedList#indexOf() implementation.

英文:

The answer by manikanta is right; however, changeElement(List list, String string, String replacement) requires you to pass the List as a parameter, which means you can't use it in the context list.changeElement("W", "X").

I'm assuming SimpleLinkedList is a Java class that you have implemented.

To use the method as list.changeElement("W", "X") instead of changeElement(list, "W", "X"), you need to add the method changeElement to the SimpleLinkedList class (which I think is actually your question).

Since actual implementation of the class isn't known, something like this:

public class SimpleLinkedList {
    public void add(String str) {
        //This should be an existing method, for example
    }

    //Assuming this is the implementation
    private class Node {
        String data;
        Node nextNode;
    }

    private Node firstNode; //Should be set/unset in your add/remove method

    //New method. To keep the order of your list, iterate and replace individually
    public void changeElement(String str, String replacement) {
        for (Node n = firstNode; n != null; n = n.nextNode) {
            if (str.equals(n.data)) {
                n.data = replacement;
                break; //If you want to replace only one instance
            }
        }
    }
}

Also, when in doubt: Look at Java's source code. What you basically want to do is find the place where str is and then replace it.

That's almost the Java LinkedList#indexOf method, except instead of returning index when you find str, you're replacing it. Java's LinkedList#indexOf() implementation.

答案2

得分: 0

我们只需要将替换字符串添加到您预期的位置,然后我们应该删除不需要的字符串。

public static void main(String[] args) {
    /* // 创建空列表
     * LinkedList list = new LinkedList(null);
     */
    LinkedList<String> listLinked = new LinkedList<String>();

    // 将一些元素按照逆序添加到列表中,
    // 以使列表排序
    listLinked.add("W");
    listLinked.add("T");
    listLinked.add("P");
    listLinked.add("L");
    listLinked.add("K");
    listLinked.add("B");

    changeElement(listLinked, "W", "X");
    System.out.println(listLinked);
}
private static void changeElement(
        List list, String string, String replacement) {
    if (list.contains(string)) {
        // 添加替换字符串
        list.add(list.indexOf(string), replacement);
        // 删除不需要的字符串
        list.remove(string);
    } else {
        System.out.println("未找到要替换的元素");
    }
}
英文:

We just have to add the replacement string at your intended location and then we should remove the unwanted string.

public static void main(String[] args) {
    /* // create empty list
     * LinkedList list = new LinkedList(null);
     */
    LinkedList listLinked = new LinkedList&lt;String&gt;();

    // add some elements in the list in
    // reverse order so that list is sorted
    listLinked.add(&quot;W&quot;);
    listLinked.add(&quot;T&quot;);
    listLinked.add(&quot;P&quot;);
    listLinked.add(&quot;L&quot;);
    listLinked.add(&quot;K&quot;);
    listLinked.add(&quot;B&quot;);

    changeElement(listLinked, &quot;W&quot;, &quot;X&quot;);
    System.out.println(listLinked);
}
private static void changeElement(
        List list, String string, String replacement) {
    if (list.contains(string)) {
        // adding the replacement
        list.add(list.indexOf(string), replacement);
        // removing the unwanted
        list.remove(string);
    } else {
        System.out.println(&quot;Element not found to replace&quot;);
    }
}

答案3

得分: 0

假设你的 SimpleLinkedList 实现了 List 接口,并且至少是 Java 8:

private static void changeElement(
    List<String> list, String string, String replacement) {
    list.replaceAll(s -> s.equals(string) ? replacement : s);
}

当然,你可以从你的 SimpleLinkedList 中调用这个 'Util' 方法,并将自身作为第一个参数传递进去。

英文:

Assuming your SimpleLinkedList implements the List interface and is at least Java 8:

private static void changeElement(
    List&lt;String&gt; list, String string, String replacement) {
    list.replaceAll(s -&gt; s.equals(string) ? replacement : s);
}

Of course you could call this 'Util' method from your SimpleLinkedList passing itself as the first argument.

huangapple
  • 本文由 发表于 2020年4月6日 04:26:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/61049043.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定