Transform
//
// Transform.java
//
import java.util.*;
public class Transform
{
public static int[] stringLengths(String[] names)
{
// transform an array of Strings into an array of ints
// containing the lengths of those Strings
int[] result = new int[names.length]; // create empty array
for (int i=0; i<result.length; i++)
result[i] = names[i].length();
return result;
}
public static void main(String[] args)
{
String[] names = {"The", "quick", "brown", "fox"};
System.out.print("names: ");
for (String name : names)
System.out.print(name + " ");
System.out.println();
int[] lengths = stringLengths(names);
System.out.print("stringLengths: ");
for (int x : lengths)
System.out.print(x + " ");
System.out.println();
}
}Output:
names: The quick brown fox
stringLengths: 3 5 5 3