Write a program that does the following: In your main function create an int arr
ID: 3533028 • Letter: W
Question
Write a program that does the following:- In your main function create an int array of 5 elements, e.g.: { 3, 4, 10, 8, 21 }
- Create a function that can accept the array as parameter. This function reverses the given array elements. After function finishes execution, using above example array, elements should look like { 21, 8, 10, 4, 3 }. Call this function from main,
- Create another function that also accepts the array as its parameter. This functions prints all the elements in the array. Call this function from the main after the previous function call.
Explanation / Answer
Here you go :
// Demonstrate the Stack class.
import java.util.*;
public class StackDemo {
public static void reverse(Object[] arr)
{
List e=Arrays.asList(arr);
Collections.reverse(e);
arr=e.toArray();
}
public static void showArr(Object[] a)
{
for(Object e:a)
System.out.println(e);
}
public static void main(String args[]) {
Object[] arr=new Object[]{3,4,10,8,21};
reverse(arr);
showArr(arr);
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.