简体   繁体   中英

toString() does not override

The purpose of the program is to find the number of areas in a "maze" that are separated from eachother. Then it should print the number of separated areas and the size and coordinates of the top left corner of each of the areas in descending order. If the areas have the same size, then the on that starts higher or more to the left should be printed first. The input is the size of the matrix width x height and then series of spaces and " * ", with " * " representing an unpassable position.

It seems to be working fine up until the printing. Even though I've overridden toString(), it seems to be printing as though i've coded 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());

        }
    }
}

in findAreas() you are adding int values to your array:

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

and not the Area objects. Like already said, use a typed List<Area>, then this would have been detetced by the compiler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM