Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6245273

Browse files
authored
fix GenericStack.pop() and modify toString()
1. Call list[size] should return null (if list[size-1] is the last element of the stack), this does not happen because the pop() method does not actually eliminate any element.2. When pop() is called, size should never be left with a negative value.
1 parent1fcbde2 commit6245273

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

‎Exercise_19/Exercise_19_01/GenericStack.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ public void push(E o) {
2222

2323
/** Return and remove the top element from the stack */
2424
publicEpop() {
25-
Eo =list[--size];
25+
if (--size <0)
26+
size =0;
27+
Eo =list[size];
28+
E[]new_list = (E[])newObject[list.length];
29+
System.arraycopy(list,0,new_list,0,size);
30+
list =new_list;
2631
returno;
2732
}
2833

@@ -41,6 +46,15 @@ private void doubleList() {
4146

4247
@Override// Override the toString array in the Object class
4348
publicStringtoString() {
44-
return"stack: " +list.toString();
49+
Strings ="Stack: [";
50+
if (isEmpty())
51+
returns +"]";
52+
for (inti =0;i <size;i++) {
53+
if (i ==size-1)
54+
s +=list[i].toString() +"]";
55+
else
56+
s +=list[i].toString() +", ";
57+
}
58+
returns;
4559
}
46-
}
60+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp