在 java 中实现 2 个没有指针的链表

Implementing 2 linked lists in java without pointers

我在 Java 代码中尝试实现 2 个链表时遇到问题。

如果我在 main 中声明我的 "head" 变量并将它们传递给任何函数,它们不能被该函数修改,因为引用变量对于该函数来说是局部的。

但是如果我将它们设为 class 变量,被调用函数将无法知道我指的是 2 个 "head" 变量中的哪一个。

这个问题在C语言中很容易解决,通过在函数中传递双指针,使局部变量可以修改传递的变量,但是在Java中如何解决这个问题呢?谢谢

指针未在 Java

中使用

在 class 级别定义属性,不要在方法的签名中使用它,或者 return 将函数的值设为原始变量。

 public class MyClass{

      private string value;

      public myFunction(){
        value = toto; // this will change the value of the attribute directly
      }

     public static void main(String[] args) {
           MyClass myClass = new MyClass();
           myClass.myFunction(); // The new value is saved at the class level
      }
 }

public class 我的班级{

       public String myFunction(String value){
          value = "toto";
          return value; // The value modified is return back to the caller
       }

       public static void main(String[] args) {
           MyClass myClass = new MyClass();
           String myValue = myClass.myFunction(myvalue); // The new value is saved
       }
 }