在 Java 中使用带有链表的迭代器

发布于 2024-12-01 15:27:18 字数 823 浏览 1 评论 0 原文

我创建了一个名为“SList”的链表类,它允许创建一个空链表。 “insertFront”方法在列表的前面插入一个对象并增加大小。下面,在主类中,我创建了一个 SList 对象并向列表添加了两个字符串。我想打印这份清单。我尝试创建一个从 java.util 导入的迭代器对象,但编译器在“Iterator”下给了我一个红色下划线。为什么我会收到此错误?我应该如何打印这个链接列表?

public class SList 
{
private SListNode head;
private int size; //number of items in the list

public SList() //constructor, empty list
{
    head = null;
    size = 0;
}
public void insertFront(Object item)
{
    head = new SListNode(item, head);
    size++;
}

}

import java.util.*;

public class LinkMain 
  {
        public static void main(String[] args)
    {
    String apples = "apples";
    String oranges = "oranges";

    SList x = new SList();

    x.insertFront(apples);
    x.insertFront(oranges);

    Iterator iter = x.Iterator();






}

}

I have created a linked-list class called "SList", which allows an empty linked-list to be created. The method "insertFront" inserts an object at the front of the list and increases the size. Below, in the main class, I have created an SList object and added two strings to the list. I would like to print this list. I attempted to create an iterator object imported from java.util, but the compiler is giving me a red underline under "Iterator". Why am I getting this error? How should I print this linked-list?

public class SList 
{
private SListNode head;
private int size; //number of items in the list

public SList() //constructor, empty list
{
    head = null;
    size = 0;
}
public void insertFront(Object item)
{
    head = new SListNode(item, head);
    size++;
}

}

import java.util.*;

public class LinkMain 
  {
        public static void main(String[] args)
    {
    String apples = "apples";
    String oranges = "oranges";

    SList x = new SList();

    x.insertFront(apples);
    x.insertFront(oranges);

    Iterator iter = x.Iterator();






}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離殇 2024-12-08 15:27:18

我对你的代码有点困惑。鉴于您当前的实现,没有任何方法可以从 SList 类返回 Iterator 对象。

如果您希望基于 Java Collections 框架进行实现,则应在代码中实现 Collection 接口,如下所示:

public class SList<E> implements Collection<E> {
    //override methods here
}

是参数化类型这使得你的类类型安全。如果实现 Collection 接口,则需要实现多个方法,其中一个方法将返回 Iterator

如果您选择不使用 Java Collections 框架,也很容易做到,您只需要创建自己的所有方法即可。

可以在这里找到一个很棒的泛型教程(如果您不想使用集合框架)位于 Java 泛型教程页面

还可以在此处找到关于 Collections 框架的另一个精彩教程 集合的 Java 教程页面

玩得开心!

I'm a bit confused by your code. Given your current implementation, there is no method that will return an Iterator object from your SList class.

If you would like to base your implementation off of the Java Collections framework, you should implement the Collection interface in your code as such:

public class SList<E> implements Collection<E> {
    //override methods here
}

The <E> is a parameterized type that makes your class type-safe. If you implement the Collection interface, you will be required to implement several methods, among them is a method that will return an Iterator<E>.

If you choose not to use the Java Collections framework, it is just as easy to do, you'll just need to create all of your own methods.

A great tutorial for generics (if you don't want to use the Collections framework) can be found here at the Java Tutorials page for generics

Another great tutorial for the Collections framework can also be found here at the Java Tutorials page for Collections

Have fun!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文