简体   繁体   English

如何从 void 方法(Java)更改 LinkedList

[英]How can I change a LinkedList from a void method (Java)

This seems very simple but I can't quite figure out why this isn't working.这看起来很简单,但我不太明白为什么这不起作用。

I want to reverse the elements in my LinkedList which I have a working method for, but I can't return the value as my prof wants it to be a void method.我想反转我的 LinkedList 中的元素,我有一个工作方法,但我不能返回值,因为我的教授希望它是一个 void 方法。 How would I go about this?我将如何 go 关于这个?

import java.util.LinkedList;
public class ListUtil {
    public static void reverse(LinkedList<String> strings) {
        LinkedList<String> reverseLinkedList = new LinkedList<>();
        for(int i = strings.size() - 1; i >= 0; i--) {
            reverseLinkedList.add(strings.get(i));
        }
        strings = reverseLinkedList;
        System.out.println(strings);
    }
}
import java.util.LinkedList;

public class ReverseTester {
    public static void main(String[] args) {
        LinkedList<String> employeeNames = new LinkedList<>();
        employeeNames.addLast("Dick");
        employeeNames.addLast("Harry");
        employeeNames.addLast("Romeo");
        employeeNames.addLast("Tom");
        
        ListUtil.reverse(employeeNames);
        System.out.println(employeeNames);
        System.out.println("Expected: [Tom, Romeo, Harry, Dick]");
    }
}

In my ListUtil class, it does reverse the list, but doesnt return a value (as it is void) but I don't know how to go about setting employeeName in the ReverseTester class.在我的 ListUtil class 中,它确实反转了列表,但不返回值(因为它是无效的)但我不知道如何 go 关于在 ReverseTester ZA2F2ED4F8EBC2CBB4C21A29DC4 中设置employeeName。

I know this is probably super simple but I have not been able to figure this out for the life of me, any help is greatly appreciated.我知道这可能非常简单,但我一生都无法弄清楚这一点,非常感谢任何帮助。

Empty and re-fill the existing list rather than replacing it.清空并重新填充现有列表,而不是替换它。

public static void reverse(LinkedList<String> strings) {
    List<String> temp = new ArrayList<>(strings);   // Copy the contents of the original list. Pass the original list to constructor of our duplicate list.
    strings.clear();                                // Empty the original list.
    for (String e : temp)
        strings.addFirst(e);                        // Refill the original list using elements from our duplicate list.
}

Or simply或者干脆

public static void reverse(LinkedList<String> strings) {
    Collections.reverse(strings);
}

Non-primitive Java object are stored by reference so you don't need to return anything from ListUtil::reverse.非原始 Java object 通过引用存储,因此您不需要从 ListUtil::reverse 返回任何内容。 Any changes made to the object in the function will be reflected in ReverseTester.java.对 function 中的 object 所做的任何更改都将反映在 ReverseTester.java 中。 This happens because, again, non-primitive Java objects are stored by reference.再次发生这种情况是因为非原始 Java 对象是通过引用存储的。 Basically your code does exactly what you want it to do.基本上,您的代码完全符合您的要求。 You make a LinkedList, populate it with items, and then reverse those items.您创建一个 LinkedList,用项目填充它,然后反转这些项目。

You will have a problem with System.out.println(employeeNames); System.out.println(employeeNames);会有问题。 though.尽管。 Because that will just print the object's formal name and not it's contents.因为那只会打印对象的正式名称,而不是它的内容。 If you want to print the contents of a list in Java you can do:如果要打印 Java 中列表的内容,可以执行以下操作:

for (String name : employeeNames) {
    System.out.println(t);
}

This is my first answer so please ask any questions if I wasn't clear enough!这是我的第一个答案,如果我不够清楚,请提出任何问题!

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

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