I'm trying to set the method "setArray()" with the values of "dimension" and "points" entered by a user. When I try to print it by calling the "getArray()" method, it prints "[[D@6818c458". Why is this happening?
Main Program:
import java.io.*; import java.util.Arrays; public class mainClass { public static void main (String [] args) throws IOException { BufferedReader myInput = new BufferedReader (new InputStreamReader(System.in));//buffered reader arrayObjects objects; objects = new arrayObjects(); int points = 0, dimension = 0; double lengthA = 0; PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayPoints.txt"));//writes to arrayPoints.txt file System.out.println("How many dimensions do you want?"); String dimensionA = myInput.readLine(); dimension = Integer.parseInt(dimensionA); objects.setDimension(dimension); System.out.println("How many points do you want?"); String pointsA = myInput.readLine(); points = Integer.parseInt(pointsA); objects.setNumPoints(points); System.out.println("Enter a range: "); String lengthB = myInput.readLine(); lengthA = Double.parseDouble(lengthB); objects.setRange(lengthA); objects.setArray(dimension, points); double[][] dataPoints = objects.getArray(); System.out.println(objects.getDimension()); System.out.println(objects.getRange()); System.out.println(objects.getNumPoints()); } }
Object Class
import java.io.*; import java.util.Arrays; public class multiDimArrayObjects { private int dimension; private double lengthA; private int points; private double [][] dataPoints; private double [] length; public multiDimArrayObjects(){ } public multiDimArrayObjects(double[][] datapoints) { lengthA = 0; points = 0; dimension = 0; dataPoints = new double [points][dimension]; length = new double [dimension]; } public multiDimArrayObjects(double myLength, int myPoints, int myDimension, double[][] dataPoints, double [] length){ lengthA = myLength; points = myPoints; dimension = myDimension; dataPoints = new double [points][dimension]; length = new double [dimension]; } public double[][] getArray(){ return dataPoints; } public void setArray(int dimension, int points){ dataPoints = new double [points][dimension]; for (int i=0; i < dataPoints.length; i++) { for (int j =0; j < dataPoints[i].length; j++) { System.out.println(dataPoints[i][j]); } } } public int getDimension(){ return dimension; } public void setDimension(int myDimension){ dimension = myDimension; } public int getNumPoints(){ return points; } public void setNumPoints(int myNumPoints){ points = myNumPoints; } public double getRange(){ return lengthA; } public void setRange(double myRange){ lengthA = myRange; } public void fillArray(double [][] dataPoints, double lengthA, int dimension, int points)throws IOException { // PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayPoints.txt"));//writes to arrayPoints.txt file for(int z = 0; z < dimension; z++){//fills the length array with the the set value of lengthA length[z] = lengthA; }//end for for(int x = 0; x < points; x++){//runs 1000 times to print 1000 data points for (int y = 0; y < dimension; y++){//runs 2 times to print an x and y coordinate dataPoints [x][y]= (2 *Math.random() - 1) * length[y];// finds a random number in the range and assigns it to the coordinate array }//end for }//end for } public void readData()throws IOException { BufferedReader readfile = new BufferedReader(new FileReader("arrayPoints.txt"));//reads data from arrayPoints.txt try {//try statement StringBuilder file = new StringBuilder(); String line = readfile.readLine();//assigns line data from file while (line != null) {//run while line isn't empty file.append(line); file.append('\n'); line = readfile.readLine(); }//end while String finalData = file.toString();//converts to a string System.out.print(finalData);//prints the coordinate } //end try finally {//close file when done readfile.close(); }//end finally }//end readFile public void writeData(double[][] dataPoints, int points)throws IOException { PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayPoints.txt"));//writes to arrayPoints.txt file for (int i = 0; i < points; i++){ fileOut.println(Arrays.toString(dataPoints[i]));//converts array to string and prints to file }//end for fileOut.close();//writes to file readData();// calls readFile method } public static void sortArray(double [] dataPoints,int low, int n){ int lo = low; int hi = n; if (lo >= n) { return; } double mid = dataPoints[(lo + hi) / 2]; while (lo < hi) { while (lo<hi && dataPoints[lo] < mid) { lo++; } while (lo<hi && dataPoints[hi] > mid) { hi--; } if (lo < hi) { double T = dataPoints[lo]; dataPoints[lo] = dataPoints[hi]; dataPoints[hi] = T; } } if (hi < lo) { int T = hi; hi = lo; lo = T; } sortArray(dataPoints, low, lo); sortArray(dataPoints, lo == low ? lo+1 : lo, n); } }