不是一个真正的答案,但表示这另一种方法:
init
var h = new HashTable of string, Int? (str_hash, str_equal)
h["a"] = Int ({1, 2, 3})
h["b"] = Int ({5, 6, 7})
h["a"].append ({4})
struct Int
data: array of int
construct (a: array of int)
this.data = a
def append (a: array of int)
this.data = this.data + a
现在有没有“变量和参数”正在进行的混合了,解决了原来的代码触发编译错误。
的问题是,这还导致一个编译错误:
resize_array.gs:14.21-14.33: error: Incompatible operand
this.data = this.data + a
哪些可以被简化为以下代码:
init
x: array of int = {1, 2, 3}
y: array of int = {4, 5, 6}
z: array of int = x + y
其中也产生相同的编译错误。
resize_array.gs:21.23-21.27: error: Incompatible operand
z: array of int = x + y
我已经添加在此基础上一个新的问题:
How to concatenate two arrays?
事实证明concating阵列(它适用于字符串虽然!)是不是在瓦拉/精灵一个简单的任务。
有关如何执行此操作的解决方案,请参阅其他问题。
我个人会为此使用Gee
容器(如果我不必经常调用一些需要普通数组的C函数)。
使用Array of int
解决办法:
init
var h = new HashTable of string, Int? (str_hash, str_equal)
h["a"] = Int ({1, 2, 3})
h["b"] = Int ({5, 6, 7})
h["a"].append ({4})
struct Int
data: Array of int
construct (a: array of int)
data = new Array of int;
append (a)
def append (a: array of int)
data.append_vals (a, a.length)
为什么你想避免GLib.Array?它的设计是为了让数组操作更容易? –
也相关:http://stackoverflow.com/questions/31358261/what-is-the-syntax-for-nested-generic-types-in-genie –
感谢您的答案。不要避免使用Array或GenericArray。试着理解这些容器。所以我做了一些测试。 –