How can I print a Vector that is into a Vector (main). I made a program that contains a vector of stacks (java.util), inside the package I have two classes: -Stack, -mainProgram; The Stack class will create the object though a Vector (java.util) with the following methods: pop,push,peek,isEmpty,search,print. I did everything but I don't know how to print the stack contained in the Vector (main) with Its elements, where I should be able to add,remove,search and print the stacks.
Example of what I mean:
The print of the stacks that I wanna print should be like this -->
Stack 1:
10
20
30
40
50
60
Do you have any advice?
You shouldn't be using the Vector
class anymore, as it has become a legacy class many years ago.
If you need to implement a stack, your best choice would be an ArrayDeque
.
This class is likely to be faster than Stack when used as a stack
However, if using Vector
is a constraint, you can simply print your elements with two nested for
loops. In my example, I've assumed that your innermost Vector
contains strings.
public void print(Vector<Vector<String>> vet){
int i = 0;
for (Vector<String> v : vet) {
System.out.printf("Stack %d:%n", ++i);
for (String s : v) {
System.out.println(s);
}
System.out.println();
}
}
Instead, the implementation with an ArrayDeque
would look like this:
public void print(ArrayDeque<ArrayDeque<String>> vet){
int i = 0;
for (ArrayDeque<String> v : vet) {
System.out.printf("Stack %d:%n", ++i);
for (String s : v) {
System.out.println(s);
}
System.out.println();
}
}