passing 2 dimenisional array to function


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: passing 2 dimenisional array to function

  1. #1
    Join Date
    Apr 2004
    Posts
    2

    passing 2 dimenisional array to function

    how is it done?!

  2. #2
    Join Date
    Dec 2002
    Posts
    83
    I don't think passing a double array should be any different than passing any other variable.
    Code:
    public void method1() {
    	int[][] myArr = new int[5][5];
    
    	method2(myArr);
    }
    
    public void method2(int[][] anArr) {
    	// work with array
    }
    -- Steven

  3. #3
    Join Date
    Feb 2004
    Posts
    808

    Re: passing 2 dimenisional array to function

    Originally posted by arironen
    how is it done?!
    in java, we call them methods.. are you a c programmer? in java, the array pointers are handled for you.. just use their name as a reference to the whole array, and make sure the types are consistent

    public void myMethod(TYPE NAME, TYPE2 NAME2, TYPE3 NAME3... etc ...)

    so a 5 dimensional array of ints is:

    int[][][][][] the5D....

    and to pass it to a method:

    public void methodTakes5DArray(int[][][][][] fiveDim){
    }

    note that arrays can be stripped of their dimension, as a 5D array can be thought of as an array of 4Dimensional arrays:

    Code:
    //declare an array of ten, 4D arrays:
    int[][][][][] the5D = new int[10][][][][];
    
    public void methodTakesA4DArray(int[][][][] the4D){
    ...
    }
    
    and elsewhere:
    for(int i=0; i<10; i++){  //better to write i<the5D.length
      //call the 4D method
      methodTakesA4DArray(the5D[i]);
    }
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  4. #4
    Join Date
    Apr 2004
    Posts
    2
    Thank you for the replies, it seems i was calling the function(method ) from a static contex(public void static main), since i wasnt using any classes i just made the function static. Thanks again for the replies.

    ps: yes, i have programmed in c and my only complaint about java is it making me type out boolean instead of bool and the lack of #define, other than that i love java

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center