Reverse arrays using Stacks in java

Problem:

Create a java program that reverses an array in java using stacks.

Output:

90 78 40 9 6 3 1
10.5 6.9 3.2 2.5

Solution:

import java.util.Stack;

public class ReverseArrayApp {

 public static void main(String[] args){
  Integer[] arr1 = {1, 3, 6, 9, 40, 78, 90};
  Double[] arr2 = {2.5, 3.2, 6.9, 10.5};
  
 
  Number[] arr1_rev = reverse(arr1);
  Number[] arr2_rev = reverse(arr2);
  
  
  for(Number eltInt : arr1_rev) 
   System.out.print(eltInt + " ");
  System.out.println();
  for(Number eltDouble : arr2_rev)
   System.out.print(eltDouble + " ");
  }

 
 private static Number[] reverse(Number[] arr) {
  Stack<Number> temp = new Stack<>();
  Number [] output = new Number[arr.length];
  
  for(Number elt: arr) 
   temp.push(elt);
  
  int index = 0;
  
  while(!temp.empty()){
   output[index] = temp.pop();
   index++;
  }
  
  return output;
  
 }
 
 
 }
 


1 comment :

  1. liposuction cost Korea's #1 Liposculpture Clinic. Lydian plastic surgery is the home of VIP patients. Celebrities, Influencers and Diplomats all know and trust Doctor An and Lydian plastic surgery clinic to provide detailed results.

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact