31.5k views
2 votes
In java Develop two functions: one to display arrays of integers and another to display arrays of Strings. In both functions, display the length of the array, then display the array index followed by the array value at that index. Two examples are below: a. Given the integer array {2, 30, 9, 48, 5, 72, 3}, the function displays: Array has 7 elements [0]: 2 [1]: 30 [2]: 9 [3]: 48 [4]: 5 [5]: 72 [6]: 3 b. Given the String array {"red", "blue", "green", "orange"}

Array has 4 elements
[0]: red
[1]: blue
[2]: green
[3]: orange

1 Answer

3 votes

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.

User Gibumba
by
8.2k points