CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2013
    Posts
    36

    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.

  2. #2
    Join Date
    Feb 2013
    Posts
    36

    Re: Creating a Matrix given arrays of the same dimensions

    hmmm, by thinking about this a little bit more, I think this should work:

    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'?

  3. #3
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Creating a Matrix given arrays of the same dimensions

    '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.
    Norm

  4. #4
    Join Date
    Feb 2013
    Posts
    36

    Re: Creating a Matrix given arrays of the same dimensions

    Quote Originally Posted by Norm View Post
    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?

  5. #5
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Creating a Matrix given arrays of the same dimensions

    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
    Last edited by Norm; August 17th, 2013 at 12:59 PM.
    Norm

  6. #6
    Join Date
    Feb 2013
    Posts
    36

    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;
                          }
         }
    Last edited by math8; August 19th, 2013 at 01:44 PM.

  7. #7
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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.
    Norm

  8. #8
    Join Date
    Feb 2013
    Posts
    36

    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.

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
  •  





Click Here to Expand Forum to Full Width

Featured