Created
November 4, 2013 22:42
-
-
Save MosheBerman/7310503 to your computer and use it in GitHub Desktop.
The output is: [a, b, c]
[a, b, c, c] Why is the output left to right? Is the insertElementAt 1-based instead of zero based?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This class extends the Stack | |
* so that there's a pushdown(i) | |
* method which copies the elements | |
* at index 0 and inserts it and index i. | |
* All items beneath index i are pushed down. | |
* | |
*/ | |
import java.util.Stack; | |
import java.util.Arrays; | |
public class ExtendedStack<T> extends Stack<T> { | |
public static void main(String[] args) | |
{ | |
ExtendedStack<String> e = new ExtendedStack<String>(); | |
e.push("a"); | |
e.push("b"); | |
e.push("c"); | |
e.dumpContents(); | |
Integer element = 2; | |
e.pushdown(element); | |
e.dumpContents(); | |
} | |
public void pushdown(int i) | |
{ | |
this.insertElementAt(this.peek(), i); | |
} | |
/** | |
* Dump the contents of the stack. | |
*/ | |
public void dumpContents(){ | |
System.out.println(Arrays.toString(this.toArray())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment