Results 1 to 7 of 7
  1. #1
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default Passing a 2D array

    This is probably a simple thing but it's boggling my mind

    I have this class
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package cardlayout;
    
    /**
     *
     * @author Administrator
     */
    public class FamArray {
    
        private static String[][] family;
    
        public FamArray() {
            family = null;
        }
    
        public FamArray(String[][] ar) {
            family = ar;
        }
    
        public static void setFam(String[][] ar) {
            family = ar;
        }
    
        public static String[][] getFam() {
            return family;
        }
       
    }
    and this method in my main class

    Java Code:
    int c = 0;
    
                while (rs.next()) {
    
                    int count = 1;
                    for (int i = 0; i < numCols; i++) {
                        
                        data[c][i] = rs.getString(count);
                        System.out.println(rs.getString(count));
                        FamArray.setFam(data);  <---This line isn't right I think
                        System.out.println("Testing FamArray data "+FamArray.getFam());
                        
                        count++;
                    }
                    c++;
                }
    When I leave that println in there to test what the FamArray value is it's just a bunch of nonsense. I don't think i'm passing my 2D array correctly or have my FamArray class right.

    Can you tell me how to pass this variable correctly?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    > When I leave that println in there to test what the FamArray value is it's just a bunch of nonsense.

    What does a "bunch of nonsense" look like?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    Arrays don't override the toString() method so Object's toString() method is used; it prints <classname>@<hashcode>. Better read the API of the Arrays class, it has methods that can return String forms of arrays in a human readable form.

    kind regards,

    Jos

  4. #4
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    I'm still not sure what i'm supposed to do. Do I need to add a toString method to my FamArray class? Or did I not create the class correctly to deal with arrays?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by toymachiner62 View Post
    I'm still not sure what i'm supposed to do. Do I need to add a toString method to my FamArray class? Or did I not create the class correctly to deal with arrays?
    Either that or read the API documentation for the Arrays class; it has a few utility methods that can produce a human readable String form given an array.

    kind regards,

    Jos

  6. #6
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    Arrays (Java 2 Platform SE v1.4.2)

    I don't see any method in here to product a human readable string. Can you point me in the right direction if i'm looking at the wrong thing?

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by toymachiner62 View Post
    Arrays (Java 2 Platform SE v1.4.2)

    I don't see any method in here to product a human readable string. Can you point me in the right direction if i'm looking at the wrong thing?
    Java 1.4 is a very old Java version; use Java version 1.6 instead.

    kind regards,

    Jos

Similar Threads

  1. passing an array between classes
    By gisler in forum New To Java
    Replies: 10
    Last Post: 04-10-2009, 11:31 PM
  2. Passing integer array from javascript to java
    By lavanyashastri in forum New To Java
    Replies: 1
    Last Post: 02-21-2009, 07:02 AM
  3. how use string array while passing to a function
    By sks9s9 in forum New To Java
    Replies: 2
    Last Post: 02-03-2009, 01:44 PM
  4. array passing dynamically
    By jazz2k8 in forum Advanced Java
    Replies: 2
    Last Post: 10-16-2008, 11:29 PM
  5. passing an array into an instance
    By lockmac in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 10:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •