Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 强制使用数组调用解析为varargs调用_Java_Casting_Jvm_Variadic Functions_Type Inference - Fatal编程技术网

Java 强制使用数组调用解析为varargs调用

Java 强制使用数组调用解析为varargs调用,java,casting,jvm,variadic-functions,type-inference,Java,Casting,Jvm,Variadic Functions,Type Inference,好的,我有一个函数,比如 public static UnorderedList newUnorderedList(Object... items) { return new UnorderedList( stream(items) .peek(e -> checkNotNull(e, "Cannot create null list item")) .map(e -> { if

好的,我有一个函数,比如

public static UnorderedList newUnorderedList(Object... items) {
    return new UnorderedList(
        stream(items)
            .peek(e -> checkNotNull(e, "Cannot create null list item"))
            .map(e -> {
                if (e instanceof Component) return newListItem((Component) e);
                if (e instanceof String) return newListItem((String) e);
                throw new IllegalArgumentException("List item must be String or Component but is neither: " + e.getClass().getName());
            }).toArray(ListItem[]::new)
    );
}
编辑:注意:这里的
无序列表
是Vaadin对html
标记的实现,我不想获取java列表。)

当您使用数组调用它时,这将触发一个警告,表示您不清楚是要将数组本身视为单个元素还是元素的容器

不要马上看到一个优雅的解决方法。这两个我都不喜欢:

  • 始终强制转换到
    对象[]
  • 对象…
    转换为
    集合

是否有注释或其他东西可以让编译器知道总是将数组解析为带注释方法上的vararg调用?(在方法声明上,而不是在调用站点上。)

您以错误的方式创建无序列表

假设它是一个集合:

    Object[] objects = new Object[]{1, "hello", "there", "george"};
    LinkedList<AtomicReference<?>> list = Arrays.stream(objects)
            .filter(Objects::nonNull)
            .map(e -> {
                if (e instanceof Integer) return new AtomicReference<>(e);
                if (e instanceof StringBuilder) return new AtomicReference<>(e);
                throw new IllegalArgumentException("PAIN");
            })
            .collect(Collectors.toCollection(LinkedList::new));
Object[]objects=newobject[]{1,“你好”,“那里”,“乔治”};

LinkedList您以错误的方式创建无序列表

假设它是一个集合:

    Object[] objects = new Object[]{1, "hello", "there", "george"};
    LinkedList<AtomicReference<?>> list = Arrays.stream(objects)
            .filter(Objects::nonNull)
            .map(e -> {
                if (e instanceof Integer) return new AtomicReference<>(e);
                if (e instanceof StringBuilder) return new AtomicReference<>(e);
                throw new IllegalArgumentException("PAIN");
            })
            .collect(Collectors.toCollection(LinkedList::new));
Object[]objects=newobject[]{1,“你好”,“那里”,“乔治”};

LinkedList从你的问题来看,我猜你想做以下事情:

Object [] items = new Object [x];
items [0] = new Object [] {"Object1", "Object2", "Object3"};
var result = newUnorderedList( items );
这会将带有字符串的数组视为单个项,从而在调用时引发异常

var result = newUnorderedList( items [0] );
将为
结果
返回3个元素

没有强制将单个数组作为单个项而不是项列表进行处理的注释。享受以下签名带来的乐趣:

Object function( Object [] ... );

根据你的问题,我猜你想做以下几点:

Object [] items = new Object [x];
items [0] = new Object [] {"Object1", "Object2", "Object3"};
var result = newUnorderedList( items );
这会将带有字符串的数组视为单个项,从而在调用时引发异常

var result = newUnorderedList( items [0] );
将为
结果
返回3个元素

没有强制将单个数组作为单个项而不是项列表进行处理的注释。享受以下签名带来的乐趣:

Object function( Object [] ... );

您可以重载该方法:

public static UnorderedList newUnorderedList(Object first, Object... other) {
    return newUnorderedListImpl(Stream.concat(Stream.of(first), Arrays.stream(other)));
}
public static UnorderedList newUnorderedList(Object[] items) {
    return newUnorderedListImpl(Arrays.stream(items));
}
private static UnorderedList newUnorderedListImpl(Stream<?> items) {
    return new UnorderedList(
        items
            .peek(e -> checkNotNull(e, "Cannot create null list item"))
            .map(e -> {
                if (e instanceof Component) return newListItem((Component) e);
                if (e instanceof String) return newListItem((String) e);
                throw new IllegalArgumentException("List item must be String or Component but is neither: " + e.getClass().getName());
            }).toArray(ListItem[]::new)
    );
}

您可以重载该方法:

public static UnorderedList newUnorderedList(Object first, Object... other) {
    return newUnorderedListImpl(Stream.concat(Stream.of(first), Arrays.stream(other)));
}
public static UnorderedList newUnorderedList(Object[] items) {
    return newUnorderedListImpl(Arrays.stream(items));
}
private static UnorderedList newUnorderedListImpl(Stream<?> items) {
    return new UnorderedList(
        items
            .peek(e -> checkNotNull(e, "Cannot create null list item"))
            .map(e -> {
                if (e instanceof Component) return newListItem((Component) e);
                if (e instanceof String) return newListItem((String) e);
                throw new IllegalArgumentException("List item must be String or Component but is neither: " + e.getClass().getName());
            }).toArray(ListItem[]::new)
    );
}


什么是无序列表?为什么不创建一个返回无序列表的收集器?您是否尝试过使用
数组。流(项目)
@lscoughlin抱歉,我认为这与此无关
UnorderedList
是Vaadin对html
标记的实现。@Lazarportovic到底要做什么?我已经在
stream(items)
中使用了它。什么是无序列表,为什么不创建一个返回无序列表的收集器?您是否尝试过使用
数组。stream(items)
@lscoughlin抱歉,我认为这与此无关
UnorderedList
是Vaadin对html
标记的实现。@Lazarportovic到底要做什么?我已经在
stream(items)
中使用了它。如果我们假设
UnorderedList
对于基于数组的类型来说是个坏名字?顺便说一下,代码主要构建一个数组,该数组是
UnorderedList
的构造函数的参数……您假设错误。这是Vaadin对html
标记的实现。它接收
组件的varargs,因此是数组。在问题中添加了这些信息。如果我们假设
UnorderedList
对于基于数组的类型来说只是一个坏名字?顺便说一下,代码主要构建一个数组,该数组是
UnorderedList
的构造函数的参数……您假设错误。这是Vaadin对html
标记的实现。它接收
组件的varargs,因此是数组。将该信息添加到问题中。恰恰相反,我希望传递一个数组并将其视为varargs参数。这里的问题是,
Object[]
是一个
对象
,因此函数——根据警告——不确定我希望它以哪种方式处理,除非我将其强制转换为其中一种。相反,我希望传递一个数组,并将其作为varargs参数处理。这里的问题是,
Object[]
是一个
对象
,因此函数——根据警告——不确定我希望它以哪种方式处理,除非我将其转换为其中之一。这是一个有趣的解决方案。您能否解释一下为什么这不会与
varargs
方法遇到相同的问题?java在决定是使用数组参数调用该方法还是使用对象参数调用该方法以及附加的空数组时不应该遇到同样的麻烦吗?当您有一个方法
m(object)
和一个方法
m(object[])
并使用数组调用
m
时,方法
m(object[])
具有优先权,这是旧的“调用最特定的方法”规则,早在Java中添加varargs之前就存在了。事实上,当没有VARARGS的另一种匹配方法存在时,java甚至不考虑VARARGS方法。如果你想调用
newUnorderedList(anObject,anArray)
,你现在会遇到和以前一样的问题,但是这种用法不在你的愿望列表中,所以如果有人想这样使用它,他们必须接受消歧。一个有趣的解决方案。您能否解释一下为什么这不会与
varargs
方法遇到相同的问题?java在决定是使用数组参数调用该方法还是使用对象参数调用该方法以及附加的空数组时不应该遇到同样的麻烦吗?当您有一个方法
m(object)
和一个方法
m(object[])
并使用数组调用
m
时,方法
m(object[])
具有优先权,这是旧的“调用最特定的方法”规则,早在Java中添加varargs之前就存在了。事实上,当没有VARARGS的另一种匹配方法存在时,java甚至不考虑VARARGS方法。如果您想调用
newUnorderedList(anObject,anArray)
,您现在会遇到与以前相同的问题,但是这种用法不在您的愿望列表中,因此如果有人想这样使用它