我试过这个并从JAVA得到奇怪的行为,有人可以帮我解释一下吗?
boolean testNull(String... string) {
if(string == null) {
return true;
} else {
System.out.println(string.getClass());
return false;
}
}
boolean callTestNull(String s) {
return testNull(s);
}
Run Code Online (Sandbox Code Playgroud)
然后我有测试用例:
@Test
public void test_cases() {
assertTrue(instance.testNull(null)); // NULL
assertFalse(instance.testNull()); // NOT NULL
assertFalse(instance.callTestNull(null)); // NOT NULL
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是,如果我打电话testNull()直接与参数null,我会true回来,但如果调用callTestNull()与null,它调用testNull(),它告诉我该参数不为空,但空数组.
Map在scala中创建时,我打电话给我Map(entities.map{e => e.id -> e}),然后我得到:
found : scala.collection.mutable.IndexedSeq[(Int, Entity)]
required: (Int, Entity)
Run Code Online (Sandbox Code Playgroud)
这是因为签名Map.apply是:def apply[A, B](elems: (A, B)*): CC[A, B],这需要一个varargs样式参数.
有没有办法转换,IndexedSeq以便可以通过Map.apply?
如何传递变长参数Go?例如,我想打电话
func MyPrint(format string, args ...interface{}) {
fmt.Printf("[MY PREFIX] " + format, ???)
}
// to be called as: MyPrint("yay %d", 213)
// or MyPrint("yay")
// or MyPrint("yay %d %d",123,234)
Run Code Online (Sandbox Code Playgroud) void TestPrint(char* format, ...)
{
va_list argList;
va_start(argList, format);
printf(format, argList);
va_end(argList);
}
int main()
{
TestPrint("Test print %s %d\n", "string", 55);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我需要得到:
Test print string 55
Run Code Online (Sandbox Code Playgroud)
实际上,我得到了垃圾输出.这段代码有什么问题?
考虑方法声明:
String.format(String, Object ...)
Run Code Online (Sandbox Code Playgroud)
该Object ...参数只是对Objects 数组的引用.有没有办法使用此方法引用实际Object数组?如果我将Object数组传递给...参数 - 结果参数值是否为二维数组 - 因为a Object[]本身是Object:
Object[] params = ....; // Make the array (for example based on user-input)
String s = String.format("%S has %.2f euros", params);
Run Code Online (Sandbox Code Playgroud)
所以数组的第一个组件(在String.format方法中使用)将是一个数组,他将生成:
[class.getName() + "@" + Integer.toHexString(hashCode())]
Run Code Online (Sandbox Code Playgroud)
然后是一个错误,因为数组大小是1.
该大胆的顺序是真正的问题.
这是第二个问题:是否一个...数组/参数有名字吗?
我希望用C/C++做到这一点.
我遇到了可变长度参数,但这表明使用libffi的 Python和C解决方案.
现在,如果我想用printf函数包装myprintf
我的工作如下:
void myprintf(char* fmt, ...)
{
va_list args;
va_start(args,fmt);
printf(fmt,args);
va_end(args);
}
int _tmain(int argc, _TCHAR* argv[])
{
int a = 9;
int b = 10;
char v = 'C';
myprintf("This is a number: %d and \nthis is a character: %c and \n another number: %d\n",a, v, b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但结果不如预期!
This is a number: 1244780 and
this is a character: h and
another number: 29953463
Run Code Online (Sandbox Code Playgroud)
我错过的任何一点?
我试图理解java如何处理函数调用中的歧义.在下面的代码中,调用method是不明确的,但method2不是!!!
我觉得两者都是模棱两可的,但为什么这会在我发出评论时编译method?为什么method2不模棱两可?
public class A {
public static <K> List<K> method(final K arg, final Object... otherArgs) {
System.out.println("I'm in one");
return new ArrayList<K>();
}
public static <K> List<K> method(final Object... otherArgs) {
System.out.println("I'm in two");
return new ArrayList<K>();
}
public static <K, V> Map<K, V> method2(final K k0, final V v0, final Object... keysAndValues) {
System.out.println("I'm in one");
return new HashMap<K,V> ();
}
public static <K, V> Map<K, V> method2(final Object... keysAndValues) …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个函数,它将一个可变函数作为参数,即
func :: (a -> ... -> a) -> a
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点?
我已经阅读了关于多变量函数的信息,我确信Oleg已经做到了,但是我试图将这个模式应用于具有可变函数作为参数的函数.特别是Olegs方法似乎只适用于格拉斯哥扩展,我希望解决方案在纯Haskell 98中工作(就像Text.Printf一样).
我问的原因是我正在尝试构建一个函数,该函数将布尔函数作为参数并检查它是否是重言式,即
isTautology :: (Bool -> ... -> Bool) -> Bool
Run Code Online (Sandbox Code Playgroud)
所以可以输入:
isTautology (\x -> x && not x)
isTautology (\x y -> x && y || not y)
Run Code Online (Sandbox Code Playgroud)
我的问题是,我一直在阅读的技巧是使返回类型成为一个类型变量(这样它可以是结果或另一个函数),但我的返回类型是固定的(Bool).
我习惯于声明像这样的可变函数:
int f(int n, ...);
Run Code Online (Sandbox Code Playgroud)
阅读C++编程语言时,我发现书中的声明省略了逗号:
int f(int n...); // the comma has been omitted
Run Code Online (Sandbox Code Playgroud)
看起来这个语法是特定于C++的,因为当我尝试使用C编译器编译时出现此错误:
test.c:1:12: error: expected ‘;’, ‘,’ or ‘)’ before ‘...’ token int f(int n...);
写作int f(int n, ...)和int f(int n...)之间有什么区别吗?
为什么这个语法添加了C++?
在我的Java代码中,我经常使用非常方便的method(Class... args)varargs.据我所知,它们允许您传递任意数量的Class对象或数组Class[].由于我也经常使用Java集合类,因此两者之间缺乏兼容性使我感到沮丧.结果,我最终做了collection.toArray(),但这有一些类型安全问题.
所以现在问题是:为什么Java不允许实例Iterable<T>作为vararg参数,只要泛型类型适合T...vararg 的类型?不是每个人都一直使用列表,集等吗?是否有一种简单,类型安全的方式来提供从集合到vararg的转换?