Results 1 to 7 of 7
  1. #1
    wizar is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default 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. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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 09:51 PM. Reason: missing semicolon, missing brace

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    edited: gcalvin supplied a better response before me.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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. #5
    wizar is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default

    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. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by wizar View Post
    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. #7
    wizar is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default

    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.

Similar Threads

  1. general help.
    By socboy6579 in forum New To Java
    Replies: 1
    Last Post: 10-24-2010, 10:41 PM
  2. general help.
    By socboy6579 in forum New To Java
    Replies: 1
    Last Post: 10-24-2010, 09:20 PM
  3. General Question about Java
    By bayan in forum New To Java
    Replies: 6
    Last Post: 05-26-2010, 11:16 AM
  4. general confused about java question (easy!)
    By sweetjava in forum New To Java
    Replies: 1
    Last Post: 08-09-2009, 02:03 AM
  5. Replies: 7
    Last Post: 12-16-2008, 06:44 PM

Posting Permissions

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