Results 1 to 8 of 8

Thread: Array Help

  1. #1
    ks1615 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default Array Help

    Hello,
    I'm wondering if its possible to do mathematic opeartions using array
    I've been trying 20 different methods doing this (changed the double values into a string)
    For example
    Java Code:
    double [] inches = {1, 2, 3, 4, 5, 6, 7};
    double [] centimeter = {inches * 2.54};
    System.out.println(centimeter);

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    It's possible to do math with arrays, but not as you're trying. You need to write out everything yourself. For instance if you want a method that multiples a scalar (i.e., 2.54) by an array, then you should create this method and do the multiplication in a for loop. Please take care to only do manipulations on a copy of the array because since an array is an object and an array variable is a reference variable, changes made to the array inside of the method will be reflected in the array outside of the method.

  3. #3
    ks1615 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default

    So something like this
    Java Code:
    double [] inches = {1, 2, 3, 4, 5, 6, 7};
    for(int i = 0, i < 8; i++)
    {
    double [] cm = {inches * 2.54};
    }
    Or like this?
    Java Code:
    double [] inches = {1, 2, 3, 4, 5, 6, 7};
    double [] cm = {1 * 2.54, 2 * 2.54, 3 * 2.54, etc}

  4. #4
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    I think i would go with the first one...
    Though I think that you should not use the "magic number"-that's what someone told me when I explicitly specified the length of the array in one of my coding (of course not in this forum)-

    You should use
    Java Code:
    array.length()
    :)

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default I don't think so....

    Java Code:
    double [] inches = {1, 2, 3, 4, 5, 6, 7};
    for(int i = 0, i < 8; i++)
    {
    double [] cm = {inches * 2.54};
    }
    I don't think the above is going to work. It's full of errors. It would have to be somethng like:
    Java Code:
    public class Inches2cms {
    	
    public static void main (String[] arg) {
    
    double [] inches = {1, 2, 3, 4, 5, 6, 7};
    double [] cms = new double [inches.length];
    for(int i = 0; i < inches.length; i++)
    	{
    	 cms [i] = inches[i] * 2.54;
    	 System.out.println(cms[i]);//added to be able to see if it works
    	}
    }
    }
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    ks1615 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default

    Thx for the responses I fixed my first program but now I'm stuck on this one.
    I have to use two given double array (One is tempreture, the other is humidity) and use both of them to get Heat Index. I've got the equation written out but i'm stuck on a few minor things
    I have to use a for-each loop also
    Here is the code
    What should I place in ___.length?
    How would I structure the for each loop?
    Java Code:
    public class HeatIndex
    {
       public static void main(String [] args)
       {
           System.out.println("\t\t\tHeat Index: Key West, Florida");
           System.out.println("\n\t\t\t\tMonths");
           String [] month = {"Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec.", "Avg."};
           double [] temp = {{70.3 , 70.8, 73.8, 77.0 80.7, 83.4, 84.5, 84.4, 83.4, 80.2, 76.3, 72.0, 78.1}};
           double [] humid = {69, 67, 66, 64, 66, 69, 67, 70, 69, 69, 70, 68};
           double [] heatIndex = new double [______.length];
           for(int i = 0; i < ; i++)
           {
               double heatIndex = -42.379 + (2.04901523 * temp) + (10.14333127 * humid) - (0.22475541 * temp * humid) - (.00683783 * temp * temp) - (.05481717 * humid * humid) + (.00122874 * temp * temp * humid) + (.00085282 * temp * humid * humid) - (.00000199 * temp * temp * humid * humid);
            }
        }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The length of the headIndex array will all depend on your requirements. You have some arrays with 12 members (one for each month) and some with 13 (each month plus a spot for the average). You have to decide how many items this array will need in order to solve your problem.
    Last edited by Fubarable; 03-01-2009 at 07:50 PM.

  8. #8
    ks1615 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default

    Well I fixed the String and the two doubles, was missing 1
    They are all evened out now
    But here is the current code
    How would I handle the length of both humid and temp into the heat index?
    Here is the current equation for the heat index

    Java Code:
     String [] month = {"Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec.", "Avg."};
           double [] temp = {70.3, 70.8, 73.8, 77.0, 80.7, 83.4, 84.5, 84.4, 83.4, 80.2, 76.3, 72.0, 78.1};
           double [] humid = {69, 67, 66, 64, 66, 69, 67, 67, 70, 69, 69, 70, 68};
           double [] heatIndex = new double [];
    Java Code:
    double heatIndex = -42.379 + (2.04901523 * temp) + (10.14333127 * humid) - (0.22475541 * temp * humid) - (.00683783 * temp * temp) - (.05481717 * humid * humid) + (.00122874 * temp * temp * humid) + (.00085282 * temp * humid * humid) - (.00000199 * temp * temp * humid * humid);

Similar Threads

  1. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 07:09 AM
  2. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:08 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
  •