multidimensional array as argument

Discussion in 'Java' started by Huub, May 29, 2004.

  1. Huub

    Huub Guest

    Hi,

    I want to pass elements of a multidimensional array to a method in a
    different class. The book I'm using (Introduction to Java Programming by
    Liang) doesn't state clearly if and how this is possible.

    Can somebody please help?

    Thanks

    Huub
     
    Huub, May 29, 2004
    #1
    1. Advertising

  2. On Sat, 29 May 2004 20:11:52 +0200, Huub wrote:

    > I want to pass elements of a multidimensional array to a method in a
    > different class. The book I'm using (Introduction to Java Programming by
    > Liang) doesn't state clearly if and how this is possible.


    <sscce>
    class TwoByTwo {

    String[][] twoByTwo = {

    { "a", "b" },
    { "c", "d" }
    };

    TwoByTwo() {

    Results.printResults( twoByTwo );
    }

    public static void main(String args[]) {

    TwoByTwo tbt = new TwoByTwo();
    }
    }

    class Results {

    public static void printResults(String[][] results) {

    for (int ii=0; ii<results.length; ii++) {
    for (int jj=0; jj<results.length; jj++) {
    System.out.print(results[ii][jj] + " \t" );
    }
    System.out.println( ii );
    }
    }
    }
    </sscce>

    > Can somebody please help?


    You might consider posting to a
    different group for the moment..
    <http://www.physci.org/codes/javafaq.jsp#cljh>

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, May 29, 2004
    #2
    1. Advertising

  3. Huub

    Woebegone Guest

    "Huub" <> wrote in message
    news:Eb4uc.648$9n5.516@amstwist00...
    > Hi,
    >
    > I want to pass elements of a multidimensional array to a method in a
    > different class. The book I'm using (Introduction to Java Programming by
    > Liang) doesn't state clearly if and how this is possible.
    >
    > Can somebody please help?
    >
    > Thanks
    >
    > Huub
    >


    Certainly it's possible -- here's a simple example:

    class P {
    private int[][] table;
    public void passTableToQ() {
    Q q = new Q();
    q.useTable(table);
    }
    }
    class Q {
    public void useTable(int[][] table) {
    }
    }

    If you just want to pass one element at a time, access it by index from the
    calling class and pass the single element as a method parameter.

    --
    Hope that answers your question! Regards,
    Sean.
     
    Woebegone, May 29, 2004
    #3
  4. Huub

    Roedy Green Guest

    On Sat, 29 May 2004 20:11:52 +0200, Huub <> wrote
    or quoted :

    >I want to pass elements of a multidimensional array to a method in a
    >different class. The book I'm using (Introduction to Java Programming by
    >Liang) doesn't state clearly if and how this is possible.


    you can either pass the whole array, individual elements of the array,
    of if you get tricky, slices of the array.

    See http://mindprod.com/jgloss/gotchas.html#MATRIX.

    It does not talk about parameter passing per se, but it works just the
    same as assignment. You likely can figure it out from what I tell you
    there about matrix initialisation.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, May 29, 2004
    #4
  5. Huub

    Huub Guest

    Woebegone wrote:
    > "Huub" <> wrote in message
    > news:Eb4uc.648$9n5.516@amstwist00...
    >
    >>Hi,
    >>
    >>I want to pass elements of a multidimensional array to a method in a
    >>different class. The book I'm using (Introduction to Java Programming by
    >>Liang) doesn't state clearly if and how this is possible.
    >>
    >>Can somebody please help?
    >>
    >>Thanks
    >>
    >>Huub
    >>

    >
    >
    > Certainly it's possible -- here's a simple example:
    >
    > class P {
    > private int[][] table;
    > public void passTableToQ() {
    > Q q = new Q();
    > q.useTable(table);
    > }
    > }
    > class Q {
    > public void useTable(int[][] table) {
    > }
    > }
    >
    > If you just want to pass one element at a time, access it by index from the
    > calling class and pass the single element as a method parameter.
    >

    Single element? Like q.useTable([0][0])?
     
    Huub, May 29, 2004
    #5
  6. Huub

    Woebegone Guest


    > >
    > > If you just want to pass one element at a time, access it by index from

    the
    > > calling class and pass the single element as a method parameter.
    > >

    > Single element? Like q.useTable([0][0])?
    >


    Close -- if Q has a method useTableElement(int), you would call it like
    q.useTableElement(table[0][0]).

    And to repeat earlier advice,

    "You might consider posting to a
    different group for the moment..
    <http://www.physci.org/codes/javafaq.jsp#cljh>"
     
    Woebegone, May 29, 2004
    #6
  7. Huub

    Huub Guest

    Woebegone wrote:
    >>>If you just want to pass one element at a time, access it by index from

    >
    > the
    >
    >>>calling class and pass the single element as a method parameter.
    >>>

    >>
    >>Single element? Like q.useTable([0][0])?
    >>

    >
    >
    > Close -- if Q has a method useTableElement(int), you would call it like
    > q.useTableElement(table[0][0]).
    >
    > And to repeat earlier advice,
    >
    > "You might consider posting to a
    > different group for the moment..
    > <http://www.physci.org/codes/javafaq.jsp#cljh>"
    >
    >

    OK, thank you.
     
    Huub, May 29, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Bazell

    slice of multidimensional array

    Dave Bazell, Jul 23, 2003, in forum: Perl
    Replies:
    2
    Views:
    4,103
  2. epigram
    Replies:
    1
    Views:
    10,859
    =?Utf-8?B?c29jaWV0b3BpYS5uZXQ=?=
    Jul 16, 2005
  3. Ben
    Replies:
    8
    Views:
    12,769
    Eki Y. Baskoro
    Dec 18, 2003
  4. Szabolcs Borsanyi

    constant multidimensional array in a function argument

    Szabolcs Borsanyi, Apr 21, 2008, in forum: C Programming
    Replies:
    1
    Views:
    333
    Ben Bacarisse
    Apr 21, 2008
  5. Felipe Ribeiro

    Passing multidimensional array as function argument

    Felipe Ribeiro, May 12, 2009, in forum: C Programming
    Replies:
    4
    Views:
    966
    Felipe Ribeiro
    May 13, 2009
Loading...

Share This Page