我有一个形状,圆形,正方形和矩形的ArrayList。我想请用户输入r
,c
或s
从数组列表中删除所有对象。
我尝试使用ArrayList.remove
,但无法删除该类的所有对象。
try
{
Scanner reader = new Scanner(new File("shapes.txt"));
while(reader.hasNextLine())
{
try
{
String line = reader.nextLine();
String[] array = line.split(",", -1);
if(array[0].equalsIgnoreCase("c"))
{
radius = Double.parseDouble(array[1]);
Shape cir = new Circle(radius);
shapeArray.add(cir);
if(array.length > 2)
{
throw new ExtraAttributeException("Object " + counter + " has to many attributes."
+ " Throwing the last attribute.");
}
}
if(array[0].equalsIgnoreCase("r"))
{
length = Double.parseDouble(array[1]);
width = Double.parseDouble(array[2]);
Shape rec = new Rectangle(length, width);
shapeArray.add(rec);
if(array.length > 3)
{
throw new ExtraAttributeException("Object " + counter + " has to many attributes."
+ " Throwing the last attribute.");
}
}
if(array[0].equalsIgnoreCase("s"))
{
length = Double.parseDouble(array[1]);
Shape sq = new Square(length);
shapeArray.add(sq);
if(array.length > 2)
{
throw new ExtraAttributeException("Object " + counter + " has to many attributes."
+ " Throwing the last attribute.");
}
}
}
catch(ExtraAttributeException e)
{
System.out.println(e.getMessage());
}
++counter;
}
}
catch(FileNotFoundException e)
{
System.out.println("File Not Found");
}
System.out.println("Which type of shape do you want to remove?");
Scanner input = new Scanner(System.in);
String remove = input.nextLine();