Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > multidimensional array as argument

Reply
Thread Tools

multidimensional array as argument

 
 
Huub
Guest
Posts: n/a
 
      05-29-2004
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

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      05-29-2004
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
 
Reply With Quote
 
 
 
 
Woebegone
Guest
Posts: n/a
 
      05-29-2004

"Huub" <(E-Mail Removed)> 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.


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-29-2004
On Sat, 29 May 2004 20:11:52 +0200, Huub <(E-Mail Removed)> 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.
 
Reply With Quote
 
Huub
Guest
Posts: n/a
 
      05-29-2004
Woebegone wrote:
> "Huub" <(E-Mail Removed)> 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])?

 
Reply With Quote
 
Woebegone
Guest
Posts: n/a
 
      05-29-2004

> >
> > 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>"


 
Reply With Quote
 
Huub
Guest
Posts: n/a
 
      05-29-2004
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing multidimensional array as function argument Felipe Ribeiro C Programming 4 05-13-2009 10:18 AM
constant multidimensional array in a function argument Szabolcs Borsanyi C Programming 1 04-21-2008 01:49 PM
Binding Multidimensional Array to DataGrid epigram ASP .Net 1 07-15-2005 11:02 PM
Returning a multidimensional array. Ben Java 8 12-18-2003 08:48 AM
slice of multidimensional array Dave Bazell Perl 2 07-24-2003 11:17 PM



Advertisments