Results 1 to 7 of 7
Thread: A general question
- 01-15-2011, 09:13 PM #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. :)
- 01-15-2011, 09:47 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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] + "."); } } }Is that what you meant?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; } }
-Gary-Last edited by gcalvin; 01-15-2011 at 09:51 PM. Reason: missing semicolon, missing brace
- 01-15-2011, 09:51 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
edited: gcalvin supplied a better response before me.
- 01-15-2011, 10:01 PM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 01-15-2011, 10:08 PM #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?
- 01-15-2011, 10:11 PM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
- 01-15-2011, 10:13 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
general help.
By socboy6579 in forum New To JavaReplies: 1Last Post: 10-24-2010, 10:41 PM -
general help.
By socboy6579 in forum New To JavaReplies: 1Last Post: 10-24-2010, 09:20 PM -
General Question about Java
By bayan in forum New To JavaReplies: 6Last Post: 05-26-2010, 11:16 AM -
general confused about java question (easy!)
By sweetjava in forum New To JavaReplies: 1Last Post: 08-09-2009, 02:03 AM -
toHexString optimization (in fact general optimization question)
By jann in forum Advanced JavaReplies: 7Last Post: 12-16-2008, 06:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks