列表元素的通用类

发布于 2024-10-12 10:56:07 字数 195 浏览 6 评论 0原文

我正在使用通用类练习编程。我想为列表元素实现一个类,该类保存对泛型类型对象的引用和对下一个列表元素的引用。

所以我想出了这个类:

class List<T>{
    T val;
    List next:
}

如何定义这个类的构造函数?对于理解泛型类及其使用还有其他建议吗?

I am practicing programming with generic classes. I want to implement a class for a list element that holds a reference on an object of generic type and a reference on the next list element.

So I came up with this class:

class List<T>{
    T val;
    List next:
}

How would you define the constructor for this class? Any other advice to understand generic classes and their use?

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

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

发布评论

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

评论(4

月亮坠入山谷 2024-10-19 10:56:07

您可以将其保留为默认构造函数,然后使用它 <代码>列表<字符串> list = new List(); - 现在类中的 val 将是 String。另一种方式:

public class List<T>{
    T val;
    List<T> next;

    public List(T val, List<T> next) {
        this.val = val;
        this.next = next;
    };

    public T getVal() {
        return val;
    }
}

然后你可以这样使用它:

List<String> strList = new List<String>("test", null);
System.out.println( strList.getVal() );

结果应该打印“test”

至于建议,我认为最好的办法是阅读这本书:Java 泛型和集合
它包含很好的描述和很多如何使用它的示例。

you can leave it with default constructor and then use it List<String> list = new List<String>(); - now val in your class will be String. Another way:

public class List<T>{
    T val;
    List<T> next;

    public List(T val, List<T> next) {
        this.val = val;
        this.next = next;
    };

    public T getVal() {
        return val;
    }
}

And then you can use it this way:

List<String> strList = new List<String>("test", null);
System.out.println( strList.getVal() );

as result "test" should be printed

As for advice, I think the best thing is to read this book: Java Generics and Collections
it contains good description and a lot of examples how to use it.

ま柒月 2024-10-19 10:56:07

这是一个很好的资源,可以帮助您了解仿制药:

http://www.angelikalanger.com /GenericsFAQ/JavaGenericsFAQ.html

This is a great resource for helping you get your head round generics:

http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html

帝王念 2024-10-19 10:56:07

查看 LinkedList 来了解您想要执行的操作可能会对您有所帮助。

Javadoc: http://download.oracle.com/javase /6/docs/api/java/util/LinkedList.html
源代码和 Java API 文档都可以在此处下载: http:// www.oracle.com/technetwork/java/javase/downloads/index.html

您将能够浏览源代码并了解它是如何使用 Java 中的泛型实际实现的。

It maybe helpful for you to look at a LinkedList for what you're trying to do.

Javadoc: http://download.oracle.com/javase/6/docs/api/java/util/LinkedList.html
Both the source and the Java API docs can be downloaded here: http://www.oracle.com/technetwork/java/javase/downloads/index.html

You'll be able to look through the source and see how it is actually implemented with Generics in Java.

泪是无色的血 2024-10-19 10:56:07

乍一看,我以为您想要更多 LISP 风格的集合,其中有一个“头”元素和一个“尾”列表。将一些代码放在一起给了我这样的结果:

package generic.list;

import java.util.List;

public class GenericList<T>
{
    private T head;

    private GenericList<T> tail;

    public GenericList(List<T> initialList)
    {
        if ( !initialList.isEmpty() )
        {
            head = initialList.get(0);

            if ( initialList.size() > 1 )
            {
                tail = new GenericList<T>(initialList.subList(1, initialList.size()));
            }
        }
    }

    public T getHead()
    {
        return head;
    }

    public GenericList<T> getTail()
    {
        return tail;
    }
}

虽然这种结构可以导致对递归算法的一些真正伟大的研究,但当您尝试学习泛型时,它实际上并没有为您做很多事情。也就是说,这是我编写的一个快速测试床,以确保这确实有效:


package generic.list;

import static org.junit.Assert.*;

import java.util.Arrays;

import org.junit.Test;

public class GenericListTest
{
    private GenericList l;

    @Test
    public void testConstructorNoElements()
    {
        l = new GenericList(Arrays.asList(new String[] {}));

        assertNull(l.getHead());
        assertNull(l.getTail());
    }

    @Test
    public void testConstructorOneElement()
    {
        l = new GenericList(Arrays.asList("One"));

        assertNotNull(l.getHead());
        assertEquals("One", l.getHead());
        assertNull(l.getTail());
    }

    @Test
    public void testConstructorMultipleElements()
    {
        l = new GenericList(Arrays.asList("One", "Two", "Three"));

        assertNotNull(l.getHead());
        assertEquals("One", l.getHead());

        assertNotNull(l.getTail());
        assertEquals(l.getTail().getHead(), "Two");
        assertEquals(l.getTail().getTail().getHead(), "Three");
        assertNull(l.getTail().getTail().getTail());
    }
}

这至少应该让您更好地了解如何实例化和使用泛型类。

您可以在 http://download.oracle.com/ 找到在线课程javase/tutorial/java/generics/index.html

关于泛型最需要理解的一点是,当您在类名中列出“T”(如“GenericList”中)时,T 就成为该类范围内的一个类。 GenericList 之外没有任何类知道 T 是什么,并且 GenericList 甚至不知道它是什么 - 它所知道的是,无论你在哪里看到 T,它都会与其他地方的 T 具有相同的类型。因此,虽然 GenericList 不一定知道它存储的内容,但它确实知道“head”(T) 的类型与通过initialList 传入的对象的类型相同。

At first glance, I thought you were going for more of a LISP style collection where you'd have a "head" element and a "tail" list. Tossing together some code gave me this:

package generic.list;

import java.util.List;

public class GenericList<T>
{
    private T head;

    private GenericList<T> tail;

    public GenericList(List<T> initialList)
    {
        if ( !initialList.isEmpty() )
        {
            head = initialList.get(0);

            if ( initialList.size() > 1 )
            {
                tail = new GenericList<T>(initialList.subList(1, initialList.size()));
            }
        }
    }

    public T getHead()
    {
        return head;
    }

    public GenericList<T> getTail()
    {
        return tail;
    }
}

While this sort of structure can lead to some really great investigations of recursive algorithms, it really doesn't do a whole heck of a lot for you when you're trying to learn generics. That said, here's a quick test bed I wrote to make sure this actually worked:


package generic.list;

import static org.junit.Assert.*;

import java.util.Arrays;

import org.junit.Test;

public class GenericListTest
{
    private GenericList l;

    @Test
    public void testConstructorNoElements()
    {
        l = new GenericList(Arrays.asList(new String[] {}));

        assertNull(l.getHead());
        assertNull(l.getTail());
    }

    @Test
    public void testConstructorOneElement()
    {
        l = new GenericList(Arrays.asList("One"));

        assertNotNull(l.getHead());
        assertEquals("One", l.getHead());
        assertNull(l.getTail());
    }

    @Test
    public void testConstructorMultipleElements()
    {
        l = new GenericList(Arrays.asList("One", "Two", "Three"));

        assertNotNull(l.getHead());
        assertEquals("One", l.getHead());

        assertNotNull(l.getTail());
        assertEquals(l.getTail().getHead(), "Two");
        assertEquals(l.getTail().getTail().getHead(), "Three");
        assertNull(l.getTail().getTail().getTail());
    }
}

That should at least give you a better idea of how to instantiate and utilize a generic class.

You can find an online lesson at http://download.oracle.com/javase/tutorial/java/generics/index.html.

The biggest thing to understand about generics is that, when you list "T" in the name of the class (as in "GenericList"), T becomes a class for the scope of this class. No classes outside of GenericList know what T is and GenericList doesn't even really know what it is - all it knows is that, everywhere you see a T, it'll be the same type as a T somewhere else. So, while GenericList doesn't necessarily know what it's storing, it does know that the type of "head" (T) is the same type as the objects being passed in via initialList.

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