泛型是多态的一种形式吗?(Are generics a form of polymorphism? [closed])
我们可以说泛型是多态的形式吗?
Can we say generics are form of polymorphism?
原文:https://stackoverflow.com/questions/4045945
最满意答案
我不确定你在找什么奇怪的东西。 您有两个 TreeSet实例,每个实例都有自己的
CompareID
实例作为Comparator
,每个CompareID
实例都维护它自己的计数器。因此,看到每个计数器值(0,1,2等)出现两次并不奇怪。 至于计数器值的出现顺序,取决于
TreeSet
的内部实现。至于
等于一组树是否是正确的输出?
是的,两个集都包含相同的元素。 订单无关紧要。 为了澄清 - 如果对于
TreeSet
的某个元素y
,compare(x,y)==0
则contains
和containsAll
TreeSet
的所有方法都会考虑将元素x
包含在TreeSet
,其中compare
是所提供的Comparator
的compare
方法。 因此,在你的例子中,只有id
属性决定两个元素是否相等。以下情形解释了输出:
0 // compare method of 1st CompareID object executed 1 // compare method of 1st CompareID object executed 2 // compare method of 1st CompareID object executed 3 // compare method of 1st CompareID object executed 0 // compare method of 2nd CompareID object executed 1 // compare method of 2nd CompareID object executed 2 // compare method of 2nd CompareID object executed 3 // compare method of 2nd CompareID object executed 4 // compare method of 1st CompareID object executed 5 // compare method of 1st CompareID object executed 6 // compare method of 1st CompareID object executed 7 // compare method of 1st CompareID object executed 8 // compare method of 1st CompareID object executed 4 // compare method of 2nd CompareID object executed 5 // compare method of 2nd CompareID object executed 6 // compare method of 2nd CompareID object executed 7 // compare method of 2nd CompareID object executed 8 // compare method of 2nd CompareID object executed
编辑:首先,您要向第一个
TreeSet
添加元素(因此compare
第一个Comparator
连续多次调用),然后您将元素添加到第二个TreeSet
(因此compare
第二个Comparator
连续多次调用),然后你调用treeSetA.containsAll(treeSetB)
,它导致第一个Comparator
被连续多次调用,最后你调用treeSetB.containsAll(treeSetA)
,这导致第二个Comparator
被多次调用一排。I'm not sure what you are finding strange. You have two TreeSet instances, each having its own
CompareID
instance serving asComparator
, and eachCompareID
instance maintains it's own counter.Therefore it's not a surprise to see each counter values (0,1,2,etc...) appearing twice. As for the order of appearance of the counter values, that depends on the internal implementation of
TreeSet
.As for
Equal set of tree // is that correct output ?
Yes, both sets contain the same elements. The order doesn't matter. To clarify - the methods
contains
andcontainsAll
ofTreeSet
consider an elementx
to be contained in theTreeSet
ifcompare(x,y)==0
for some elementy
of theTreeSet
, wherecompare
is thecompare
method of the suppliedComparator
. Therefore, in your example, only theid
property determines if two elements are equal.The following scenario explains the output :
0 // compare method of 1st CompareID object executed 1 // compare method of 1st CompareID object executed 2 // compare method of 1st CompareID object executed 3 // compare method of 1st CompareID object executed 0 // compare method of 2nd CompareID object executed 1 // compare method of 2nd CompareID object executed 2 // compare method of 2nd CompareID object executed 3 // compare method of 2nd CompareID object executed 4 // compare method of 1st CompareID object executed 5 // compare method of 1st CompareID object executed 6 // compare method of 1st CompareID object executed 7 // compare method of 1st CompareID object executed 8 // compare method of 1st CompareID object executed 4 // compare method of 2nd CompareID object executed 5 // compare method of 2nd CompareID object executed 6 // compare method of 2nd CompareID object executed 7 // compare method of 2nd CompareID object executed 8 // compare method of 2nd CompareID object executed
EDIT : First you are adding elements to the first
TreeSet
(hencecompare
of the firstComparator
is called several times in a row), then you are adding elements to the secondTreeSet
(hencecompare
of the secondComparator
is called several times in a row), then you calltreeSetA.containsAll(treeSetB)
which causescompare
of the firstComparator
to be called several times in a row, and finally you calltreeSetB.containsAll(treeSetA)
which causescompare
of the secondComparator
to be called several times in a row.
相关问答
更多-
你不能像这样引用一个尚未完成创建的对象,但听起来你想在这里使用一个getter: var pizzaOrder = { id: "pizza", counter: 0, get sentence() { return this.id + this.counter; } }; pizzaOrder.counter++; console.log(pizzaOrder.sentence); You can't reference an object that hasn' ...
-
Java线程安全计数器(Java Thread Safe Counter)[2023-12-31]
您的代码在特定测试中运行良好。 这并不意味着它总能正常工作。 尝试迭代很多次,你可能会开始看到异常。 您的测试有点像,如果您测试过一座桥可以通过搭载一辆车在桥上支撑20辆卡车。 它没有展示任何东西。 并且使用测试证明代码是线程安全的几乎是不可能的。 只有仔细阅读和理解所有潜在的问题才能保证。 一个非线程安全的程序可能会工作多年,并突然有一个bug。 为了使您的计数器安全,请使用AtomicInteger。 编辑: 另外,正如@SpringRush所说,你不是在线程之间共享一个计数器。 每个线程都有自己的计数 ... -
你传递的是counter的值 ,而不是变量本身。 您的递归调用不能修改外部调用中counter的值。 Java是通过价值传递的。 一个可能的解决方案是做 counter = foo(num, counter); 在递归调用中。 或者,也可以return foo(num, counter) ,因为除了返回它之外,你不会对任何事情做任何事情。 You're passing the value of counter, not the variable itself. Your recursive call c ...
-
p > 4背后的逻辑是什么? 如果你确定知道有4人需要输入,你可以使用一个简单的for循环。 for (int i = 0; i < 4; i++) { System.out.print("Enter the best time available for person " + (i+1) + "."); p=kbd.nextInt(); if (p == 1) morning++; else if (p == 2) noon++; else if (p == 3) ...
-
Collections.sort被实现为mergesort。 从源头上看,所有比较条件都是>0或<=0 ,偶然地将负面情况视为与情况相同。 不同的排序实现可能会失败。 Per jbellis的评论,“ 这不完全是”只是运气“,尽管 - Collections.sort保证是”稳定的“,这意味着相同的元素必须在排序后处于相同的相对顺序。我不确定这是不可能的拿出一个稳定的排序实现,但是这个比较失败了,但我想不出我的头顶。 “ private static void mergeSort(Object[] src, ...
-
使用比较器的意外输出(Unexpected output using comparator)[2023-07-16]
你测试i1i2,而是i2> i1。 将您的比较器更改为 return i1 < i2 ? -1 : (i1 > i2) ? 1 : 0; 添加 为了对另外两个答案做出新的贡献,这两个答案说了同样的话,并且让我大吃一惊,大多数包装类都内置了compareTo()方法,可以让您免于任何想法。 比如你的比较器可以调用Integer.compareTo(),即 return i1.compareTo(i2); You test for i1 < i2, and, i ... -
像这样的东西应该工作: for ($i = 3; $i <= 14; ++$i) { if (empty(${"f$i"})) { ${"f$i"} = ${"bla$i"}(); } } 最重要的是,您可以使用${"f$i"}来查找变量变量。 Something like this should work: for ($i = 3; $i <= 14; ++$i) { if (empty(${"f$i"})) { ${"f$i"} = ${"bla ...
-
要使某个类变量而不是实例变量,我们需要将其设置为static 。 有关static变量的更多信息以及它们与常规变量的不同之处: https : //en.wikipedia.org/wiki/Static_variable TLDR:你的第一个解决方案是正确的,尽管我认为它应该读取private static int counter = 0; To make something a class variable rather than an instance variable, we need to mak ...
-
我猜你试图找到这两个数组的匹配数,但是相等元素的相对索引应该是不同的,这样我们就可以将它们添加到伪计数器机制中。 所以看看这个方法并尝试一下: public int psuedoMatch(int[] guess) { int psuedoCount = 0; if (lotteryNumbers.length != guess.length) return 0; int size = lotteryNumbers.length; // Determin ...
-
我不确定你在找什么奇怪的东西。 您有两个 TreeSet实例,每个实例都有自己的CompareID实例作为Comparator ,每个CompareID实例都维护它自己的计数器。 因此,看到每个计数器值(0,1,2等)出现两次并不奇怪。 至于计数器值的出现顺序,取决于TreeSet的内部实现。 至于 等于一组树是否是正确的输出? 是的,两个集都包含相同的元素。 订单无关紧要。 为了澄清 - 如果对于TreeSet的某个元素y , compare(x,y)==0则contains和containsAll Tr ...