1

I need to figure out what this method does. I don't understand this part specifically. I also would like to know how to print the answer. What I have done just prints a memory address I think.

tmp[i] = a[i].length();

Here is the entire method

    private static int[] bar(String []a)
{
    int [] tmp = new int[a.length];
    for (int i = 0; i<a.length;i++)
    {
        tmp[i] = a[i].length();
    }
    return tmp;
}

This is what I have tried to do to call the method and print the answer.

    String [] a = {"hey", "how", "are", "you"};
    System.out.println(bar(a));
1
  • 2
    return the size of each word, and by the way it's not the memory address, it's just the name of the class followed by the hashcode Commented Oct 31, 2015 at 16:43

1 Answer 1

3

The method creates an array of sizes of the parameters. So for ["hey", "how", "are", "you"] it will give back [3, 3, 3, 3].

An easy way to print an array nicely is Arrays.toString:

System.out.println(Arrays.toString(bar(a)));

What I have done just prints a memory address I think.

No. What you saw was the result of the toString method of the array. Arrays inherit the default implementation of Object.toString, which is classname + "@" + hexadecimal representation of the hashcode.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.