任何比较条件都返回错误值



我在尝试比较两个int时遇到了非常奇怪的行为,但首先要做的是。这是我在类中的方法:

public class CollectionsTutorial {
private Collection<String> collection = null;

public CollectionsTutorial() {
this.collection = new ArrayList<String>();

for(int i=0;i<100000;i++) {
collection.add("item_"+((i+1)%10000));
}
}

public Set<String> getSet(){
HashSet<String> set = new HashSet<String>(10000);
for(String item: this.collection) {
if (!set.contains(item)) {
set.add(item);
}
}
return set;
}
public TreeSet<String> getEvery3ElementAsSortedSet(){
TreeSet<String> tree = new TreeSet<String>();
int counter = 0;
for(String item : this.collection) {
if (counter%2==0) {
tree.add(item);
counter = -1;
}
counter++;
}
return tree;
}
}

整个类在这里并不重要——它只包含ArrayList<String>。我需要返回树中的每三个元素,一切都很顺利,直到可以比较当前计数器值为止。每次都是真的。我确信它在测试时是有用的,它将集合的每一个元素都添加到树中。我尝试过使用compareTo()equalsTo()valueOf()intValue(),但没有任何帮助。

编辑。我已经将代码更改为类视图——也许是其他原因导致了错误?

这是我试图通过的测试

public class CollectionsTutorialTest {
@Test
public void GetEvery3ElementTest() {
CollectionsTutorial testObj = new CollectionsTutorial();
TreeSet<String> tree = new TreeSet<String>();
tree.addAll(testObj.getSet() );
assertEquals(false, tree.contains("item_2"));
}

}

"我需要返回树中的每三个元素"这种描述对于模CCD_ 6运算符来说是非常重要的。

如果您想在每三次迭代中输入一个If条件,则可以使用以下条件进行检查:counter % 3 == 0。你根本不需要减去任何东西。

%是如何工作的

简单地说:模运算符返回除法的结果。

示例:

5 / 3 = 1 with remain 2
5 % 3 = 2 
9 / 3 = 3 with remain 0 
9 % 3 = 0

注意:

if (counter % 2 == 0) {
tree.add(item);
counter = -1;
}
counter++;

您使用counter % 2 == 0。这将返回第二个元素的true。然后设置counter = -1,因为您打算获得每3个元素。然而,这是行不通的。

原因如下:

System.out.println(0 % 1);        // 0 % 1 = 0
System.out.println(0 % 2);        // 0 % 2 = 0
System.out.println(0 % 3);        // 0 % 3 = 0
System.out.println(0 % 4);        // 0 % 4 = 0

正如您所看到的,每当counter达到值0时,if条件将导致true,而不管除数是多少。这就是您使用% 3的原因。

列表由以下人员创建:

for(int i=0;i<100_000;i++) {
collection.add("item_"+((i+1)%10_000));
}

导致:

collection[0] = item_1
collection[1] = item_2
collection[2] = item_3
collection[3] = item_4
...
collection[9_998] = item_9999
collection[9_999] = item_0         // (9_999+1) % 10_000 is 0
collection[10_000] = item_1        // (10_000+1) % 10_000 is 1
collection[10_001] = item_2
collection[10_002] = item_3     
collection[10_003] = item_4    
...
collection[19_998] = item_9999      // (19_998+1) % 10_000 is 9_999
collection[19_999] = item_0         // (19_999+1) % 10_000 is 0
collection[20_000] = item_1
collection[20_001] = item_2
collection[20_002] = item_3
collection[20_003] = item_4
...

以上不是代码

现在注意,20_001 % 30,因此第三个item_2将被选取;CCD_ 18和CCD_。因此,实际上,该项被添加3次到(由于它是Set,因此只有一个实例被保留(。最后,结果中将显示每个的一个版本。类似于counter%3 == 1counter%3 == 2(基本上是有问题发布的第一个代码版本。(

教训:检查item_2是否存在,不检查条件"em"是否存在;每次都返回true"。


更好的测试:

...
int index = 0;  // DEBUG
for(String item : this.collection) {
if (counter%2==0) {
System.out.println(index);  // DEBUG
tree.add(item);
counter = -1;
}
index += 1;  // DEBUG
...

上面的不是完整的代码,只是展示了测试的想法


奖励:
Set不会保存重复项("不包含重复元素的集合">(,如下片段:

if (!set.contains(item)) {
set.add(item);
}

if是不必要的——add()已经检查过了

最新更新