So my question simply put is this, is it possible to have an n dimensional array be the argument for a function?
Printable View
So my question simply put is this, is it possible to have an n dimensional array be the argument for a function?
They are usually referred to as methods in Java.
The answer is as easy as writing a little code:
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.
Yes, I meant a variable depth array.
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.
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.
Sorry, here's my test code:
And the error is:Code:public static void main(String [] args){
Object [] hello = new Object[20];
doStuff(hello);
}
public void doStuff(Object [][] hello){
}
doStuff(java.lang.Object[][]) in test cannot be applied to (java.lang.object[])
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.
No, but this compiles:
Now try to figure out how many dimensions there are.Code:public class Foo001 {
public static void main(String[] args) {
Object[] hello = new Object[20];
doStuff(hello);
}
public static void doStuff(Object whatTheHeck) {
}
}
Are you familiar with reflection?
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
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... ;(
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
Code:int dim = 0;
Class arrayCount = hello.getClass();
while (arrayCount.isArray()) {
dim++;
arrayCount = arrayCount.getComponentType();
}
Arrays are objects, so there's no mystery about that.Quote:
apparently you can just declare the argument as an Object with no arrays about it, which is interesting,
db
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.
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.
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?
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.
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.
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
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
Code:Object[][][][][][][][][][][][] // etc