java - 将链表重新用于另一种类型(通用类型)

标签 java generics linked-list

所以我有这个链表类:

public class LinkedList {
    private LLNode rootNode;

    public Node FindItemByData(String data) {
        if(rootNode == null)
            return null;
        else
            return rootNode.findItemByData(data);
    }

这个节点类:

public class LLNode {


   LLNode tail; //tail node
   Node data; //some data


   public LLNode(LLNode tail, Node data)
   {
       this.tail = tail;
       this.data = data;
   }

   public Node findItemByData(String data) {
       if(this.data.name.equals(data))
           return this.data;
       else
           return this.tail.findItemByData(data);
   }

我想重新使用链表来存储 LLNode 的每个节点数据内的图中的边。我尝试使用通用类型替换类型,但这破坏了 findItemByData 函数的功能,因为它依赖于显式声明为节点的数据。

有什么方法可以将此类重用于多种类型吗?或者我不应该在通用类中引用 data.name 吗?

实现背景:

public class Graph {

    //USE LINKED LIST
    LinkedList Nodes;
    //Node[] Nodes;   
    int noOfNodes;

    public Graph() {
        noOfNodes = 0;
        //Nodes = new Node[25];
        Nodes = new LinkedList();
    }

    public void AddNode(String name, int x, int y) {
        //Nodes[noOfNodes++] = new Node(name,x,y);
        Nodes.AddItem(new Node(name,x,y));
    }

..

public class Node {
    String name;    //Node's name
    int x,y;        //Node's coords
    LinkedList Adjacencies;
    int noOfAdj = 0;
    int size = 0;


    public Node(String name, int x, int y) {    //Constructor
        this.name = name;
        this.x = x;
        this.y = y;
        Adjacencies = new LinkedList();
    }

    public void addAdjacency(String dest, double distance) {
        Adjacencies.AddItem(new Edge(this.name, dest, distance)); //I want to do this
    }
}

编辑:尝试使用泛型:

public class LinkedList<T> {
    private LLNode rootNode;

    public T FindItemByData(String data) {
        if(rootNode == null)
            return null;
        else
            return rootNode.findItemByData(data);
    }
}

public class LLNode<T> {


   LLNode tail; //tail node
   T data; //some data


   public LLNode(LLNode tail, T data)
   {
       this.tail = tail;
       this.data = data;
   }

   public T findItemByData(String data) {
       if(this.data.name.equals(data))
           return (T) this.data;
       else
           return (T) this.tail.findItemByData(data);
   }
}

public class Graph {

    LinkedList<Node> Nodes;
    int noOfNodes;

    public Graph() {
        noOfNodes = 0;
        Nodes = new LinkedList();
    }

    public void AddNode(String name, int x, int y) {
        Nodes.AddItem(new Node(name,x,y));
    }
}

public class Node {
    String name;    //Node's name
    int x,y;        //Node's coords
    LinkedList<Edge> Adjacencies;
    int noOfAdj = 0;
    int size = 0;


    public Node(String name, int x, int y) {    //Constructor
        this.name = name;
        this.x = x;
        this.y = y;
        Adjacencies = new LinkedList();
    }

    public void addAdjacency(String dest, double distance) {
        Adjacencies.AddItem(new Edge(this.name, dest, distance)); //I want to do this
    }
}

最佳答案

基本上你是说 Node 和 Edge 应该遵守相同的契约(Contract)。为此,您应该让他们实现一个接口(interface),其中包含属于该合约的所有方法。

在这种情况下,这可能只是 getData()。然后将此接口(interface)用于可以采用任何 Edge 或 Node 的方法。

另一种方法是让 Edge 成为 Node 的扩展。 公共(public)类 Edge 扩展 Node。然后你就可以在任何需要 Node 的地方使用它。

关于java - 将链表重新用于另一种类型(通用类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35749826/

相关文章:

c++ - 帮助显示两个矩阵的总和(使用链表)

c - 使用 C 中最少的赋值语句构建列表 {1,2,3}

java - 行组合

java - 在ubuntu中执行jar包括dll库

c# - 如何使用 LINQ 查询列表 <T>

vb.net - 当 T 是值类型时,从字符串转换为泛型类型 T 的更快方法?

c - C 统计文本文件数量时发生数据丢失

java - 如何将在 fragment 中创建的 ArrayList<String> 传递给主要 Activity

Java:GregorianCalendar 的最大值和最小值是什么/在哪里?

Java 泛型为什么需要通配符(问号)?