Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

About stack

Status
Not open for further replies.

ttea

IS-IT--Management
Feb 24, 2003
11
HK
I am testing to throw out a value in the array, like myarray={"9","0","8"} change to array={"9","8"}

Code:

String location[] = request.getParameterValues("select");
java.util.Stack stack = new java.util.Stack();
int losize=java.lang.reflect.Array.getLength(location);

for(int j=0;j<losize;j++)
{
if(location[j] !=&quot;0&quot;)
stack.push(location[j]);
}

while (!stack.isEmpty())
out.println(stack.pop()+&quot;<br>&quot;);

But the output is just the reversed order, &quot;0&quot; still in there, like {9,0,8}, become {8,0,9} only, what's wrong?
 
Stack has a logic of first in last out.(FILO)

try this

for(int j=(losize-1);j>=0;j--)
{
if(location[j] !=&quot;0&quot;)
stack.push(location[j]);
}

instead of

for(int j=0;j<losize;j++)
{
if(location[j] !=&quot;0&quot;)
stack.push(location[j]);
}

secondly don't use != in Strings.Use equals(<String>) method.
Thirdly your parameters might not be equal to your desired values.Check them.(location[j].equals(&quot;9&quot;) vice versa..) Salih Sipahi
Software Engineer.
City of Istanbul Turkey
ssipahi@yonbilgi.com
 
Hattusas

The problem is solved.

Thanks~~:p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top