# Creating a Matrix given arrays of the same dimensions

• August 16th, 2013, 06:51 PM
math8
Creating a Matrix given arrays of the same dimensions
say I have two int arrays of length 3 (ex: a = [ 1 2 3] and b = [4 5 6])
I would like a method that returns the 2x3 Matrix:

M =

1 2 3
4 5 6.

I have tried using

Code:

public int[][] createMatrix(int[] a, int[] b){
double[][] M = new double[2][a.length];
for(int i = 0; i < 2; i++){
for (int j = 0; j <a.length; j++){
M[i][j] = a[j];
}
}
return M;
}

I have two problems with the code above:
1)Is there a way to 'count' the number of arguments this method has? As opposed to writing '2' in
Code:

for(int i = 0; i < 2; i++)
, I think that would be better if it was something like
Code:

for(int i = 0; i < 'number of arguments in this method'; i++)
2)Well, I can see that the matrix M above will be
123
123
for the simple reason that I have not used the array b anywhere in the code. I am not sure how I can incorporate b in the inner loop.

Any help is appreciated.
• August 16th, 2013, 08:58 PM
math8
Re: Creating a Matrix given arrays of the same dimensions

Code:

double[][] M = new double[2][a.length];
M[0]=a;
M[1]=b;

But still, let say, instead of two arrays, I have two hundred arrays a1,...,a200. And I don't want to type
M[0]=a;....
over and over again. What is the best way to loop through all two hundred arrays to 'concatenate' and get the 200x3 matrix?

I guess my question is, is there a way to refer to the 'number of arguments that a method has'?
• August 17th, 2013, 09:29 AM
Norm
Re: Creating a Matrix given arrays of the same dimensions
Quote:

'number of arguments that a method has'?
The number of args a method has is set by the programmer when the method is coded. It won't change when the code is executed. Look at the "method overloading" technique. It allows multiple methods to have the same name with different number/type of args.
• August 17th, 2013, 12:47 PM
math8
Re: Creating a Matrix given arrays of the same dimensions
Quote:

Originally Posted by Norm
The number of args a method has is set by the programmer when the method is coded. It won't change when the code is executed. Look at the "method overloading" technique. It allows multiple methods to have the same name with different number/type of args.

I am not trying to change the number of arguments the method has, I am just wondering, just like you can find the number of entries an array arr has by typing arr.length, is there a way to find the number of arguments a method has?
• August 17th, 2013, 12:51 PM
Norm
Re: Creating a Matrix given arrays of the same dimensions
Quote:

find the number of arguments a method has?
The number of args will be what the programmer coded. It will be the same when the code is executed as when the code was typed in.
To get the values of any of the args passed to a method, the code needs the name of the arg.

Are you asking about the variable arguments language feature? syntax: type ... variableName
• August 19th, 2013, 01:38 PM
math8
Re: Creating a Matrix given arrays of the same dimensions
My question is, is there a way for the method to loop using each of its arguments one by one?
For instance, in the code below, is there a way to write the line "M[i] = argi;" in a correct way so that the program will understand it?
Code:

public int[][] method(arg0, arg1,..., arg200){
for(int i = 0; i < 200; i++){
"M[i] =  argi  ;"
return M;
}
}

• August 19th, 2013, 01:44 PM
Norm
Re: Creating a Matrix given arrays of the same dimensions
Each of the args to a method has a unique name assigned by the programmer when the program is coded.

If the args to the method are in a single array, its contents can be looped over.

There is no generic syntax to get to method arguments using an index. There might be a way using reflection methods and classes in the java.lang.reflect package.
• August 19th, 2013, 07:06 PM
math8
Re: Creating a Matrix given arrays of the same dimensions
I think I can use an ArrayList and an 'enhanced for loop' to loop through all its entries. I was hoping there was a way to refer to the 'argument's number' of a method, but apparently not.