Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: Returning an array

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Returning an array

    So this class take the value of endMiles and startMiles from the main class. Then it would calculate the distance by subtracing the startMiles from endMiles then returning the value of distanceV(This is the variable I use). As you can see I have a method to return the value of distanceV, this work perfectly fine but I'm calculating more than one distance so I want to put all the distanceV into an array. I have two method to put the distanceV to an array but when I return it and try to print it in the main method, it give me some random characters. Thanks

    public class AnnualFuelUse
    {
        //Variables
        private int endVMiles , startVMiles, distanceV;
     
    	private int [] distanceArray = new int[4];
     
        //Constructor
        AnnualFuelUse(int endMiles,int startMiles)
        {
            endVMiles = endMiles;
            startVMiles= startMiles;
            distanceV = 0;
     
        }
     
        //Calculation
            public void calcDistance()
        {
            distanceV =  endVMiles - startVMiles;   
        }
     
        //Return startDistance
        public int getStart()
        {
            return startVMiles;
        }
        //Return endDistance
        public int getEnd()
        {
            return endVMiles;
        }
        //Return distance
        public int getDistance()
        {
            return distanceV;
        }
     
            //Calc distanceArray
        public void calcArray()
        {
            for(int index = 0 ; index < distanceArray.length; index++)
            {
                distanceArray[index] = distanceV;
            }
        }
     
        //Return distanceArray
        public int[] getArray()
        {
            return distanceArray;
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Returning an array

    it give me some random characters
    Please post the output that you are talking about and a full program that compiles, executes and generates the output.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Returning an array

    This is the main method class

    public class AnnualFuelUseTester
    {
        public static void main(String[]args)
        {
            //Array of object
            AnnualFuelUse fuelUse []= {
                                       new AnnualFuelUse(17283 , 16923 ),
                                       new AnnualFuelUse(17483 , 17283 ),
                                       new AnnualFuelUse(17733 , 17483 ),
                                       new AnnualFuelUse(18033 , 17733 )
                                      };
     
     
        //Call Methods
        for(int index = 0 ; index < fuelUse.length; index++)
        {
            fuelUse[index].calcDistance();
     
        }
     
    	//Print max/min
    	System.out.println("Here is an example:"+fuelUse[1].getArray());
        }
    }

    This print out " This is an example :[I@36c27576"

    Is what I did on the other class the way to return an array ? Thanks

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Returning an array

    This print out " This is an example :[I@36c27576"
    That is what is printed out when you print an int array. It is returned by the toString() method

    To see the contents of an array, use the Arrays class's toString() method:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Returning an array

    So instead of making the array in the outer class, I decided to make the array in the main method class.(I haven't learn the toString yet so can't used it). Here is what I have. Is it possible to put any of this: - in the outer class ( The class in my orignal post) instead of the main? Thanks
      int[] distanceA = new int[4];
        for(int index = 0 ; index< fuelUse.length; index++)
        {
            distanceA[index] = fuelUse[index].getDistance();
        }
    	int min  = Integer.MAX_VALUE;
    	for(int i = 0 ; i < distanceA.length;i++)
    	{
    		if(distanceA[i] < min)
    			min = distanceA[i];

    public class AnnualFuelUseTester
    {
        public static void main(String[]args)
        {
            //Array of object
            AnnualFuelUse fuelUse []= {
                                       new AnnualFuelUse(17283 , 16923 ),
                                       new AnnualFuelUse(17483 , 17283 ),
                                       new AnnualFuelUse(17733 , 17483 ),
                                       new AnnualFuelUse(18033 , 17733 )
                                      };
     
     
        //Call Methods
        for(int index = 0 ; index < fuelUse.length; index++)
        {
            fuelUse[index].calcDistance();
     
        }
     
        int[] distanceA = new int[4];
        for(int index = 0 ; index< fuelUse.length; index++)
        {
            distanceA[index] = fuelUse[index].getDistance();
        }
    	int min  = Integer.MAX_VALUE;
    	for(int i = 0 ; i < distanceA.length;i++)
    	{
    		if(distanceA[i] < min)
    			min = distanceA[i];
        }
     
        }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Returning an array

    What does the program output now when it is executed?

    Is it possible to put any of this: - in the outer class
    Are you asking about a method? Who will call the method? What data would the method need to do its computations? Will it need to save any data between calls it receives?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Returning an array

    With the toString method or my modify code?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Returning an array

    The Arrays class's toString() method is used for debug output and rarely used to generate output for a report. You print out the contents of an array so you can see what the program has put into it.

    Post the output from your program that shows what the code is printing out and add some comments to the post describing what you want the output to look like.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Returning an array

    With this code :

    public class AnnualFuelUseTester
    {
        public static void main(String[]args)
        {
            //Array of object
            AnnualFuelUse fuelUse []= {
                                       new AnnualFuelUse(17283 , 16923 ),
                                       new AnnualFuelUse(17483 , 17283 ),
                                       new AnnualFuelUse(17733 , 17483 ),
                                       new AnnualFuelUse(18033 , 17733 )
                                      };
     
     
        //Call Methods
        for(int index = 0 ; index < fuelUse.length; index++)
        {
            fuelUse[index].calcDistance();
     
        }
     
        int[] distanceA = new int[4];
        for(int index = 0 ; index< fuelUse.length; index++)
        {
            distanceA[index] = fuelUse[index].getDistance();
        }
    	int min  = Integer.MAX_VALUE;
    	for(int i = 0 ; i < distanceA.length;i++)
    	{
    		if(distanceA[i] < min)
    			min = distanceA[i];
        }
     
        }
    }
    It print out perfectly what I wanted, but I wanted this part of the code: - To be in the AnnualFuelUse class (My orignal post) instead of the main class. It is appropriate to have this part of the code in the main class instead of the AnnualFuelUse? I thought the main class suppose to be only used for calling on the method on the outside class ( I don't know what it called when one class is associated with another so I'll just call it outside class) Thanks
      int[] distanceA = new int[4];
        for(int index = 0 ; index< fuelUse.length; index++)
        {
            distanceA[index] = fuelUse[index].getDistance();
        }
    	int min  = Integer.MAX_VALUE;
    	for(int i = 0 ; i < distanceA.length;i++)
    	{
    		if(distanceA[i] < min)
    			min = distanceA[i];
        }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Returning an array

    Have you tried making it a method?

    I don't know what rules you've been given about what goes in a "main" class and can't give you any advice.
    I put the main() method in the class that has the code to be executed. I don't create a separate, special class that has only a main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    maple1100 (January 21st, 2013)

  12. #11
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Returning an array

    I prefer coding the way you describe it too.- Yes I try making it a method but I don't know how to get it minimum value (min) back to the main method class so I can display it.

    public class AnnualFuelUse
    {
        //Variables
        private int endVMiles , startVMiles, distanceV;
        private int [] distanceArray = new int[4];
        private int min  = Integer.MAX_VALUE;
        //Constructor
        AnnualFuelUse(int endMiles,int startMiles)
        {
            endVMiles = endMiles;
            startVMiles= startMiles;
            distanceV = 0;
     
        }
     
        //Calculation
            public void calcDistance()
        {
            distanceV =  endVMiles - startVMiles;   
        }
     
        //Return startDistance
        public int getStart()
        {
            return startVMiles;
        }
        //Return endDistance
        public int getEnd()
        {
            return endVMiles;
        }
        //Return distance
        public int getDistance()
        {
            return distanceV;
        }
     
             //Calc distanceArray
        public void calcArray()
        {
            for(int index = 0 ; index < distanceArray.length; index++)
            {
                distanceArray[index] = distanceV;
            }
        }   
     
        public void calcDDD()
        {
    	for(int i = 0 ; i < 4;i++)
    	{
    		if(distanceArray[i] < min)
    			min = distanceArray[i];
             }
        }
     
        public int getMin()
        {
            return min;
        }
    }

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Returning an array

    how to get it minimum value (min)
    Call the getMin() method.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Returning an array

    I was thinking of putting this in the main method class : - But since the methods are not static, I can't call the method this way. May I get a hint of how to call it? Thanks
    System.out.println("Min Value:"+AnnualFuelUse.getMin());

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Returning an array

    To call a non-static method in a class you need to get a reference to an instance of the class and use that to call the method:
       refToClass.methodInClass(); //  call methodInClass using refToClass

    Where in the main() method is there a reference to an instance of the AnnualFuelUse class?
    Use that reference to call that instance's getMin() method.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Returning an array

    Would this be a consider the reference to the instance? Thanks

            AnnualFuelUse fuelUse []= {
                                       new AnnualFuelUse(17283 , 16923 ),
                                       new AnnualFuelUse(17483 , 17283 ),
                                       new AnnualFuelUse(17733 , 17483),
                                       new AnnualFuelUse(18033 , 17733 )
                                      };

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Returning an array

    That looks like an array full of references to 4 different instances.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Returning an array

    Hmm yes. This is why I was having the problem of turning the finding the minimum value into a methods. I guess I'll keep it in the main method to make it easier. Thanks for your help!

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Returning an array

    Each instance has its own min value.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #19
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Returning an array

    I didn't want to spam the forum with a new thread, so hopefully it find for me to request help in this same one since it related.
    I'm getting loss of precision for the following : - mpgA is a double but "i" is an int. So I get the loss of precision here "minMPG = mpgA[i]" How can I fix this? Thanks
    	double[] mpgA = new double[4];
        for(int index = 0 ; index< fuelUse.length; index++)
        {
            mpgA[index] = fuelUse[index].getMPG();
        }
     
    	int minMPG  = Integer.MAX_VALUE;
    	for(int i = 0 ; i < mpgA.length;i++)
    	{
    		if(mpgA[i] < minMPG)
    			minMPG = mpgA[i];
        }

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Returning an array

    If you're happy with 1.99 becoming 1, then you can cast the double to an int by putting (int) before it.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #21
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Returning an array

    So I cast the array of double to array of int?

  23. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Returning an array

    Not the array, the double value.
    If you don't understand my answer, don't ignore it, ask a question.

  24. The Following User Says Thank You to Norm For This Useful Post:

    maple1100 (January 21st, 2013)

Similar Threads

  1. Returning An Array
    By LoganC in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 28th, 2012, 02:57 PM
  2. Having trouble returning an array
    By 93tomh in forum What's Wrong With My Code?
    Replies: 16
    Last Post: July 30th, 2012, 11:09 AM
  3. [SOLVED] Issue when returning an array
    By Broxxar in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 21st, 2012, 09:19 PM
  4. returning 2d array in java
    By dr.code.skm in forum Member Introductions
    Replies: 2
    Last Post: July 20th, 2011, 10:14 AM
  5. returning a 2D array
    By straw in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 03:30 AM