gpt4 book ai didi

java - 如果条件评估不正确

转载 作者:行者123 更新时间:2023-12-01 22:18:42 25 4
gpt4 key购买 nike

以下 Java 测试代码发生了一些奇怪的情况。这是使用 Spring 编写的。

ResourceType resourceType1 = new ResourceType(RESOURCE_TYPE_NAME);
resourceType1 = resourceTypeService.addResourceType(resourceType1);

ResourceType resourceType2 = new ResourceType(RESOURCE_TYPE_NAME_2);
resourceType2 = resourceTypeService.addResourceType(resourceType2);

List<ResourceType> all = resourceTypeService.getAll();
List<ResourceType> myObjects = new ArrayList<>();
for (ResourceType resourceType : all) {
if(resourceType.getId() == resourceType1.getId()
|| resourceType.getId() == resourceType2.getId()){
myObjects.add(resourceType);
}
}

assertTrue(myObjects.get(0).getId() == resourceType1.getId());

当我运行它时,它会在 .get(0) 调用上返回 IndexOutOfBounds

这很奇怪,因为 resourceType1resourceType2 的 ID 位于 all 列表中。

这是调试代码的结果:

插入后的资源类型1:

resourceType1 = {ResourceType@5986} 
id = 179

插入后的资源类型1:

resourceType2 = {ResourceType@5987} 
id = 180

所有在getAll()之后

all = {ArrayList@5988}  size = 37
..........
35 = {ResourceType@5986}
id = 179
36 = {ResourceType@5987}
id = 180

这是调试数据。

为什么if条件不满足?

更新#1

错误堆栈跟踪:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at com.test.ihbs.service.ResourceTypeServiceTest.test_getAll(xxx.java:107)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:64)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:106)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

最佳答案

正如我在评论中所说,您无法使用 == 比较器来比较 Long。它仅适用于 (-128, 127) 之间的值,并且仅当它们不是使用 new 关键字创建时才有效。它之所以有效,是因为这些值由 JVM 缓存,因此比较引用返回 true。

herehere .

1)

Integer i = 100;
Integer p = 100;
if (i == p) System.out.println("i and p are the same.");
if(i.equals(p)) System.out.println("i and p contain the same value.");

输出为:

i and p are the same.
i and p contain the same value.

2)

Integer i = 200;
Integer p = 200;
if (i == p) System.out.println("i and p are the same.");
if(i.equals(p)) System.out.println("i and p contain the same value.");

输出为:

i and p contain the same value.

3)

Integer i = new Integer (100);
Integer p = new Integer(100);
if(i==p) System.out.println(“i and p are the same object”);
if(i.equals(p)) System.out.println(“ i and p contain the same value”);

在这种情况下,输出只有:

i and p contain the same value

关于java - 如果条件评估不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30427899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com