Stack subclasses Vector which already supports this, try this...
stack.toArray(array)
Here is the Javadoc for this.
Answer from Todd on Stack OverflowStack subclasses Vector which already supports this, try this...
stack.toArray(array)
Here is the Javadoc for this.
I found that when I used the toArray method, I got the results in the reverse order to what I expected. When I created this stack:
Stack<String> stack = new Stack<>();
stack.push("foo");
stack.push("bar");
I wanted an array like:
{"bar", "foo"}
Because of course, stacks are LIFO. If you pop each element off the stack, you would pop "bar" first, and then "foo".
Instead, toArray returns {"foo", "bar"}.
A solution is to use a LinkedList instead. There is a push method on LinkedList, it performs the same as addFirst, and the result will be a list whose contents (if you traverse it of course) are "bar" and then "foo". And it also has the toArray method which returns as expected.
Test case:
LinkedList<String> list = new LinkedList<>();
list.push("foo");
list.push("bar");
String[] arr = list.toArray(new String[0]);
Assert.assertArrayEquals(new String[] { "bar", "foo" }, arr);
Videos
You can use Stream APIs of Java 8
int[] intArray = Arrays.stream(array).mapToInt(Integer::intValue).toArray();
If you can consider using Apache commons ArrayUtils then there is a simple toPrimitive API:
public static double[] toPrimitive(Double[] array, double valueForNull)Converts an array of object Doubles to primitives handling null.
This method returns null for a null input array.
You can convert entire string and then you get the toCharArray method separately characters in an array
Scanner t = new Scanner(System.in);
int x = t.nextInt();
char[] xd = String.valueOf(x).toCharArray();
for (int i = 0; i < xd.length; i++) {
System.out.println(xd[i]);
}
Another way of doing this would be:
int test = 12345;
int[] testArray = new int[String.valueOf(test).length()];
And then looping over it.
int x = 10382;
String[] str1 = Integer.toString(x).split("");
for(int i=0;i<str1.length;i++){
System.out.println(str1[i]);
}
It has to be a raw Stack[] or you can use List<Stack<YourClass>> lstStack = new ArrayList<Stack<YourClass>>().
In this case, I would prefer to use
List<Stack<Integer>> lstStack = new ArrayList<Stack<Integer>>(stackLength);
One solution could be:
public class StackInteger extends Stack<Integer> {
}
And then:
StackInteger[] numbers = new StackInteger[3];
Or even:
Stack<Integer>[] numbers = new StackInteger[3];
Try something more like this...
Before you are only reading in one number one time, which seems like you want to be used for the size of your array. After this, you continually push the same number onto the stack because you never prompt the user to enter in any other number.
The code below goes one step further than your code in that once you have learned the number from the User and set your array size, you ask for a new number over and over again to populate your array until your array is filled. This can be accomplished by prompting the user to enter a value over and over again in the for loop.
I am not really sure why exactly you need an array to do what you want to do though, consider removing the array and just using the first number entered as your bounds for the for loop rather than the array size. The array data structure is really just a waste of space as the only time you ever use it is to use its length as a stopping point in the for loop
public class NumberReverse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter stack size: ");
int number = sc.nextInt();
int[] array = new int[number];
Stack<Integer> stack = new Stack<Integer>();
for(int i = 0; i < array.length; i++){
System.out.println("Enter your number: ");
int value = sc.nextInt();
stack.push(value);
}
while (!(stack.isEmpty())) {
System.out.println(stack.pop());
}
}
}
Based on the comment below consider trying something like the code below. You are only printing out one number for the same reasons that I have listed above...
import java.util.*;
public class NumberReverse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter how many numbers you want to place on the Stack: ");
int stackSize = sc.nextInt();
Stack<Integer> stack = new Stack<Integer>();
for(int i = 0; i < stackSize; i++){
System.out.println("Enter numbers: ");
int number = sc.nextInt();
stack.push(number);
}
while (!(stack.isEmpty())) {
System.out.println(stack.pop());
}
}
}
You have to iterate over all the numbers entered by the user, and push them on the stack:
public static void main(String[] args) {
final Scanner sc = new Scanner(System.in);
System.out.print("Enter numbers (finish with something else): ");
final Stack<Integer> stack = new Stack<Integer>();
while (sc.hasNextInt())
stack.push(sc.nextInt());
while (!(stack.isEmpty()))
System.out.println(stack.pop());
}