Results 1 to 17 of 17
  1. #1
    foota is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default N dimensional array as argument?

    So my question simply put is this, is it possible to have an n dimensional array be the argument for a function?

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

    Default

    They are usually referred to as methods in Java.

    The answer is as easy as writing a little code:

    Java Code:
    class Test {
        void foo(/*your code here to declare a multidimensional array argument*/) {}
    }

    If n is supposd to be a variable rather than some specific value (you don't say), then the problem becomes more interesting.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pbrockway2 View Post
    \
    If n is supposd to be a variable rather than some specific value (you don't say), then the problem becomes more interesting.
    But still potentially solvable.

  4. #4
    foota is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Yes, I meant a variable depth array.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'd imagine it should be possible, why not try writing a small snippet and see what happens when you pass it different depth arrays.
    Last edited by sunde887; 05-07-2011 at 03:48 AM.

  6. #6
    foota is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Yeah that doesn't fly.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by foota View Post
    Yeah that doesn't fly.
    I don't understand what you're saying here. Have you've tried it and it failed? If so, then post your code and your errors. Or are you saying that you don't agree with previous posts in your thread? If so, then please be more specific so we can address your concerns.

  8. #8
    foota is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Sorry, here's my test code:
    Java Code:
        public static void main(String [] args){
            Object [] hello = new Object[20];
            
            doStuff(hello);
        }
        
        public void doStuff(Object [][] hello){
            
        }
    And the error is:
    doStuff(java.lang.Object[][]) in test cannot be applied to (java.lang.object[])

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What happens if you make it take a 1 d array and pass it a multi dimensional array? I would test it but I'm on my phone.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    No, but this compiles:

    Java Code:
    public class Foo001 {
       public static void main(String[] args) {
          Object[] hello = new Object[20];
    
          doStuff(hello);
       }
    
       public static void doStuff(Object whatTheHeck) {
          
       }
    }
    Now try to figure out how many dimensions there are.

    Are you familiar with reflection?
    Last edited by Fubarable; 05-07-2011 at 04:04 AM.

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

    Default

    First if you tried the code I posted earlier - ie left out the main() method - you would have seen that the code compiles without error. So

    Java Code:
    public void doStuff(Object [][] hello) {}

    is a perfectly good method taking a multidimensional argument, which answers (one version of) your original question.

    The problem now is how you call or invoke that method. Notice that you have to use a multidimensional array as an argument! At the moment you have "Object[] hello = new Object[20];". But that declares a 1-d array. So change it so it declares a 2-d array of Object references. (along the lines of Arrays in Oracle's Tutorial. Or mindprod.com where the author calls it "matrix initialisation")

    Also note that you are calling doStuff() from the static main() method, so doStuff() itself has to be declared as static. (You should be usre you understand this point.)

    --------------------

    I'm still a little uncertain about the array dimension. You say it should be variable, but the example you give is for a 2-d array.

    [edit] posted with my usual celerity... ;(

  12. #12
    foota is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    What I meant phrock was how to make a method that can take any dimension of an array -- and apparently you can just declare the argument as an Object with no arrays about it, which is interesting,

    And on how to count the dimensions of an array you can do this
    Java Code:
    int dim = 0;
    Class arrayCount = hello.getClass();
    while (arrayCount.isArray()) {
        dim++;
        arrayCount = arrayCount.getComponentType();
    }

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    apparently you can just declare the argument as an Object with no arrays about it, which is interesting,
    Arrays are objects, so there's no mystery about that.

    db

  14. #14
    foota is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Well yes arrays are Objects, but then what is different between an array of Objects and a singular Object which makes an Object parameter able to take any dimensioned array while an array of Objects can only take it's dimension of array, after all they are both just objects right?

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

    Default

    Quote Originally Posted by foota View Post
    Well yes arrays are Objects, but then what is different between an array of Objects and a singular Object which makes an Object parameter able to take any dimensioned array while an array of Objects can only take it's dimension of array, after all they are both just objects right?

    The difference is that an Object is just a thing. Anything. Including an array of things that can themselves be anything.

    But an array of Object is more specific: it is an array of things. Which might, or might not, themselves be arrays.

    And an Object[][] is even more specific: it is an array of arrays of things (although those things can also be arrays).

    What you written might well do what you want (if so, good). But the results of that getDim() method might be a little surprising.

    Java Code:
    public class GetDim {
    	public static void main(String[] args) {
    		Object test;
    		
    		test = new Object[][] {
    			{"fee"}, 
    			{"fie", "foe"}, 
    			{"fum"}
    		};
    		System.out.println(getDim(test));
    
    		test = new Object[] {
    			new Object[]{"fee"}, 
    			new Object[]{"fie", "foe"}, 
    			new Object[]{"fum"}
    		};
    		System.out.println(getDim(test));
    	}
    	
    	@SuppressWarnings("rawtypes")
    	static int getDim(Object hello) {
    		int dim = 0;
    		Class arrayCount = hello.getClass();
    		while (arrayCount.isArray()) {
    		    dim++;
    		    arrayCount = arrayCount.getComponentType();
    		}
    		return dim;
    	}
    }

    The first test is reported as a 2-d array while the second as 1-d. The second will be reported as 1-d not because of it's contents but because of how it was declared.

  16. #16
    foota is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Hm, that is really interesting. I would not have thought that they would be different, could you please explain the difference in how they are stored internally?

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

    Default

    I wouldn't have a clue how they are "stored internally". I'm not saying it's a Bad question, just that I don't know. Others might. I'm not even sure how much the specifications of the Java language or the JVM say about the way an implementation might arrange memory etc.

    From the point of view of their content the two things assigned to test are not different. Which is why I said that your method would give surprising (==unintended) results.

    --------------

    Everything in Java has a type. For arrays the type comes from the type of its elements, which are all of the same type. So in the example I posted the following produce things of different type.

    Java Code:
    test = new Object[][] { // etc
    test = new Object[] { //etc

    At runtime getClass() will return a Class which depends only on the element type. And getComponentType() will return a Class reflecting that element type. These methods are precisely the way provided in the Java language for the element type (however it is stored internally) to be got at.

    ---------------

    It is interesting, but I wonder whether it is the way to go. Personally if I had a need for matrices of some variable dimension I would probably make a class to represent such a thing.

    Java Code:
    class Matrix {
        public Matrix(int dim) {
            // etc
        }
    }

    Then - to answer the question you posed at the start of thread - you could have a method that took a multidimensional array as an argument just by saying

    Java Code:
    void foo(Matrix m) {}

    Of course you would have to implement the behaviour you wanted such a multidimensional matrix to have. (Ie you couldn't just reuse Java array methods and operations). But that is not such a bad thing: it is often the case that your own class will be superior (more expressive) than dragooning an array into service.

    To take just one example, if the dimension gets large (100, say) then the chances are good that the matrix will be rather sparse. For example if the length of each dimension is 5 then such a matrix could hold 7.88860905 10^69 elements. It is very unlikely that you have that much data! By implementing your own class you could have such a beast without whatever overhead is associated with that many nulls, and without the plain ugliness (or worse) of writing

    Java Code:
    Object[][][][][][][][][][][][] // etc

Similar Threads

  1. 2 dimensional array
    By sehudson in forum New To Java
    Replies: 5
    Last Post: 02-21-2011, 12:56 AM
  2. 2 dimensional array help!
    By ber1023 in forum New To Java
    Replies: 9
    Last Post: 01-02-2011, 01:29 AM
  3. Two dimensional array
    By niu_niu in forum New To Java
    Replies: 4
    Last Post: 06-13-2010, 01:34 AM
  4. about two dimensional array
    By matin1234 in forum New To Java
    Replies: 2
    Last Post: 06-01-2010, 12:09 PM
  5. two-dimensional array
    By kHim in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 08:21 PM

Tags for this Thread

Posting Permissions

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