multidimensional array as argument

H

Huub

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
 
A

Andrew Thompson

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 );
}
}
}
Can somebody please help?

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

Woebegone

Huub said:
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.
 
R

Roedy Green

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

Huub

Woebegone said:
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])?
 
W

Woebegone

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

Huub

Woebegone said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top