Results 1 to 7 of 7
Thread: A general question
 01152011, 10:13 PM #1Member
 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. :)
 01152011, 10:47 PM #2Senior 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; } }
GaryLast edited by gcalvin; 01152011 at 10:51 PM. Reason: missing semicolon, missing brace
 01152011, 10:51 PM #3
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 10
edited: gcalvin supplied a better response before me.
 01152011, 11:01 PM #4Senior 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
 01152011, 11:08 PM #5Member
 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?
 01152011, 11:11 PM #6Senior Member
 Join Date
 Mar 2010
 Posts
 952
 Rep Power
 7
 01152011, 11:13 PM #7Member
 Join Date
 Nov 2010
 Posts
 18
 Rep Power
 0
Similar Threads

general help.
By socboy6579 in forum New To JavaReplies: 1Last Post: 10242010, 10:41 PM 
general help.
By socboy6579 in forum New To JavaReplies: 1Last Post: 10242010, 09:20 PM 
General Question about Java
By bayan in forum New To JavaReplies: 6Last Post: 05262010, 11:16 AM 
general confused about java question (easy!)
By sweetjava in forum New To JavaReplies: 1Last Post: 08092009, 02:03 AM 
toHexString optimization (in fact general optimization question)
By jann in forum Advanced JavaReplies: 7Last Post: 12162008, 07:44 PM
Bookmarks