This discussion is archived
11 Replies Latest reply: Oct 24, 2007 4:04 PM by 807600 RSS

Array Lab

807600 Newbie
Currently Being Moderated
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?