如何解决运行时错误?

时间:2014-02-15 07:37:13

标签: java algorithm data-structures

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


class Ideone{
    //start 10:10 pm

    public static List<point> pointArray = new ArrayList<point>(10);
    public static TreeMap<Integer, point> pointMap = new TreeMap<Integer, point>();

    public static void main(String[] args){



         point p1 = new point(-2, -1);
         point p2 = new point(0,2);
         point p3 = new point(1,3);
         point p4 = new point(3,6);
         point p5 = new point(7,8);
         point p6 = new point(0,6);
         point p7 = new point(1,8);

         //Ideone.addSegment(p1);
         Ideone.addSegment(p2);
         Ideone.addSegment(p3);
         //Ideone.addSegment(p4);
         //Ideone.addSegment(p5);
         //Ideone.addSegment(p6);
         //Ideone.addSegment(p7);

         Ideone.findMaxSegment();
    }

    public static class point{
        private static int start;
        private static int end;

        public point(int start, int end){
            start = this.start;
            end = this.end;
        }

        public static void setStart(int start){
            point.start = start;            
        }

        public static void setEnd(int end){
            point.end = end;
        }
    }

    public static void addSegment(point given){
        //An array of points. 
       int len = 0;

        // if the given point doesn't become part of existing point, add as new element
        for(int i = 0; i < pointArray.size(); i++){
            point temp = pointArray.get(i);
            if((given.end >= temp.start) && (given.end <= temp.end)){

                temp.setStart(given.start);   
                len = temp.end - temp.start;
                System.out.println(temp.start+ " " + temp.end);
                pointMap.put(len, temp);
            }else if( given.start >= temp.end && given.start >= temp.start){
                temp.setEnd(given.end);
                len = temp.end - temp.start;
                pointMap.put(len, temp);
                System.out.println(temp.start+ " " + temp.end);
            }else{
                if(pointArray.get(i) == null){
                    point newPoint = new point(given.start,given.end);
                    pointArray.add(newPoint);
                    len = given.end - given.start;
                    pointMap.put(len, newPoint);
                    System.out.println(given.start+ " " + given.end);
                }    
            }
        }
        //else extend the point.
    }

    public static void findMaxSegment(){
        point maxLengthPoint = pointMap.lastEntry().getValue();
        System.out.println(maxLengthPoint.start + " " +  maxLengthPoint.end + " " + pointMap.lastEntry().getKey());
    }

}

以上代码用于以下问题:给出了一维线段的起始和结束坐标。找到可以从这些段形成的最长线段的坐标。写两个函数addSegment()和findMaxSegment()。

我收到错误

运行时错误

5 个答案:

答案 0 :(得分:0)

当变量在main内声明时,它们仅在main内可见。将声明移出main并进入类本身:

class Ideone {

    private static ArrayList<point> pointArray = new ArrayList<point>(10);
    private static Map<Integer, point> pointMap = new TreeMap<Integer, point>();


    public static void main(String[] args){
        ...

答案 1 :(得分:0)

您在main方法中声明了pointArraypointMap。它们仅在该方法中可用(方法的范围)。您可以将它们声明为实例变量,以便通过在main方法之外声明它们来使它们可用于该类的所有方法。

class Ideone {
    private static ArrayList<point> pointArray = new ArrayList<point>(10);
    private static Map<Integer, point> pointMap = new TreeMap<Integer, point>();

    //Methods and stuffs ...
}

答案 2 :(得分:0)

将这两个方面与主要方法保持一致

private static ArrayList<point> pointArray = new ArrayList<point>(10);
Map<Integer, point> pointMap = new TreeMap<Integer, point>();

并使用..来访问它

   for(int i = 0; i < Ideone.pointArray.size(); i++){

并且为了访问map,你需要像这样实例化Ideone类

Ideone ideon=new Ideone();
ideon.pointMap.put(len, temp);//

答案 3 :(得分:0)

问题是你要定义变量:

pointArray
pointMap
主方法中的

,这意味着除非将其传递给另一个函数或对象,否则只能在该方法中使用它们。最好的解决方案是将它们定义为实例变量,如:

class Ideone{
    //start 10:10 pm
    private static ArrayList<point> pointArray;
    private static ArrayList<point> pointMap;
    // Rest of code

答案 4 :(得分:0)

class Ideone{
  // declare it above outside main method
    private static ArrayList<point> pointArray;
    private static ArrayList<point> pointMap;

你的其余代码.......

有些你写了

for(int i = 0; i < pointArray.size(); i++){
            point temp = pointArray[i];//wrong

这是错误的,因为你试图从arrayList

获取元素
point temp = pointArray.get(i);//use get method to fetch elements from ArrayList

这句话也错了

pointMap.add(len, temp);//wrong

应该是

pointMap.put(len, temp);//use put method to insert element in Hashmap

以及

public static void findMaxSegment(){
        point maxLengthPoint = pointMap.lastEntry();// this is also wrong since there is no method name lastEntry in map
        System.out.println(maxLengthPoint.start + " " +  maxLengthPoint.end + " " + maxLengthPoint);
}
相关问题