Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Array Lab

807600Oct 24 2007 — edited Oct 24 2007
I'm having trouble with this lab on Arrays:

In the following code fragment, a is a five-element array of Strings and p is a five-element array of ints. Write code that assigns to each element of p the length of the corresponding element of a. For example, when a is the array with elements "Oops", "I", "did", "it", "again" (in that order), then after executing your code the elements of p should be 4, 1, 3, 2, 5 (in that order).

String[] a = { "Oops", "I", "did", "it", "again" };
int[] p = new int[5];

I tried this code but it didn't work:

p[0] = a[0].length;
p[1] = a[1].length;
p[2] = a[2].length;
p[3] = a[3].length;
p[4] = a[4].length;

How should I fix it?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 21 2007
Added on Oct 24 2007
11 comments
698 views