草庐IT

bounded-wildcard

全部标签

Java 泛型 : adding wrong type in collection

谁能解释一下?我有这两个类:abstractclassAnimal{publicvoideat(){System.out.println("Animaliseating");}}classDogextendsAnimal{publicvoidwoof(){System.out.println("woof");}}classCatextendsAnimal{publicvoidmeow(){System.out.println("meow");}}这是Action:importjava.util.ArrayList;importjava.util.List;publicclassTest

【二分—STL】lower_bound()函数&&upper_bound()函数的使用总结

目录一、基本用法:二、具体到题目中如何应用1、数的范围2、递增三元组3、数组元素的目标和一、基本用法:lower_bound()用于二分查找区间内第一个大于等于某值(>=x)的迭代器位置upper_bound()用于二分查找区间内第一个大于某值(>x)的迭代器位置函数前两个参数分别是已被排序的序列的起始迭代器位置和结束迭代器位置,将要被查询的范围为[first,last),是一个左闭右开区间的范围。第三个参数则是需要搜寻的元素的值。最后返回查询成功的迭代器的地址。搜索的序列当中若无合法答案返回last迭代器地址注意点:返回的是地址,不是那个要查找的数的下标。所以就注定了在这个函数的后边就要减去

java - Checkstyle eclipse 插件 : error cannot initialize module TreeWalker Token "WILDCARD_TYPE"

我有一个eclipsecheckstyle插件的问题,我刚刚安装了这个,当我在java文件上使用sun_checkstyle(eclipse)执行checkstyle-configuration时,我有这个错误:cannotinitializemoduleTreeWalker-Token"WILDCARD_TYPE"wasnotfoundinAcceptabletokenslistincheckcom.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck@2261fbdcannotinitializemo

具有有界通配符的泛型类型的 Java 泛型集合

请帮我解决这个问题:如果LionIS-AAnimal并给出Cage:Cagec=newCage();//ok,但是Set>cc=newHashSet>();//notok我在这里没有看到什么? 最佳答案 当分配给具有非通配符泛型类型的变量(Set)时T,被分配的对象必须恰好有T作为其通用类型(包括T的所有通用类型参数,通配符和非通配符)。在你的情况下T是Cage,与Cage不同类型.你能做什么,因为Cage可分配给Cage,是使用通配符类型:Set>a=newSet>(); 关于具有有界

java - 在 Java 中,在泛型实例上调用 getClass 时如何避免原始类型?

假设我在Java中有这个:Listlist=newArrayList();list.getClass();最后一个表达式的类型是Class.我明白为什么,由于删除,它不能是Class>.但是为什么不能是Class>呢??如果我想将这个表达式的结果分配给一个以某种方式保持该类实际上是某种类型的信息List?ClasslistClass=list.getClass();//rawtypewarningClass>listClass=(Class>)list.getClass();//uncheckedcastwarning 最佳答案 首

javax.naming.NameNotFoundException : Name [comp/env] is not bound in this Context. Java 调度程序无法找到 [comp] 错误

我想做的是在一段时间后更新我的数据库。所以我正在使用java调度程序和连接池。我不知道为什么,但我的代码只能工作一次。它将打印:initsuccesssuccessjavax.naming.NameNotFoundException:Name[comp/env]isnotboundinthisContext.Unabletofind[comp].atorg.apache.naming.NamingContext.lookup(NamingContext.java:820)atorg.apache.naming.NamingContext.lookup(NamingContext.jav

java - 具有最终上限的通配符

Class可以正常编译,但是Integer是最终类型,因此将它用作上限没有意义(永远不会extend它)。如果您尝试使用最终类型作为类型参数的上限,您将收到编译器警告:ThetypeparameterTshouldnotbeboundedbythefinaltypeInteger.Finaltypescannotbefurtherextended为什么使用最终类型作为通配符的上限非常好,但对类型参数抛出警告?为什么Java甚至允许通配符受最终上层类型的限制? 最佳答案 Class不像Class那样允许赋值.例如,编译:Classnu

java - 多个通配符边界

假设我有以下类(class):publicclassEither{publicObjectget();}Either是一种存储类型A或B的一个对象的类型。get()检索那个对象。问题是是否可以使用泛型来改变get()的方法签名这样返回的类型就不仅仅是Object,但是A和B的任何公共(public)父类(superclass)型。例如,Either可以有get()返回Number,一个Either,Set>可以有get()返回Iterable或Collection,等等。(显然,Either应该有get()返回Foo)。如果这完全可能的话,如果我有Either,List>,什么是最具体

Java 泛型 : How does method inference work when wildcard is being used in the method parameters?

假设我有以下内容:classx{publicstaticvoidmain(String[]args){Lista=newLinkedList();Listb=newLinkedList();Listc=newLinkedList();abc(a,"Hello");//(1)Errorabc(b,"Hello");//(2)Errorabc(c,"Hello");//(3)okdef(b);//(4)ok//ShowinginferenceatworkInteger[]a={10,20,30};//(5)Tisinferredtobe?extendsObjectMethodsignatu

java - 在 Guava 中,为什么在可能使用 "T"的地方只使用 "? super T"?

为什么实用程序工厂方法经常使用特定的通用参数(如T)而不是有界通配符参数(如?superT)?例如Functions#forPredicate的签名是:publicstaticFunctionforPredicate(Predicatepredicate)为什么不使用:publicstaticFunctionforPredicate(Predicatepredicate)哪个可以使类似下面的事情成为可能?PredicateisPositivePredicate=...FunctionisPositiveInteger=Functions.forPredicate(isPositiveP