Okay guys htis is my updated code. This is what I have. I need stack to keep it original numbers and I need stack 2,3,and 4 to keep their original numbers and right now all I'm getting is 1s 2s and 0s
import java.util.Random;
public class Lab5
{
public static void main(String[] arg)
{
Random rnd = new Random();
Stack<Integer> stack = new IStack<Integer>();
Stack<Integer> stack2= new IStack<Integer>();
Stack<Integer> stack3= new IStack<Integer>();
Stack<Integer> stack4= new IStack<Integer>();
int stream,y;
for(int i=0;i<20;i++)
{
stream = rnd.nextInt(101);
stack.push(stream);
// stack2.push(stream);
}
for(int i=0; i<20; i++)
{
int x = stack.pop()%3;
if(x == 0%3)
stack2.push(x);
if(x == 1%3)
stack4.push(x);
if(x == 2%3)
stack3.push(x);
}
while( !stack.isEmpty() )
System.out.print(stack.pop()+" ");
System.out.println();
while( !stack2.isEmpty() )
System.out.print(stack2.pop()+" ");
System.out.println();
while( !stack3.isEmpty() )
System.out.print(stack3.pop()+" ");
System.out.println();
while( !stack4.isEmpty() )
System.out.print(stack4.pop()+" ");
System.out.println();
}
}
for(int i=0;i<21;i++)
{
stream = rnd.nextInt(101);
stack.push(stream);
stack2.push(stream);
}
After this code, your stack2 contains 21 Ints.
But when you do this:
for(int i=0; i<21;i++)
{
int x = stack2.pop();
if(stack2.pop()== 0%3) ....
It removes 2 items per loop (since you call stack2.pop() twice) and you get a ArrayOutOfBounds Exception. You should replace the second and for that matter all stack2.pop() codebits with x.
you should do like this: do stack2.pop()
only once and check. Out of bound exception was because you were doing pop
operation more than the stack
size.
for(int i=0; i<21;i++)
{
int x=stack2.pop();
if(x==0%3)
stack.push(x);
else
stack.push(x);
if(x == 1%3)
stack4.push(x);
else
stack.push(x);
if(x == 2%3)
stack3.push(x);
else
stack.push(x);
stack.pop()
will return the last item on the stack. However, it will also remove it from the stack. Putting the result into a variable and checking against that variable instead will do what you want.
See this simplified example of what you currently are doing and why it's causing you an error
|
stack.push(1);
| 1 |
stack.push(4);
| 1 | 4 |
stack.pop(); // returns 4
| 1 |
stack.pop(); // returns 1
|
stack.pop(); // throws array out of bounds exception, since no more items in stack.
I guess the more solutions we present, the more constraints will show up. First of all, the straight-forward solution would be to skip the pop phase and simply fill in all stacks in one go in the first place:
for(int i=0;i<20;i++) {
final int value = rnd.nextInt(101), x=value%3;
stack.push(value);
if(x == 0) stack2.push(x);
else if(x == 1) stack4.push(x);
else if(x == 2) stack3.push(x);
}
If having a pop-phase is a requirement, you can utilize the fact that a stack is actually obsolete for storing a number of values which are all the same; a simple counter would be sufficient:
for(int i=0;i<20;i++)
stack.push(rnd.nextInt(101));
int numberOfZeros=0;
while(!stack.isEmpty()) {
int value = stack.pop(), x = value % 3;
if(x == 0) numberOfZeros++;
else if(x == 1) stack4.push(x);
else if(x == 2) stack3.push(x);
stack2.push(value);// use stack2 as temporary storage
}
while(!stack2.isEmpty()) stack.push(stack2.pop()); // transfer back
for(;numberOfZeros>0; numberOfZeros--) stack2.push(0);
Finally, here is the academic solution that most likely will make your teacher happy:
create a recursive helper function:
static void fillOtherStacks(Stack<Integer> from,
Stack<Integer> stack2, Stack<Integer> stack3, Stack<Integer> stack4) {
int value = from.pop(), x = value % 3;
if(x == 0) stack2.push(x);
else if(x == 1) stack4.push(x);
else if(x == 2) stack3.push(x);
if(!from.isEmpty())
fillOtherStacks(from, stack2, stack3, stack4);// process the rest of stack
from.push(value); // and push back the value
}
and use it
for(int i=0;i<20;i++)
stack.push(rnd.nextInt(101));
fillOtherStacks(stack, stack2, stack3, stack4);
All solutions use the pure stack operations pop
, push
and isEmpty
which your custom Stack
will have to implement.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.