标签云

微信群

扫码加入我们

WeChat QR Code

So I have this linked list class:public class LinkedList {private LLNode rootNode;public Node FindItemByData(String data) {if(rootNode == null)return null;elsereturn rootNode.findItemByData(data);}And this node class: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); }I want to re-use the linked list for storing edges in a graph within each Node data of the LLNode. I had a go at replacing the type using Generic Types but this breaks the functionality of the findItemByData function as it relies on data being explicitly declared as a Node. Is there any way I can reuse this class for multiple types? Or should I not be referring to data.name in a Generic Class?Implementation context:public class Graph {//USE LINKED LISTLinkedList 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 nameint x,y;//Node's coordsLinkedList Adjacencies;int noOfAdj = 0;int size = 0;public Node(String name, int x, int y) {//Constructorthis.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}}Edit: attempt at using generics:public class LinkedList<T> {private LLNode rootNode;public T FindItemByData(String data) {if(rootNode == null)return null;elsereturn 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 nameint x,y;//Node's coordsLinkedList<Edge> Adjacencies;int noOfAdj = 0;int size = 0;public Node(String name, int x, int y) {//Constructorthis.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}}


To clarify, The Nodes Linked List stores objects of type Node, where as the Adjacencies Linked List needs to be of type Edge.

2019年04月22日23分38秒

Hey Erick, the code shown is prior to my attempt at generics. I tried public class LLNode<T>{..}and declaring data as T data but this did not work as LLNode's methods relied on a Node type (findItemByData)

2019年04月22日23分38秒

It worked! To clarify for anyone else, I extended the Edge class and created an empty constructor inside the Node class.

2019年04月23日23分38秒