Final answer:
To display the contents of integer and string arrays in Java, create two functions: 'displayIntArray' and 'displayStringArray', which iterate over the arrays and print each element's index and value, also indicating the array's length.
Step-by-step explanation:
In Java, to display arrays of integers and display arrays of Strings, you can create two separate functions that iterate over the elements of the array and print the index along with the value at that index. Below is an example of how you might define these functions:
public void displayIntArray(int[] array) {
System.out.println("Array has " + array.length + " elements");
for(int i = 0; i < array.length; i++) {
System.out.println("[" + i + "]: " + array[i]);
}
}
public void displayStringArray(String[] array) {
System.out.println("Array has " + array.length + " elements");
for(int i = 0; i < array.length; i++) {
System.out.println("[" + i + "]: " + array[i]);
}
}
When you call
displayIntArray with an integer array such as {2, 30, 9, 48, 5, 72, 3}, it displays the number of elements followed by each index and value. Similarly, calling
displayStringArray with a String array such as {"red", "blue", "green", "orange"} produces the desired output.