-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.java
More file actions
62 lines (52 loc) · 2.28 KB
/
Copy pathstack.java
File metadata and controls
62 lines (52 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.ArrayList;
public class stack {
public stack(){
System.out.println("Stack Instantiated"); //Debug message to console
}
private ArrayList<String> StackString = new ArrayList<String>(); //List used to produce stack containing string values
private ArrayList<String[][]> StackStringArray = new ArrayList<String[][]>(); //List used to produce stack containing string array values
public void push(String s){ //Push method for pushing string values onto stack when string parameter is used
try {
StackString.add(s);
}
catch(ArrayStoreException e){ //If string array value is attempted to be inserted into string stack
System.out.println("Error: " + e + " - wrong data type inserted into stack");
}
}
public void push(String[][] s){ //Push method for pushing string array values onto stack when string array parameter is used
try {
StackStringArray.add(s);
}
catch(ArrayStoreException e){ //If string value is attempted to be inserted into string array stack
System.out.println("Error: " + e + " - wrong data type inserted into stack");
}
}
public String popString(){ //Pop function for string stacks
try {
String s = StackString.get(StackString.size()-1);
StackString.remove(StackString.size()-1); //Takes top value, copies, removes from stack, then returns copied value
return s;
}
catch(IndexOutOfBoundsException e){
System.out.println("Error: " + e);
return null;
}
}
public String[][] popStringArray(){ //Pop function for string array stacks
try {
String[][] s = StackStringArray.get(StackStringArray.size()-1);
StackStringArray.remove(StackStringArray.size()-1); //Takes top value, copies, removes from stack, then returns copied value
return s;
}
catch(IndexOutOfBoundsException e){
System.out.println("Error: " + e);
return null;
}
}
public int sizeString(){
return StackString.size(); //To return size of string stack
}
public int sizeStringArray(){
return StackStringArray.size(); //To return size of string array stack
}
}