Answer to Question #272699 in Java | JSP | JSF for harryy

Question #272699

8. Groupings

by CodeChum Admin

Did you know that you can also reverse lists and group them according to your liking with proper code? Let's try it to believe it.


Instructions:

  1. Create a variable that accepts a positive odd integer.
  2. Create an empty list. Then, using loops, add random integer values into the list one by one. The number of values to be stored is dependent on the inputted odd integer on the first instruction.
  3. Reverse the order of the list. Then, print out the list's new order in this manner:
  4. [first-half values]-[middle value]-[second-half values], just like that of the sample output. Make use of list slicing that you’ve learned from the past lessons to easily achieve this segment.

Input

The first line contains an odd positive integer.

The next lines contains an integer.

5
1
3
4
5
2

Output

A line containing a list.

[2,5]-[4]-[3,1]
1
Expert's answer
2021-11-28T11:44:44-0500
import java.util.Scanner;
public class Array {  
    public static void main(String[] args) { 
        Scanner scan=new Scanner(System.in);
        System.out.println("Input an interger");
        int num=scan.nextInt();
        int [] array = new int[]{ 1, 3, 4, 5,2};
        System.out.println("Original array: ");  
        for (int i = 0; i < array.length; i++) {  
            System.out.print(array[i] + " ");  
        }  
        System.out.println();  
        System.out.println("Array in reverse order: ");   
        for (int i = array.length-1; i >= 0; i--) {  
            System.out.print("["+array[i] + "] ");  
        }  
    }  
}  

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS