Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中使用类似列表的类_Java - Fatal编程技术网

在java中使用类似列表的类

在java中使用类似列表的类,java,Java,所以我试着在Stackoverflow中搜索,但找不到有用的东西。如果有这样的链接,请指给我看 假设我有一个Java类,其变量如下所示: class Example { public type a; public Example next; } 显然,我想使用这个类作为列表,在这里我可以使用object变量next在末尾添加更多元素 我的困惑来自于迭代这些。 假设我已经将类示例的对象称为test,并在下一个变量中添加了一些元素。 在方法中使用此对象时,如果我使用

所以我试着在Stackoverflow中搜索,但找不到有用的东西。如果有这样的链接,请指给我看

假设我有一个Java类,其变量如下所示:

    class Example {
      public type a;
      public Example next;
}
显然,我想使用这个类作为列表,在这里我可以使用object变量next在末尾添加更多元素

我的困惑来自于迭代这些。 假设我已经将
类示例的对象称为test,并在下一个变量中添加了一些元素。
在方法中使用此对象时,如果我使用:

test = test.next;
Example test2 = test;
test2 = test2.next
或者如果我使用:

test = test.next;
Example test2 = test;
test2 = test2.next
我想这两个意思是相同的,都是对同一个对象的引用,所以迭代一个也意味着迭代第二个。当我从方法返回时,该对象上的“指针”返回到我的起始对象。 对吗

现在,如果要删除其中一个元素,我会遇到一个问题。 假设我之前写的是正确的,我不能只写
test=test.next
如果我想删除“列表”中的一个元素,我应该如何处理这个问题?

还有一件事;我不能使用LinkedList或其他任何东西,这就是我应该如何完成我的任务。

你要找的是Java迭代器。请看一下这个以获得帮助。本质上,您应该用Java创建一个类型化集合,例如
List
,并在该列表上使用迭代器
List
来自
java.util

您正在实现非常标准的链表删除算法。这里有一个简单的方法,但是你可以搜索你自己并跟随任何东西

您也可以实际使用Java

您还可以尝试读取LinkedList方法的实际Java源代码


我还想指出,您并不是在“使用列表之类的类”。您只是在实现一个LinkedList,并选择调用您的类示例,而不是List、LinkedList或SingleLinkedList。

使用a这并不是真正的重复,我在这里看到了对变量和引用的误解