Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #19604 > unrolled thread
| Started by | Chad <cdalten@gmail.com> |
|---|---|
| First post | 2012-11-03 07:45 -0700 |
| Last post | 2012-11-03 16:02 -0700 |
| Articles | 1 on this page of 21 — 9 participants |
Back to article view | Back to comp.lang.java.programmer
Creating a new stack from an exisiting one. Chad <cdalten@gmail.com> - 2012-11-03 07:45 -0700
Re: Creating a new stack from an exisiting one. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-11-03 15:51 +0000
Re: Creating a new stack from an exisiting one. Chad <cdalten@gmail.com> - 2012-11-03 09:22 -0700
Re: Creating a new stack from an exisiting one. markspace <-@.> - 2012-11-03 11:20 -0700
Re: Creating a new stack from an exisiting one. "BartC" <bc@freeuk.com> - 2012-11-03 18:58 +0000
Re: Creating a new stack from an exisiting one. "Pascal J. Bourguignon" <pjb@informatimago.com> - 2012-11-04 00:49 +0100
Re: Creating a new stack from an exisiting one. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-11-04 00:44 +0000
Re: Creating a new stack from an exisiting one. "Pascal J. Bourguignon" <pjb@informatimago.com> - 2012-11-04 13:14 +0100
Re: Creating a new stack from an exisiting one. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-11-04 12:48 +0000
Re: Creating a new stack from an exisiting one. Thomas Richter <thor@math.tu-berlin.de> - 2012-11-04 14:14 +0100
Re: Creating a new stack from an exisiting one. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-11-04 15:38 +0000
Re: Creating a new stack from an exisiting one. "Pascal J. Bourguignon" <pjb@informatimago.com> - 2012-11-04 14:41 +0100
Re: Creating a new stack from an exisiting one. "BartC" <bc@freeuk.com> - 2012-11-04 14:11 +0000
Re: Creating a new stack from an exisiting one. "Pascal J. Bourguignon" <pjb@informatimago.com> - 2012-11-04 16:00 +0100
Re: Creating a new stack from an exisiting one. markspace <-@.> - 2012-11-04 11:02 -0800
Re: Creating a new stack from an exisiting one. Martin Gregorie <martin@address-in-sig.invalid> - 2012-11-04 19:56 +0000
Re: Creating a new stack from an exisiting one. Jeff Higgins <jeff@invalid.invalid> - 2012-11-03 16:46 -0400
Re: Creating a new stack from an exisiting one. Jeff Higgins <jeff@invalid.invalid> - 2012-11-04 08:22 -0500
Re: Creating a new stack from an exisiting one. Jeff Higgins <jeff@invalid.invalid> - 2012-11-04 11:30 -0500
Re: Creating a new stack from an exisiting one. Jeff Higgins <jeff@invalid.invalid> - 2012-11-04 19:40 -0500
Re: Creating a new stack from an exisiting one. Lew <lewbloch@gmail.com> - 2012-11-03 16:02 -0700
Page 2 of 2 — ← Prev page 1 [2]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-11-03 16:02 -0700 |
| Message-ID | <eb9beb63-a05e-4efa-9cac-1c59e45b645b@googlegroups.com> |
| In reply to | #19604 |
Chad wrote:
> I have stack like the following..
>
> [1, 4, 10, 20]
Others have commented on the vagueness of your spec. I'll just assume things.
I assume you mean that '20' is at the top of the stack, based on your later
remarks.
> What I need to do is pop the top item off the stack such that it looks
> like the following..
>
> [1, 4, 10] [20]
By this I assume you mean stack [1, 4, 10] and item '20' in its own memory.
> Then, starting at [20], I need to be able to push the numbers 78 and
> 99 onto this stack such that the output looks like
>
> [1, 4, 10] [20, 78, 99]
So here you push first 20, then the others onto a second stack.
> Can this be done without creating a new stack? If so how? I just need
Based on what you said and my assumptions, no. The very definition of your
operations is to push '20' onto a new stack, so you have to create a new
stack onto which to push '20'.
> some general idea. Here is what I came up with so far..
>
> import java.util.*;
>
> public class funcTest {
Java naming conventions call for an initial upper-case letter in type names.
> public static void main(String[] args) {
> Stack<Integer> stack = new Stack<Integer>();
> stack.add(1);
> stack.add(4);
> stack.add(10);
> stack.add(20);
>
> System.out.println(stack);
>
> Object temp = stack.pop();
Really 'temp' should be declared 'Integer'.
> System.out.println(stack + " " + "["+ temp + "]");
>
> //This part from here on down doesn't work
And by "doesn't work", you mean ...?
> /*
> stack = (Stack)temp;
Oh, /that's/ what you mean. This violates the type system of Java.
You see, the object in 'temp' is not itself a stack. So there.
> stack.push(78);
You can't "push" onto an 'Integer'.
> stack.push(99);
> System.out.println(stack + " " + "["+ temp + "]");
> *
> */
> }//end main()
Don't make comments that contribute no information.
> }
The fundamentals of Java rest on its type system. You tried to break it and
of course you ran afoul of type enforcement.
Don't do that.
Part of your misstep was to duck the generics. Don't try to run away from
generics. Generics turns the attempt to 'push()' an 'Integer' into a compile
error instead of a production exception.
When you program in Java, take time to think about types. You want two stacks
of integers by your very problem statement.
public class DualStacker
{
public static void main(String[] args)
{
Stack<Integer> stack = new Stack<>();
stack.add(1);
stack.add(4);
stack.add(10);
stack.add(20);
System.out.println(stack);
Integer temp = stack.pop();
System.out.println(popped + " "+ temp);
Stack<Integer> spill = new Stack<>();
spill.push(temp);
spill.push(78);
spill.push(99);
System.out.println(stack + " ["+ spill + "]");
}
}
--
Lew
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.java.programmer
csiph-web