如何在tcl文件中将tcl列表转换为java列表



我能够将java列表转换为tcl列表,但无法将tcl列表转换为java列表

样本.tcl:-

global mgr;
set mgr [java::new sample_impl]
proc sample {arg_list} {
# what i need to do inside hear
$::mgr sample_impl_in_java "java_list";  ## hear i call the java function that takes the java_list as a argument 
}
set var "hello"
sample {$var "world"}; ## pass a tcl list in sample function in tcl

sample_impl.java:-

public void sample_impl_in_java(List listObj) {
System.out.println(listObj);
}

任何人都可以帮我找出解决方案。

Tcl的列表类型很像Java ArrayList,但API不同,修改语义也不同。将一个转换为另一个需要手动步骤:

proc sample {arg_list} {
# Make an ArrayList
set jlist [java::new java.util.ArrayList]
# Copy the items over as strings; if you want a list of other things, this is the bit you change
foreach item $arg_list {
$jlist add [java::new String $item]
}
# Do the call
$::mgr sample_impl_in_java $jlist
}

你可能想这样称呼它:

set var "hello"
sample [list $var "world"]

另一种方法会起作用,但结果不太可能是你所期望的。

最新更新