java.lang.UnsupportedOperationException:
在这个程序中,我将属性存储到arraylistjava.lang.UnsupportedOperationException:,java,Java,在这个程序中,我将属性存储到arraylist public static void main(String [] args) throws Exception{ Property property1 = new Property(24,"Boston Australia",3,45,67); Property property2 = new Property(24,"Boston Malvern",3,45,67); Property property3 =
public static void main(String [] args) throws Exception{
Property property1 = new Property(24,"Boston Australia",3,45,67);
Property property2 = new Property(24,"Boston Malvern",3,45,67);
Property property3 = new Property(24,"Boston Caulfield",3,45,67);
Property property4 = new Property(24,"Boston Caulfield",3,45,67);
Property property5 = new Property(24,"Boston Caulfield",3,45,67);
SimplePropertyRepositoryImpl sl;
sl = new SimplePropertyRepositoryImpl();
sl.addProperty(property1);
sl.addProperty(property2);
sl.addProperty(property3);
sl.addProperty(property4);
sl.addProperty(property5);
}
public class SimplePropertyRepositoryImpl implements PropertyRepository{
ArrayList<Property> properties;
public SimplePropertyRepositoryImpl(){
properties = new ArrayList<>();
}
@Override
public void addProperty(Property property) throws Exception {
properties.add(property);
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
SimplePropertyRepositoryImpl有一个addProperty方法,我在其中将property对象添加到arraylist
public static void main(String [] args) throws Exception{
Property property1 = new Property(24,"Boston Australia",3,45,67);
Property property2 = new Property(24,"Boston Malvern",3,45,67);
Property property3 = new Property(24,"Boston Caulfield",3,45,67);
Property property4 = new Property(24,"Boston Caulfield",3,45,67);
Property property5 = new Property(24,"Boston Caulfield",3,45,67);
SimplePropertyRepositoryImpl sl;
sl = new SimplePropertyRepositoryImpl();
sl.addProperty(property1);
sl.addProperty(property2);
sl.addProperty(property3);
sl.addProperty(property4);
sl.addProperty(property5);
}
public class SimplePropertyRepositoryImpl implements PropertyRepository{
ArrayList<Property> properties;
public SimplePropertyRepositoryImpl(){
properties = new ArrayList<>();
}
@Override
public void addProperty(Property property) throws Exception {
properties.add(property);
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
请帮助我理解,当我没有使用任何不可修改的集合时,为什么会出现此异常。在
addProperty
中删除无条件的抛出。大概
public void addProperty(Property property) throws Exception {
properties.add(property);
// throw new UnsupportedOperationException("Not supported yet.");
}
由于上述抛出
未被注释掉,因此抛出
的UnsupportedOperationException
抛出新的UnsupportedOperationException(“尚不受支持”)代码>-代码完全按照要求执行;异常跟踪准确地指出了它被写入的位置-。在SimplePropertyRepositoryImpl.addProperty..(第29行)