Complete the code to print the length of the array.
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[1]);In Java, arrays have a length property (without parentheses) to get their size.
Complete the code to loop through the array using its length property.
int[] values = {10, 20, 30};
for (int i = 0; i < values[1]; i++) {
System.out.println(values[i]);
}The loop uses values.length to know how many times to run.
Fix the error in accessing the array length property.
String[] names = {"Anna", "Bob", "Cara"};
int size = names[1];
System.out.println(size);The correct way to get the length of an array is using .length without parentheses.
Fill both blanks to create a map of array elements to their lengths.
Map<String, Integer> map = new HashMap<>();
String[] words = {"cat", "dog", "bird"};
for (String word : words) {
map.put(word, word[1]);
}
System.out.println(map.size() == words[2]);Strings use .length() method to get length, arrays use .length property.
Fill all three blanks to create a dictionary of word lengths for words longer than 3 characters.
Map<String, Integer> wordLengths = new HashMap<>();
String[] words = {"apple", "bat", "carrot", "dog"};
for (String [1] : words) {
if ([1].[2]() > 3) {
wordLengths.put([3], [3].length());
}
}
System.out.println(wordLengths);Use word as the loop variable, length() to get string length, and word to put in the map.
