使用toString打印结果数组

时间:2018-07-08 10:17:25

标签: java

我试图添加toString来打印结果数组...但是我遇到了其他用户提示无法为我工作的异常...问题在哪里....请发送给我在写处写代码 项目名称:天际线 gitHub上的代码:https://github.com/mouhyi/Algorithms/blob/master/SkyLine.java

我尝试了这个,但遇到了以下例外情况: 图片 :   https://i.stack.imgur.com/YLdBm.png   https://i.stack.imgur.com/r311t.png

但是不能工作

我的代码:

import java.util.ArrayList;
import java.util.Arrays;
public class sakhteman

{

public static ArrayList<Point> findsakhteman(Building[] sakhtemanha){
    int n = sakhtemanha.length;
    if(n == 1){
        ArrayList<Point> sl = new ArrayList<Point>();
        sl.add(new Point(sakhtemanha[0].left, sakhtemanha[0].height));
        sl.add(new Point(sakhtemanha[0].right, 0));
        return sl;
    }
    ArrayList<Point> sl1 = findsakhteman(Arrays.copyOfRange(sakhtemanha, 0,n/2 ));
    ArrayList<Point> sl2 = findsakhteman(Arrays.copyOfRange(sakhtemanha, n/2+1, n ));
    return merge(sl1, sl2);
}
public static ArrayList<Point> merge(ArrayList<Point> sl1, ArrayList<Point> sl2){
    ArrayList<Point> sakhteman = new ArrayList<Point>();
    int curH1=0, curH2=0, curX=0;
    while(!sl1.isEmpty() && !sl2.isEmpty()){
        if( sl1.get(sl1.size()-1).x < sl2.get(sl1.size()-1).x ){
            curX = sl1.get(sl1.size()-1).x;
            curH1 = sl1.get(sl1.size()-1).y;
            sl1.remove(sl1.size()-1);
            sakhteman.add(new Point(curX, Math.max(curH1, curH2)));
        }else{
            curX = sl2.get(sl2.size()-1).x;
            curH1 = sl2.get(sl2.size()-1).y;
            sl2.remove(sl1.size()-1);
            sakhteman.add(new Point(curX, Math.max(curH1, curH2)));
        }
    }
    if(sl1.isEmpty()){
        sakhteman.addAll(sl2);
    }else if(sl2.isEmpty()){
        sakhteman.addAll(sl1);
    }
    return sakhteman;
}
public static class Building{
    int left, right, height;
    public Building(int left, int right, int height) {
        this.left = left;
        this.right = right;
        this.height = height;

    }
    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
}
public static class Point{
    public int x, y;
    public Point(int x, int y){
        this.x = x;
        this.y = y;
    }
}
public static class runClass {
    public static void main(String[] args) {
        sakhteman.Building[] arr = new Building[] {
            new sakhteman.Building(1, 11, 5),
            new sakhteman.Building(2, 6, 7),
            new sakhteman.Building(3, 13, 9),
            new sakhteman.Building(12, 7, 16),
            new sakhteman.Building(14, 3, 25),
            new sakhteman.Building(19, 18, 22),
            new sakhteman.Building(23, 13, 29),
            new sakhteman.Building(24, 4, 28)
        };
        ArrayList<Point> res=findsakhteman(arr);

        System.out.println(res.toString()); 
    }
}
}

1 个答案:

答案 0 :(得分:0)

您正在执行一个永不结束的递归循环,该循环在findsakhteman函数中(第12行),请检查您的停止条件。