Java 无法将矩形转换为圆形?

Java 无法将矩形转换为圆形?,java,object,casting,dynamic-arrays,Java,Object,Casting,Dynamic Arrays,因此,我试图编写一个方法,返回具有最大面积的对象的索引。这是我目前的方法 private static int findPositionLargestObject(ArrayList < GeometricObject > geoList) { int maxIndexC = 0; int maxIndexR = 0; for (GeometricObject o: geoList) { for (int i = 1; i < geoLi

因此,我试图编写一个方法,返回具有最大面积的对象的索引。这是我目前的方法

private static int findPositionLargestObject(ArrayList < GeometricObject > geoList) {
    int maxIndexC = 0;
    int maxIndexR = 0;
    for (GeometricObject o: geoList) {
        for (int i = 1; i < geoList.size(); i++) {
            if (o instanceof Rectangle) {
                if (((Rectangle) geoList.get(i)).getArea() > ((Rectangle) geoList.get(maxIndexR)).getArea()) {
                    maxIndexR = i;
                }
            }
            if (o instanceof Circle) {
                if (((Circle) geoList.get(i)).getArea() > ((Circle) geoList.get(maxIndexC)).getArea()) {
                    maxIndexC = i;
                }
            }

        }
    }
    if (maxIndexC > maxIndexR) {
        return maxIndexC;
    } else return maxIndexR;
}
私有静态int findPositionLargestObject(ArrayListgeoList){
int maxIndexC=0;
int-maxIndexR=0;
对于(几何对象:地理列表){
对于(int i=1;i((矩形)geoList.get(maxIndexR)).getArea()){
maxIndexR=i;
}
}
if(o圆的实例){
if(((圆圈)geoList.get(i)).getArea()>((圆圈)geoList.get(maxIndexC)).getArea()){
maxIndexC=i;
}
}
}
}
如果(maxIndexC>maxIndexR){
返回maxIndexC;
}否则返回maxIndexR;
}
但是,当我运行此方法时,收到错误消息“矩形不能转换为圆形”。我之所以使用两个不同的if语句,是因为对于circle和rectangle对象,getArea方法是不同的。知道我为什么会收到这封信吗,谢谢

这是我的公开课

public class hw2redo 
{
     public static void main(String[] args) throws FileNotFoundException {

          GeometricObject g = null;
          File diskFile = new File("file.txt");
          Scanner diskScanner = new Scanner(diskFile);
          ArrayList<GeometricObject> list = new ArrayList<GeometricObject>();
          while(diskScanner.hasNext()){
              String geolist = diskScanner.nextLine();
              g = recreateObject(geolist);

              list.add(g);

          }
          diskScanner.close();
         /* while (diskScanner.hasNext()) {
              String list = diskScanner.nextLine();
              g = recreateObject(list);
          }
          diskScanner.close();*/
          showObjects(list);
          findPositionLargestObject(list);
       }





    private static GeometricObject recreateObject(String data) {

          String[] list = data.split(",");
          String geoObject = list[0];

          if (geoObject.equals("Circle")) {
             String color = list[1];
             boolean filled = Boolean.valueOf(list[2]); 
             double radius = Double.valueOf(list[3]);
             return new Circle(radius, color, filled);
          }

          if (geoObject.equals("Rectangle")) {
             String color = list[1];
             boolean filled = Boolean.valueOf(list[2]);
             double height = Double.valueOf(list[3]);
             double width = Double.valueOf(list[4]);
             return new Rectangle(width, height, color, filled);
          }
        return null;


       }

    private static void showObjects(ArrayList<GeometricObject> list) {

         for(GeometricObject o : list) {

             if ( o instanceof Circle)
             {
             System.out.println(o);
             ((Circle) o).printCircle();
             System.out.println("");
             }
             if ( o instanceof Rectangle)
             {
             System.out.println(o);
             ((Rectangle) o).printRectangle();
             System.out.println("");
             }
         }
    }
      private static int findPositionLargestObject(ArrayList<GeometricObject> geoList) {

            int maxIndexC = 0;
            int maxIndexR = 0;
            for(GeometricObject o : geoList)
            {
            for (int i = 1; i < geoList.size(); i++) {
                if ( o instanceof Rectangle)
                {
                if (((Rectangle) geoList.get(i)).getArea() > ((Rectangle) geoList.get(maxIndexR)).getArea()) {
                    maxIndexR = i;
                }
                }
                if ( o instanceof Circle)
                {
                if (((Circle) geoList.get(i)).getArea() > ((Circle) geoList.get(maxIndexC)).getArea()) {
                        maxIndexC = i;
                }
                }

            }
        }
           if (maxIndexC > maxIndexR)
           {
               return maxIndexC;
           }
           else
               return maxIndexR;
     }
}
公共类hw2redo
{
公共静态void main(字符串[]args)引发FileNotFoundException{
GeometricObject g=null;
File diskFile=新文件(“File.txt”);
扫描仪diskScanner=新扫描仪(磁盘文件);
ArrayList=新建ArrayList();
while(diskScanner.hasNext()){
字符串geolist=diskScanner.nextLine();
g=重新创建对象(地理列表);
增加(g);
}
diskScanner.close();
/*while(diskScanner.hasNext()){
String list=diskScanner.nextLine();
g=重新创建对象(列表);
}
diskScanner.close()*/
显示对象(列表);
findPositionLargestObject(列表);
}
私有静态GeometricObject重新创建对象(字符串数据){
String[]list=data.split(“,”);
字符串geoObject=list[0];
if(geoObject.equals(“圆”)){
字符串颜色=列表[1];
boolean filled=boolean.valueOf(列表[2]);
双半径=double.valueOf(列表[3]);
返回新圆(半径、颜色、填充);
}
if(geobject.equals(“矩形”)){
字符串颜色=列表[1];
boolean filled=boolean.valueOf(列表[2]);
双倍高度=双倍.valueOf(列表[3]);
double width=double.valueOf(列表[4]);
返回新矩形(宽度、高度、颜色、填充);
}
返回null;
}
私有静态void showObjects(ArrayList列表){
对于(几何对象:列表){
if(o圆的实例)
{
系统输出打印ln(o);
((圈)o).printCircle();
System.out.println(“”);
}
if(矩形的o实例)
{
系统输出打印ln(o);
((矩形)o.printRectangle();
System.out.println(“”);
}
}
}
私有静态int findPositionLargestObject(ArrayList地理列表){
int maxIndexC=0;
int-maxIndexR=0;
for(几何对象o:geoList)
{
对于(int i=1;i((矩形)geoList.get(maxIndexR)).getArea()){
maxIndexR=i;
}
}
if(o圆的实例)
{
if(((圆圈)geoList.get(i)).getArea()>((圆圈)geoList.get(maxIndexC)).getArea()){
maxIndexC=i;
}
}
}
}
如果(maxIndexC>maxIndexR)
{
返回maxIndexC;
}
其他的
返回maxIndexR;
}
}

您正在检查
o
是否为
矩形
/
圆形
的实例,但正在强制转换
地理列表。请改为将(i)
转换为相应的类型。事实上,
o
在循环中除了
instanceof
检查之外,没有在任何地方使用。

您正在检查
o
是否是
矩形
/
圆形
的实例,而是将
地理列表强制转换为相应的类型。事实上,
o
除了
instanceof
检查之外,在循环中的任何地方都不会使用。

对于每个对象,都要在数组中循环两次

for (GeometricObject o: geoList) { // for each GeometricObject in geoList, 
    for (int i = 1; i < geoList.size(); i++) { // loop through each object in geoList.
for(GeometricObject o:geolost){//对于geolost中的每个GeometricObject,
对于(inti=1;i

删除for-each循环或常规for循环。

对于每个对象,在数组中循环两次

for (GeometricObject o: geoList) { // for each GeometricObject in geoList, 
    for (int i = 1; i < geoList.size(); i++) { // loop through each object in geoList.
for(GeometricObject o:geolost){//对于geolost中的每个GeometricObject,
对于(inti=1;i

删除for-each循环或常规for循环。

根据我的理解,您将有两个类,矩形和圆形,它们是从一个称为几何体的公共类扩展而来的。这里需要注意的一点是,矩形和圆形是同级的。也就是说,矩形永远不是圆形,反之亦然。直接该语句的结果是,不能将矩形实例视为圆形实例,反之亦然

在方法
findPositionLargestObject
中,您将
maxIndexC
maxIndexR
初始化为0。虽然这听起来无害,但接下来您可以执行以下操作:

            if ( o instanceof Rectangle)
            {
            if (((Rectangle) geoList.get(i)).getArea() > ((Rectangle) geoList.get(maxIndexR)).getArea()) {
//什么
((Rectangle) geoList.get(maxIndexR))