下面的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());当我运行它时,它会在IndexOutOfBounds调用上返回.get(0)。
这很奇怪,因为resourceType1和resourceType2在all列表中有is。
这是调试代码的结果:
插入后的资源类型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)发布于 2015-05-24 20:55:18
正如我在备注中所说的,使用==比较器不能进行很长时间的比较。它只适用于(-128,127)之间的值,并且只有当它们不使用new关键字创建时才有效。它之所以有效,是因为JVM缓存了这些值,因此比较引用返回true。
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发布于 2016-07-28 06:35:41
"==“总是比较值的内存位置或对象引用。方法总是比较values.but =,也间接使用"==“运算符来比较值。整数使用整数缓存存储从-128到+ 127之间的值,如果使用==操作符检查-128到127之间的任何值,则返回true。对于这些值以外的值,则返回false。
参考链接获取更多信息。
https://stackoverflow.com/questions/30427899
复制相似问题