Results 1 to 5 of 5
  1. #1
    mcleanj is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default returning an array weird problem

    Here's an example of the code i'm having a problem with

    Java Code:
    	
    public static double[] convertT(double[] t){
       double[] tArray=t;
       int index=0;
    		
       for (double num:t){
          tArray[index]=num+273.1;
          index++;
       }
    		
       return tArray;
    }
    
    public static void main(String[] args) {
    		
       double[] tempC = {77.0, 77.0, 63.5, 53.3, 53.3, 77.6, 77.6, 77.6, 52.9, 52.9, 77.6,
    	              62.7, 53.7, 53.7, 79.5, 79.5, 64.0, 64.0, 54.5, 39.2, 38.3, 49.4,
    	              40.2, 40.2, 40.2, 40.2, 39.7, 40.2, 40.2, 40.2, 39.9, 39.9, 39.8};
    	
    		
       double[] tempK=convertT(tempC);
    		
       for (double num:tempK){
          System.out.print(num+":");
       }
    		
    		
    }
    Okay better Description. I have an array of temperatures in degrees C and need to convert them into degrees K. I pass the array into the converT function and change each value inside an temp array. I have checked the temp array before returning it and the result is correct. I then return the array and assign it to the new array tempK in the main line. The values that are in tempK do not agree with the values that were in the temp array before i returned it.

    Any ideas?
    Last edited by mcleanj; 01-28-2011 at 05:26 AM. Reason: formatting plus description

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

    Default

    Please edit your post to add code tags so your code is readable. My first link below will tell you how. Also, please provide more detail about your problem. Your current description is a bit unclear to me.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    When you pass Object (and arrays are Objects) you pass a reference to the Object/array and not a copy of it. So in your code the variables tempC, t, tArray and tempK all reference the same array. So when you make a change to one of the variables, all the other variables see that change.

    Solution: create a new array in your method.

  4. #4
    mcleanj is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    hopefully thats a bit better thanks for the links new to this forum

  5. #5
    mcleanj is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

Similar Threads

  1. returning double array
    By Billaguana in forum New To Java
    Replies: 2
    Last Post: 01-16-2011, 03:59 AM
  2. Problem returning Array values Please help
    By drgnfire25 in forum New To Java
    Replies: 4
    Last Post: 01-12-2011, 12:53 AM
  3. returning array
    By aizen92 in forum New To Java
    Replies: 4
    Last Post: 01-08-2011, 03:10 PM
  4. Returning An Array
    By elektronika in forum New To Java
    Replies: 2
    Last Post: 12-07-2009, 03:43 PM
  5. Returning array problem.
    By Chase in forum New To Java
    Replies: 4
    Last Post: 10-21-2008, 09:07 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
  •