Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Array and Objects trouble

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Array and Objects trouble

    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);
      }
    }


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    150
    My Mood
    Cheerful
    Thanks
    11
    Thanked 25 Times in 25 Posts

    Default Re: Array and Objects trouble

    You're calling the toString() of arrays, which are pretty similar to Object's toString(). It basically returns hash code.

    You're better using a for loop


    double[][] array = objects.getArray();
     
     
    for (int i=0; i < array.length; i++)
    {
    for (int j =0; j < array[i].length; j++)
    {
    System.out.println(array[i][j]);
     
     
    }
    }

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Array and Objects trouble

    Quote Originally Posted by GoodbyeWorld View Post

    You're better using a for loop


    double[][] array = objects.getArray();
     
     
    for (int i=0; i < array.length; i++)
    {
    for (int j =0; j < array[i].length; j++)
    {
    System.out.println(array[i][j]);
     
     
    }
    }
    I have made changes based on your answer. When ran, it will print the number of points the user asked for, however it only prints zeros. Is there an error happening when I set the array to the values of "points" and "dimension" to cause this to happen? (I have changed the code above to the updated code). Thanks

  4. #4
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    150
    My Mood
    Cheerful
    Thanks
    11
    Thanked 25 Times in 25 Posts

    Default Re: Array and Objects trouble

    Quote Originally Posted by incxx View Post
    I have made changes based on your answer. When ran, it will print the number of points the user asked for, however it only prints zeros. Is there an error happening when I set the array to the values of "points" and "dimension" to cause this to happen? (I have changed the code above to the updated code). Thanks
    That is because java, I believe, will auto-initialize primitives (it doesn't do that with objects). Since you didn't call fillArray(), the data never got inserted into it, hence it was still in java's default initialization. I believe that java sets uninitialized number types to 0 by default.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Array and Objects trouble

    Quote Originally Posted by GoodbyeWorld View Post
    Since you didn't call fillArray(), the data never got inserted into it, hence it was still in java's default initialization.
    So would i call the fillArray() before I send "dimension" and "points" into setArray() by adding this,

     
        objects.fillArray(objects.getArray(), objects.getRange(), objects.getDimension(), objects.getNumPoints());
     
        objects.setArray(dimension, points);
     
        double[][] dataPoints = objects.getArray();

  6. #6
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    150
    My Mood
    Cheerful
    Thanks
    11
    Thanked 25 Times in 25 Posts

    Default Re: Array and Objects trouble

    Quote Originally Posted by incxx View Post
    So would i call the fillArray() before I send "dimension" and "points" into setArray() by adding this,

     
        objects.fillArray(objects.getArray(), objects.getRange(), objects.getDimension(), objects.getNumPoints());
     
        objects.setArray(dimension, points);
     
        double[][] dataPoints = objects.getArray();
    Not exactly. It seems that setArray() is initializing your array. (It is not filling it, but making memory for the array itself.) Your default constructor doesn't do that so it will be null and will thrown an exception likely if you call fillArray() or anything with it before calling setArray().

    (You can have a separate method print out the array if you need to.)

    public multiDimArrayObjects(double[][] datapoints)
      {
       lengthA = 0;
       points = 0;
       dimension = 0;
       dataPoints = new double [points][dimension];    // <--------------  that is changing your param variable, not your class variable
       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];  // <--------------  that is changing your param variable, not your class variable
       length = new double [dimension];  // <--------------  that is changing your param variable, not your class variable
      }


    I think, in scoping, that the variable dataPoints that you're referring to would go to the most local level, or your constructor.

    If you want to initialize your class variable to the array passed to it, do something like this


    this.dataPoints = dataPoints;


    The "this" is an object's reference to itself. That would set the class variable dataPoints rather than reassigning your param "dataPoints" which is what your code appeared to be doing.

    Also,

    dataPoints = new double[points][dimension] makes an array of size 0 in your first non-default constructor.
    It was also make your array "length" an array of size 0 in that constructor.

    As for what to do about your methods, you could call setArray(int, int) without the printing. Then you would call fillArray(). After that, you could call a method that prints out the array.

Similar Threads

  1. Program with objects and classes trouble
    By incxx in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 3rd, 2013, 02:16 PM
  2. Trouble adding objects to JComboBox
    By MarcusSt0ne in forum AWT / Java Swing
    Replies: 2
    Last Post: April 12th, 2013, 06:51 AM
  3. [SOLVED] Merge two 1D array's into a new 1D array. Trouble appending last index
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2012, 12:57 PM
  4. Replies: 17
    Last Post: July 27th, 2012, 12:52 AM
  5. [SOLVED] array trouble
    By littlefuzed in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 08:56 PM