To convert a Stack
to an int[]
in Java, you can follow these steps:
import java.util.*;
public class StackToArrayExample {
public static void main(String[] args) {
Stack stack = new Stack<>();
stack.push(10);
stack.push(15);
stack.push(30);
stack.push(20);
stack.push(5);
// Convert the stack to an int array
int[] intArr = stack.stream().mapToInt(Integer::intValue).toArray();
// Print the array
System.out.println("Converted array:");
for (int num : intArr) {
System.out.print(num + " ");
}
}
}