2014-02-15 75 views
0
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()。如何解决运行时错误?

我得到错误的

运行时错误

+0

查找到的变量范围。 –

+0

找不到你。你能解释更多吗?如何解决它? – user3167973

+0

你在main中声明了'pointArray'和'pointMap',所以它在方法的范围内不被识别。 – alfasin

回答

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); 
} 
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){ 
     ... 
+0

它的工作。修复后,我得到运行时错误。请问你能帮帮我吗。 – user3167973

0

你宣布的主要方法都pointArraypointMap。它们只能在该方法内使用(方法的范围)。您可以将它们声明为一个实例变量,以便通过在主方法之外声明它们,它们将可用于该类的所有方法。

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

    //Methods and stuffs ... 
} 
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++){ 

和访问地图,你需要实例Ideone类这样

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

问题我正在定义变量S:

pointArray 
pointMap 
中的主要方法,这意味着,除非将它传递到另一个功能或对象可以只使用其中在该方法

。最好的办法是将其定义为实例变量所以像:

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