Question

java codeWrite a method called reverse that takes an array of integers as an input and returns the same values in reverse order as the

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Reversing an array using Java

Answer:

          Below the method uses the function java.util.Collections.reverser(List list) method. This method reverses the elements in the specified list. At first we convert the array into a list by using java.util.Arrays.asList(array) and then we reverse the list.

import java. util.*;

public class reverseArray {

             /*function to reverse the elements in an array*/

            static void reverse(Integer array[])

            {

                        /*Converting the array into list and reversing the elements*/

                          Collections.reverse(Arrays.asList(array)) ;

                              /*printing the reversed list*/

                          System.out.println(Arrays.asList(array) ;

             }

/*calling main function*/

             public static void main(String[] args)

             {

                         Integer[] array = {2, 4, 5, 7, 8, 9, 3, 10} ;

                             /*calling reverse method*/

                         reverse(array) ;

              }

}

Output:

[10, 3, 9, 8, 7, 5, 4 ,2]

                     

Add a comment
Know the answer?
Add Answer to:
java code Write a method called reverse that takes an array of integers as an input...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 11) Write a method (including header and body) that takes as an input an array of...

    11) Write a method (including header and body) that takes as an input an array of doubles and f three doubles containing the average, the maximum and the minimunm values of the input array. Test the program by calling the method from the main method. For example Input: 1 2 3 45 Output: 3 5 1 12) Write a Java method that takes a string as an argument and returns the reverse the string. Test the program by calling the...

  • JAVA Array 3. Write a method called noVowels that takes a String as input and returns...

    JAVA Array 3. Write a method called noVowels that takes a String as input and returns true if it doesn't contain any vowels (a, e, i, o, u)

  • JAVA PLEASE! Write a method called displayPets that takes an array of strings as input. The...

    JAVA PLEASE! Write a method called displayPets that takes an array of strings as input. The method should display the contents of the pets array on one line. Each name should be separated by a space. Use an enhanced for loop to iterate the array. Make sure to declare modifier, parameters and return values. Given an array String[] pets = {"Lucky", "Slinger", "Beast", "Trumpet", "Lulu", "Shadow", "Daisy"} A sample run when calling displayPets(pets) from main() would look like: Pets names:...

  • Write a Java method called compare that takes two integers as input parameters and returns 1...

    Write a Java method called compare that takes two integers as input parameters and returns 1 if the first parameter is larger than the second parameter returns – 1 if the second parameter is larger than the first parameter, and returns 0 if the two parameters are equal.

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • Write a Java program that has a method called arrayAverage that accepts an arrary of numbers...

    Write a Java program that has a method called arrayAverage that accepts an arrary of numbers as an argument and returns the average of the numbers in that array. Create an array to test the code with and call the method from main to print the average to the screen. The array size will be from a user's input (use Scanner). - array size = int - avergage, temperature = double - only method is arrayAverage Output:

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

  • Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes...

    Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes three integer values into the first three positions of an array of integers calls a method you write called doubleEachValue that takes an array of integers (the one whose values you hard-coded in main) as its only argument and returns an ArrayList of integers, with each value in returned ArrayList equal to double the correspondingly indexed value in the array that is passed in...

  • JAVA Code: Complete the method, sumOdds(), below. The method takes in an array of integers, numbers,...

    JAVA Code: Complete the method, sumOdds(), below. The method takes in an array of integers, numbers, and returns the sum of all the odd numbers in the array. The method should return 0 if the array contains no odd numbers. For example, if the given array is [12, 10, 5, 8, 13, 9] then the method should return 27 (5+13+9=27). Starter Code: public class Odds { public static int sumOdds(int[] numbers) { //TODO: complete this method    } }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT