toString()不会覆盖

时间:2015-09-22 18:19:01

标签: java

该计划的目的是找到彼此分开的“迷宫”中的区域数量。然后它应该按降序打印每个区域左上角的分隔区域的数量和大小和坐标。如果区域具有相同的大小,则应首先打印从左侧开始更高或更高的开启。输入是矩阵宽度x高度的大小,然后是一系列空格和“*”,“*”表示不可通过的位置。

在打印之前似乎工作正常。即使我已经覆盖toString(),它似乎是打印好像我已经编码 System.out.println(areas.size);

import java.io.*;
import java.util.*;

class Area implements Comparable {
    int size;
    int x,y;    

    public Area(int Size,int col,int row){
        size=Size;
        x=col;
        y=row;
    }
    public String toString(){
        return "Size: " + size + "\n" + "Row:" + y + "\n" + "Col:" + x + "\n";
    }
    public int compareTo(Object o1){
        if(this.size>((Area) o1).size)
            return -1;
        else if(this.size<((Area) o1).size)
            return 1;
        else if(this.y<((Area)o1).y)
            return -1;
        else if(this.y>((Area)o1).y)
            return 1;
        else if(this.x<((Area)o1).x)
            return -1;
        else if(this.x>((Area)o1).x)
            return 1;
        else return 0;
    }
}


public class ConnectedAreasInMatrix {
    static List areas = new ArrayList();
    static int height,width,br;
    static char[][] a;
    static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args) {
        matrixIni();
        findAreas();
        print();

    }

    //gets the use input and forms the matrix
    private static void matrixIni() {
        try{
            System.out.println("Enter the size of the matrix:"+"\n"+"Width:");
            width = Integer.parseInt(input.readLine());
            System.out.println("Height:");
            height = Integer.parseInt(input.readLine());
        }
        catch(IOException e){
            System.out.print(e);
        }
        catch(NumberFormatException f){
            System.out.println("Invalid number.");
        }
        System.out.println("Enter the matrix 1 row at a time:");
        a= new char[height][width];

        int i=0;
        String temp = new String();
        do{
            try{
                temp=input.readLine();
                if(temp.length()==width){
                    a[i]=temp.toCharArray();
                    i++;
                }
                else
                    System.out.println("Invalid row length.");
            }
            catch(IOException e){
                System.out.println(e);
            }
        }
        while(i!=height);       

    }

    //finds an uncounted area.
    static void findAreas() {
        Area tempArea;
        br=0;
        for(int i=0;i<width;i++){
            for(int k=0;k<height;k++){
                if(a[i][k]==' '){
                    tempArea = new Area(1,i,k);
                    areas.add(areaBuilder(i,k,tempArea.size));
                    br++;
                }
            }

        }

    }

    //counts all the connected positions
    private static int areaBuilder(int i, int k, int tempAreaSize) {
        a[i][k]='*';

        if(k+1<width)
            if(a[i][k+1]==' ') tempAreaSize = areaBuilder(i,k+1,tempAreaSize+1);

        if(i+1<height)
            if(a[i+1][k]==' ') tempAreaSize = areaBuilder(i+1,k,tempAreaSize+1);

        if(k-1>=0)
            if(a[i][k-1]==' ') tempAreaSize = areaBuilder(i,k-1,tempAreaSize+1);

        if(i-1>=0)
            if(a[i-1][k]==' ') tempAreaSize = areaBuilder(i-1,k,tempAreaSize+1);
        return tempAreaSize;
    }

    //prints the result. Expected output is the number of connected areas and after it the size 
    //and coordinates of the top left corner of each area.

    static void print(){
        Collections.sort(areas);
        System.out.println("The connected areas are "+br+"\n");
        Iterator  itr = areas.iterator();

        while(itr.hasNext()){
            System.out.println(itr.next());

        }
    }
}

1 个答案:

答案 0 :(得分:1)

在findAreas()中,您将int值添加到数组中:

areas.add(areaBuilder(i,k,tempArea.size));

而不是Area对象。 就像已经说过的那样,使用一个类型化的List&lt; Area&gt;,然后编译器会对此进行检测。