gpt4 book ai didi

c# - 如何在C#或CS1503中使用通用变量

转载 作者:行者123 更新时间:2023-12-02 10:52:24 25 4
gpt4 key购买 nike

我写了一个带有链表的HashTable。
这是节点元素的代码

    class ListNode<T>
{
public T value{get; set;}
public ListNode<T> next{get; set;}

public void Insert(T newValue){
value = newValue;
}
}
这是Linkedlist的实现
    class LinkedL<T> : IEnumerable<T>
{
ListNode<T> head;
ListNode<T> tail;

public void Add(T newVal){
ListNode<T> node = new ListNode<T>();
node.Insert(newVal);

if (head == null)
head = node;
else
tail.next = node;

tail = node;
}
}
最后是带有简单哈希函数X%N的HashTable
    class HashTable 
{
int N;
public LinkedL<ListNode<int>>[] values;

public HashTable(int n){
N = n;
values = new LinkedL<ListNode<int>>[N];
}

public void Insert(int newValue){
var mod = newValue % N;
values[mod].Add(newValue);
}
}
当我尝试将Int(int)插入HashTable实例编译器时,抛出CS1503错误 cannot convert from "int" to "this.ListNode<int>" 这有点令人困惑。

最佳答案

那是因为当您执行此values[mod]时,您正在访问LinkedL<ListNode<int>>,因此Add方法需要添加ListNode<int>
我认为该混淆在您的声明中位于public LinkedL<ListNode<int>>[] values;中。
您最后是addind []。因此,这意味着您声明的是ArrayLinkedL,而不仅仅是LinkedL

关于c# - 如何在C#或CS1503中使用通用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64427542/

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