gpt4 book ai didi

java - 在java中实例化递归定义的复合泛型类

转载 作者:太空宇宙 更新时间:2023-11-04 11:44:05 25 4
gpt4 key购买 nike

我定义了包含 GraphNode 的 Graph 类,如下所示,我声明 N 的目的是使用泛型比较 2 个 GraphNode 对象。

问题是我该如何实例化递归绑定(bind)的图。

声明时出错,如下所示。

Graph<Integer,Comparable<GraphNode>> graph = new Graph<>();

绑定(bind)不匹配:类型 Comparable<GraphNode>不是有界参数 <N extends Comparable<GraphNode<T,N>>> 的有效替代类型 Graph<T,N>

public class Graph<T, N extends Comparable<GraphNode<T, N>>> {

private N root;

private Class<N> clazz;

Graph(Class<N> clazz) {
this.clazz = clazz;
}

public N getInstance() {
try {
return clazz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
return null;
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
}
}

@SuppressWarnings("unchecked")
public void insert(T d, N n) {
if (root == null && n == null)
root = getInstance();
if (root == null)
root = n;
N node = root;
while (node != null) {
if (node.equals(n)) {
N newNode = getInstance();
((GraphNode<T, N>) newNode).setAdjNode(newNode);
}
}
}

}

public class GraphNode<T, N extends Comparable<GraphNode<T, N>>> implements Comparable<N> {

private T data;

private LinkedHashSet<N> adjNodes = new LinkedHashSet<>();

GraphNode() {
data = null;
}

GraphNode(T d) {
setData(d);
}

public void setAdjNode(N n) {
adjNodes.add(n);
}

public T getData() {
return data;
}

public void setData(T data) {
this.data = data;
}

@Override
public int hashCode() {
return data.hashCode();
}

@Override
public boolean equals(Object obj) {
if (obj instanceof GraphNode<?, ?>) {
return ((GraphNode<?, ?>) obj).getData() == this.getData();
}
return false;
}

@Override
public String toString() {
return data + "";
}

@Override
public int compareTo(N o) {
return this.compareTo(o);
}

}

最佳答案

这解决了我的上述问题

 public class Graph<T extends Comparable<T>> {

private GraphNode<T> root;

public void insert(T d, GraphNode<T> n) {
if (root == null && n == null)
root = new GraphNode<T>(d);
if (root == null)
root = n;
GraphNode<T> node = root;
Queue<GraphNode<T>> queue = new ConcurrentLinkedQueue<>();
queue.add(root);
while (!queue.isEmpty()) {
node = queue.poll();
node.setNodeColor(color.BLACK);
if (node.equals(n)) {
GraphNode<T> newNode = new GraphNode<T>(d);
((GraphNode<T>) newNode).setAdjNode(newNode);
} else {
queue.addAll(node.getUnexploredAdjNodes());
}
}
}

}
public class GraphNode<T extends Comparable<T>> implements Comparable<GraphNode<T>> {

enum color {
WHITE, GREY, BLACK
};

private T data;

private color nodeColor = color.WHITE;

private LinkedHashSet<GraphNode<T>> adjNodes = new LinkedHashSet<>();

GraphNode() {
data = null;
}

GraphNode(T d) {
setData(d);
}

public void setAdjNode(GraphNode<T> n) {
adjNodes.add(n);
}

public T getData() {
return data;
}

public void setData(T data) {
this.data = data;
}

public LinkedHashSet<GraphNode<T>> getAdjNodes() {
return adjNodes;
}

public LinkedHashSet<GraphNode<T>> getUnexploredAdjNodes() {
LinkedHashSet<GraphNode<T>> n = new LinkedHashSet<>();
for (GraphNode<T> node : adjNodes) {
if (node.getNodeColor() == color.WHITE)
n.add(node);
}
return n;
}

public color getNodeColor() {
return nodeColor;
}

public void setNodeColor(color nodeColor) {
this.nodeColor = nodeColor;
}

@Override
public int hashCode() {
return data.hashCode();
}

@Override
public boolean equals(Object obj) {
if (obj instanceof GraphNode<?>) {
return ((GraphNode<?>) obj).getData() == this.getData();
}
return false;
}

@Override
public int compareTo(GraphNode<T> o) {
return data.compareTo(o.data);
}

@Override
public String toString() {
return data + "";
}
}
Graph<Integer> graph = new Graph<>();

关于java - 在java中实例化递归定义的复合泛型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42502997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com