我正在尝试修改从方法返回为Object集合的特定对象。现在还有很多其他对象也被添加到这个集合中。因此找到特定的对象变得困难。我尝试使用“instanceof”来解决它,但我得到以下异常:
java.lang.ClassCastException: com.sheesoft.MockBean cannot be cast to com.sheesoft.TestBean
at com.sheesoft.Main.main(Main.java:30)
我已经能够使用某些测试类重现相同的异常。请让我知道我哪里出错了。
主:
package com.sheesoft;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Collection<Object> beanCollection = new ArrayList<Object>();
TestBean tb1 = new TestBean();
MockBean mb1 = new MockBean();
beanCollection.add(tb1);
beanCollection.add(mb1);
try{
Iterator<Object> itr = beanCollection.iterator();
while(itr.hasNext()){
Object object = itr.next();
if(object instanceof TestBean){
TestBean tb2 = (TestBean) itr.next();
tb2 = modifyTestBody(tb2);
System.out.println(tb2.getBody());
}
if(object instanceof MockBean){
MockBean mb2 = (MockBean) itr.next();
System.out.println(mb2.getMockPayload());
}
}
}catch(Exception e){
e.printStackTrace();
}
}
private static TestBean modifyTestBody(TestBean tb) {
tb.setBody("NewBody");
return tb;
}
}
Bean1:
package com.sheesoft;
public class TestBean {
private String header;
private String body;
public TestBean(){
setHeader("header");
setBody("body");
}
public String getHeader(){
return header;
}
public String getBody(){
return body;
}
public void setHeader(String header){
this.header=header;
}
public void setBody(String body){
this.body = body;
}
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("header = "+header+"\n");
sb.append("body = "+body+"\n");
String returns = sb.toString();
return returns;
}
}
Bean 2:
package com.sheesoft;
public class MockBean {
private String mockHeader;
private String mockBody;
private String mockPayload;
public MockBean(){
setMockHeader("mockHeader");
setMockBody("mockBody");
setMockPayload("mockPayload");
}
public String getMockHeader(){
return mockHeader;
}
public String getMockBody(){
return mockBody;
}
public String getMockPayload(){
return mockPayload;
}
public void setMockHeader(String header){
this.mockHeader=header;
}
public void setMockBody(String body){
this.mockBody = body;
}
public void setMockPayload(String payload) {
this.mockPayload = payload;
}
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("mockHeader = "+mockHeader+"\n");
sb.append("mockBody = "+mockBody+"\n");
sb.append("mockPayload"+mockPayload+"\n");
String returns = sb.toString();
return returns;
}
}
答案 0 :(得分:1)
在
System.out.println(itr.next().toString());
if(itr.next() instanceof TestBean){
TestBean tb2 = (TestBean) itr.next();
tb2=modifyTestBody(tb2);
System.out.println(tb2.getBody());
}
if(itr.next() instanceof MockBean){
MockBean mb2 = (MockBean) itr.next();
System.out.println(mb2.getMockPayload());
}
您将分别致电itr.next()
三次。当您只检查是否有一个元素可用时,会移动三个不同的元素。相反,将itr.next()
存储到变量中并对该变量执行所有操作。
答案 1 :(得分:1)
在您的代码中:
System.out.println(itr.next().toString());
if(itr.next() instanceof TestBean){
TestBean tb2 = (TestBean) itr.next();
tb2=modifyTestBody(tb2);
System.out.println(tb2.getBody());
您已拨打itr.next()
三次。所以当你写第三个itr.next()
时你已经跳到了第三个元素。因此,在每个循环中,您将迭代为三个元素。因此,Iterator
过早耗尽并抛出java.util.NoSuchElementException
修改强> 请改用此代码:
while(itr.hasNext()){
Object obj = itr.next();
System.out.println(obj.toString());
if(obj instanceof TestBean){
TestBean tb2 = (TestBean) obj;
tb2=modifyTestBody(tb2);
System.out.println(tb2.getBody());
}
if(obj instanceof MockBean){
MockBean mb2 = (MockBean) obj;
System.out.println(mb2.getMockPayload());
}
}
}catch(Exception e){}
答案 2 :(得分:0)
在您拥有的代码中
System.out.println(itr.next().toString());
if(itr.next() instanceof TestBean){
TestBean tb2 = (TestBean) itr.next();
....
问题是,每次调用itr.next()时,都会从集合中获取下一个对象,因此在这种情况下,您不需要一个对象,而是多个对象。
您应该将此代码更改为:
Object object = itr.next();
System.out.println(object.toString());
if(object instanceof TestBean){
TestBean tb2 = (TestBean) object;
....