1. Member
Join Date
Nov 2010
Posts
18
Rep Power
0

## A general question

Can anyone explain to me how to use arrays between multiple classes?

Suppose I have two double arrays. I want to pass both arrays into a class where I can use the elements of both arrays in calculations (like multiplying the first element of array 1 by the first element of array 2; 2nd element of array 1 by 2nd element of array 2, etc.) Once I put these results into a third array, how would I bring it back into the first class to print it out?

If anyone could give me an example, or help me figure one out, that would be great.
Please be understanding of me. I'm a beginner, and all of you were in a similar position at one point or another. :)

2. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
7
Java Code:
package org.javaforums.arraymultiplier;

public class MyProgram {
public static void main(String[] args) {
double[] dArray1 = {2.4, 6.8, 5.7};
double[] dArray2 = {7.3, 7.2, 9.56, 8.4};
ArrayMultiplier am = new ArrayMultiplier();
double[] result = am.multiply(dArray1, dArray2);
for (int i = 0; i < result.length; i++) {
System.out.println("result[" + i + "] = " + result[i] + ".");
}
}
}
Java Code:
package org.javaforums.arraymultiplier;

public class ArrayMultiplier {
public double[] multiply(double[] d1, double[] d2) {
int max = 0;
int min = 0;
if (d1.length > d2.length) {
max = d1.length;
min = d2.length;
} else {
max = d2.length;
min = d1.length;
}
double[] result = new double[max];
for (int i = 0; i < min; i++) {
result[i] = d1[i] * d2[i];
}
for (int i = min; i < max; i++) {
if (d1.length > min) {
result[i] = d1[i];
} else {
result[i] = d2[i];
}
}
return result;
}
}
Is that what you meant?

-Gary-
Last edited by gcalvin; 01-15-2011 at 10:51 PM. Reason: missing semicolon, missing brace

3. edited: gcalvin supplied a better response before me.

4. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
7
Please note that my example is meant only to illustrate passing and returning array type arguments. It is not meant to imply anything about correctly multiplying arrays.

-Gary-

5. Member
Join Date
Nov 2010
Posts
18
Rep Power
0
sunde, thanks for your reply. I'm sure that your solution would have helped as well, I like learning various ways to go around a problem. :)

Gary, thanks for your example, but I'm a bit confused regarding your second reply. Do you mean that the calculations using arrays should be done differently? I would have done calculations using a loop like you did, is that wrong?

6. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
7
Originally Posted by wizar
sunde, thanks for your reply. I'm sure that your solution would have helped as well, I like learning various ways to go around a problem. :)

Gary, thanks for your example, but I'm a bit confused regarding your second reply. Do you mean that the calculations using arrays should be done differently? I would have done calculations using a loop like you did, is that wrong?
I'm not saying it's wrong, but I'm not saying it's right either. That's your call. Especially regarding my method of dealing with different-sized arrays -- I'm just not vouching for the "correctness" of that in any mathematical sense.

-Gary-

7. Member
Join Date
Nov 2010
Posts
18
Rep Power
0
Ok, thanks. :)
Since the arrays I'm dealing with are all the same size, I guess that it's not too much of a problem.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•