Results 1 to 4 of 4
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    103
    Rep Power
    0

    Default Understanding Problem in Code !

    Consider the following code:

    Java Code:
    public class Aufg3 {
        public static void main(String[] args) {
            int[] test = decToBin(12, getBinArray(12));
            for(int i = 0; i < test.length; i++){
                System.out.println(test[i]);
            }
        }
    
        public static int[] getBinArray(int number){
            int res = number, length = 0;
            while(res != 0){        
                res /= 2;
                        length++;
            }
            return new int[length];
        }
    
        public static int[] decToBin(int number, int[] array){
            int res = number, k = array.length-1;
            while(res != 0){
                if(res%2 == 0){
                    array[k] = 0;
                }else{
                    array[k] = 1;
                }
                k--;
                res /= 2;
            }
            return array;
        }
    }

    IF we consider the following part of code:

    Java Code:
     public static int[] getBinArray(int number){
            int res = number, length = 0;
            while(res != 0){        
                res /= 2;
                        length++;
            }
            return new int[length];
        }

    Why are they returning the following:

    Java Code:
    return new int[length];
    Please explain as I haven't understood it.

    Thanks

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

    Default Re: Understanding Problem in Code !

    Quote Originally Posted by Jack_Tauson_Sr View Post
    Why are they returning the following:

    Java Code:
    return new int[length];
    Who are "they" and how are they "returning the following"?

    If you mean "the compiler" and if you're seeing an error, then by all means you should post the error for us to see and review.

  3. #3
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    103
    Rep Power
    0

    Default Re: Understanding Problem in Code !

    "They" means the person who has written the code. I'm trying to understand it. Shouldn't it be returning just "length" ?

    Quote Originally Posted by Fubarable View Post
    Who are "they" and how are they "returning the following"?

    If you mean "the compiler" and if you're seeing an error, then by all means you should post the error for us to see and review.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,927
    Rep Power
    4

    Default Re: Understanding Problem in Code !

    Take a look at the method return value. It is int[]. This means that it is returning an array of ints. If it was just returning the length, the return value would just be an int.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Need help understanding steganography code
    By nrott in forum New To Java
    Replies: 3
    Last Post: 02-04-2012, 11:20 AM
  2. Urgent help understanding code
    By varons666 in forum New To Java
    Replies: 3
    Last Post: 06-03-2011, 07:51 PM
  3. need help understanding part of code
    By mustachMan in forum New To Java
    Replies: 7
    Last Post: 02-09-2011, 07:11 PM
  4. Replies: 9
    Last Post: 09-21-2010, 04:15 PM
  5. trouble understanding code help
    By yasmin k in forum New To Java
    Replies: 4
    Last Post: 11-16-2009, 09:46 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
  •