11 Replies Latest reply on Oct 24, 2007 11:16 PM by 3004

    Array Lab

      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?