2013-10-30 60 views
0

我有一個對象和客戶端類創建,它按距離原點的距離順序打印座標。客戶端類詢問用戶他們希望數組有多少維(x,y或x,y,z)他們想要生成多少個點,以及每個座標從哪個範圍運行(例如-x到x ,-y到y等)。當我運行程序時,它會打印出正確數量的點,但數組中總是會有一個額外的元素(等等,當用戶輸入數組的維數爲'4'時,它總是打印出一個額外的元素 - > [4, 5,9,6,1])。這是爲什麼發生?客戶端和對象類輸出值

客戶端類

import java.io.*; 

public class MA { 

    public MA() { } 

    public static void main(String args[]) throws IOException { 

    String myString = "arrayNumPoints.txt"; 
    int numpoints = 0; 
    int dimension = 0; 
    double lengthscale = 0; 
    double [][] points = new double[numpoints][dimension + 1]; 

    BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in)); 

    MB pointsB = new MB(); 

    System.out.println("Enter number of points:"); 
    String numpointsA = myInput.readLine(); 
    numpoints = Integer.parseInt(numpointsA); 
    pointsB.setnumpoints(numpoints); 

    System.out.println("Enter the dimension:"); 
    String dimensionA = myInput.readLine(); 
    dimension = Integer.parseInt(dimensionA); 
    pointsB.setdim(dimension); 

    System.out.println("Enter length(range):"); 
    String lengthscaleA = myInput.readLine(); 
    lengthscale = Double.parseDouble(lengthscaleA); 
    pointsB.setlengthscale(lengthscale); 


    pointsB = new MB(numpoints, lengthscale, dimension, points); 

    pointsB.fillarray(pointsB.getarray(), pointsB.getlengthscale(), pointsB.getdim(), pointsB.getnumpoints()); 
    pointsB.caldistance(pointsB.getarray(), pointsB.getnumpoints(), pointsB.getdim()); 
    pointsB.sort(pointsB.getarray(), 0, pointsB.getnumpoints()-1, pointsB.getdim()); 
    pointsB.writefile(pointsB.getarray(), pointsB.getnumpoints(), myString); 
    pointsB.readfile(myString); 
    } 
} 

對象類

import java.io.*; 
import java.util.Arrays; 

public class MB { 

    //variables and arrays are declared 
    private double lengthscale; 
    private int numpoints; 
    private int dimension; 
    private double [][] points; 

    //constructor 
    public MB() { } 

    //constructor 
    public MB(double [][] points) { 
    numpoints = 0; 
    lengthscale = 0; 
    dimension = 0; 
    points = new double[numpoints][dimension + 1]; 
    } 

    //constructor 
    public MB(int mynumpoints, double mylengthscale, int mydimension, double [][] mypoints) { 
    numpoints = mynumpoints; 
    lengthscale = mylengthscale; 
    dimension = mydimension; 
    points = new double[numpoints][dimension + 1]; 
    } 

    //numpoints getter 
    public int getnumpoints() 
    { 
    return numpoints; 
    } 

    //numpoints setter 
    public void setnumpoints(int mynumpoints) { 
    numpoints = mynumpoints; 
    } 

    //lengthscale getter 
    public double getlengthscale() { 
    return lengthscale; 
    } 

    //lengthscale setter 
    public void setlengthscale(double mylengthscale) { 
    lengthscale = mylengthscale; 
    } 

    //dimension getter 
    public int getdim() { 
    return dimension; 
    } 

    //dimension setter 
    public void setdim(int mydimension) { 
    dimension = mydimension; 
    } 

    //array getter 
    public double[][] getarray() { 
    return points; 
    } 

    //array setter 
    public void setarray(double [][]mypoints, int numpoints, int dimension) { 
    points[numpoints][dimension] = mypoints[numpoints][dimension]; 
    } 

    //fill array method 
    public void fillarray(double [][]mypoints, double mylengthscale, int mydimension, int mynumpoints) throws IOException { 

    for(int x = 0; x < mynumpoints; x++) 
    { 
     for(int y = 0; y < mydimension; y++) { 
     //fills array with random points within the specified range 
     mypoints[x][y] = (dimension * Math.random() - 1) * mylengthscale; 
     }//end for 
    }//end for 
    } 

    //writefile method 
    public void writefile(double [][]mypoints, int mynumpoints, String myString) throws IOException { 

    //writes to myString 
    PrintWriter fileOut = new PrintWriter(new FileWriter(myString)); 

    //for loop runs for length of mylengthscale 
    for(int m = 0; m < mynumpoints; m++) { 
     //prints points to file 
     fileOut.println(Arrays.toString(mypoints[m])); 
    } 
     //close file 
     fileOut.close(); 
    //end for 
    } 

    //readfile metod 
    public void readfile(String myString) throws IOException 
    { 
    String filePath = myString; 
    //reads data from mString 
    BufferedReader in = new BufferedReader(new FileReader(new File(myString))); 
    String line = null; 

    while(((line = in.readLine()) != null)) 

    System.out.println(line); 
    in.close(); 
    } 


    //caldistance method 
    public void caldistance(double [][]mypoints, int mynumpoints, int mydimension) { 

    //for loop; calculates distance for specified number of points 
    for(int i = 0; i < mynumpoints; i++) { 
     for(int k = 0; k < mydimension; k++) { 
     mypoints[i][mydimension] = mypoints[i][k] * mypoints[i][k]; 
     }//end for loop 
     mypoints[i][mydimension] = Math.sqrt(mypoints[i][mydimension]); 
    }//end for loop        
    } 

    //sort method 
    public double[][] sort(double[][] mynewpoints, int down, int top, int mydimension) { 

    System.arraycopy(mynewpoints, 0, mynewpoints, 0, mynewpoints.length); 

    //variables are declared 
    int d = down; 
    int u = top; 

    //determines pivot point 

    double [] pivot = mynewpoints[(down + top)/2]; 


    //sorts the values of the array, comparing it to the pivot 
    do { 
     while (mynewpoints[d][mydimension] < pivot[mydimension]) { 
     d++; 
     } 

     while (mynewpoints[u][mydimension] > pivot[mydimension]) { 
     u--; 
     } 

     if (d <= u) { 
     double[] temporary = new double[mynewpoints[d].length]; 

     //compres values in array and switches them  
     for (int y = 0; y < mynewpoints[d].length; y++) { 
      temporary[y] = mynewpoints[d][y]; 
      mynewpoints[d][y] = mynewpoints[u][y]; 
      mynewpoints[u][y] = temporary[y]; 
     } 
     d++; 
     u--; 
     } 
    } while (d <= u); 

    if (down < u) { 
     mynewpoints = sort(mynewpoints, mydimension, down, u); 
    } 

    if (d < top) { 
     mynewpoints = sort(mynewpoints, mydimension, d, top); 
    } 

    return mynewpoints; 

    } 
} 

回答

0

你應該更具體的(我們展示的代碼片段,您可以用和出錯)。

但是你有沒有意識到,在你的MB的構造函數:

最後一行沒有做任何事情?你必須把它寫這樣的:

this.points = new double[numpoints][dimension + 1]; 

因爲你有兩個變量points,一個是類變量,第二個作爲參數傳遞。如果發生這種情況,不使用this選擇非類變量。

+0

我在你提到的地方加了'this'。現在,當我運行程序時,我收到錯誤說 'java.lang.ArrayIndexOutOfBoundsException:3 \t在MB.sort(MultidimensionalB.java:144) \t在MB.sort(MultidimensionalB.java:171) \t (mynewpoints [d] [mydimension] user2939235

0

可能是因爲您要添加1由用戶給出的尺寸:

points = new double[numpoints][dimension + 1]; 

這導致具有比dimension值多一個列在數組中。

+0

我有'+ 1'的原因是因爲我在那個地方儲存了距離。 – user2939235