排序内部类java的向量

时间:2013-11-19 10:36:52

标签: java vector inner-classes

我正在学习java我必须将程序从c ++更改为java。输入: 10 26 77 12 37 14 18 19 96 71 95 91 9 98 43 66 77 2 75 94 91 我收到了一个错误。我之前从未使用过比较器。任何人都可以告诉我我做错了什么。

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
import java.util.Vector;

public class Main {
    static int n;

    static public class punct
    {
        public int x;
        public int y;
    }

    static punct v[];
    static Vector <punct> vec;
    static boolean sortY(punct a,punct b)
    {
        return (a.y < b.y);
    }

    static boolean sortX(punct a,punct b)
    {
        return (a.x < b.x);
    }

    static int myDistance(punct a1,punct a2)
    {

        return ((a1.x - a2.x) * (a1.x - a2.x) + (a1.y - a2.y) * (a1.y - a2.y)) ;
    }   

    static int devideAndConquer(int left,int right)
    {
        int d;
        int c;
        if(right - left <= 2)
        {
            d = (1000000000);

            for(int i = left; i < right ; i++ )
            {
                for(int j = i + 1 ; j<= right; j++)
                {
                    d = Math.min(myDistance(v[i], v[j]),d);
                }
            }
            Arrays.sort(v,new Comparator <punct>()
            {
                public int compare(punct a, punct b)
                {
                    return (int) a.y - b.y;
                }
            });

            return d;
        }
        else
        {
            int middle = left + (right - left) / 2;

            d = Math.min(devideAndConquer(left, middle),devideAndConquer(middle + 1, right));

            int l = left;
            int r = middle+1;

            vec.clear();
            while( (l <= middle) && (r <= right) )
            {
                if( sortY(v[l], v[r]) == true)
                {
                    vec.add(v[l]);
                    l++;
                }
                else
                {
                    vec.add(v[r]);
                    r++;
                }
            }

            while( l <= middle )
            {
                vec.add(v[l]);
                l++;
            }

            while( r <= right )
            {
                vec.add(v[r]);
                r++;
            }

            for(int i = left, k = 0; i<=right; i++,k++)
            {
                v[i] = vec.get(k);
            }

            for(int i = left ; i<= right ; i++)
            {
                for(int j = i+1; (j <= right) && (j-i < 7) ; j++)
                {
                    d = Math.min(myDistance(v[i],v[j]),d);
                }
            }

            return d;
        }
    }

    static void read()
    {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        v = new punct [n+10];
        punct a = new punct();
        for(int i = 0; i<=n-1;i++)
        {
            a.x = scanner.nextInt();
            a.y = scanner.nextInt();
            v[i] = a;
        }
        scanner.close();
    }

    static void write(int d)
    {
        double x = Math.sqrt(d);
        System.out.print(x);
    }
    public static void main(String args[])
    {
        read();
        Arrays.sort(v,new Comparator <punct>(){
                    public int compare(punct a, punct b)
                    {
                        return  a.x - b.x;
                    }
                });
        int d = devideAndConquer(0,n-1);
    }
}

3 个答案:

答案 0 :(得分:2)

您的read()方法

中存在一个问题

更改

    punct a = new punct();  //this line should be in your for loop
    for(int i = 0; i<=n-1;i++)
    {
        a.x = scanner.nextInt();
        a.y = scanner.nextInt();
        v[i] = a;
    }

    for(int i = 0; i<=n-1;i++)
    {
        punct a = new punct(); //moved inside the loop
        a.x = scanner.nextInt();
        a.y = scanner.nextInt();
        v[i] = a;
    }

另一个问题在这里

更改

   n = scanner.nextInt();
   v = new punct [n+10]; 

    n = scanner.nextInt();
    v = new punct [n]; 

哦,我错过了这个

更改

  static Vector <punct> vec;

 static Vector <punct> vec = new Vector<punct>();

答案 1 :(得分:1)

read()方法中的两个问题:

    static void read()
    {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        v = new punct [n];
        for(int i = 0; i<=n-1;i++)
        {
            punct a = new punct();
            a.x = scanner.nextInt();
            a.y = scanner.nextInt();
            v[i] = a;
        }
        scanner.close();
    }
  1. 数组初始化:v = new punct[n+10] should be v = new punct[n]
  2. 创建punct对象(punct a = new punct();)应该在for-loop
  3. 此外,您应该初始化vec对象:static Vector <punct> vec = new Vector<punct>();


    另一个小注意事项:您可以使用d = (1000000000);

    代替d = Integer.MAX_VALUE;

答案 2 :(得分:0)

下一个[问题]是v数组中的10个元素都是空值,这就是你得到NullPointerException的原因。核心在于:v = new punct [n+10];

您必须将其更改为v = new punct [n];'或增加for循环中的迭代次数。