aspose file tools*
The moose likes Beginning Java and the fly likes explain method action please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "explain method action please" Watch "explain method action please" New topic
Author

explain method action please

Ronald Pruitt
Ranch Hand

Joined: Jan 15, 2001
Posts: 32
Given the following information and code can someone please walk me through whattakes place, and show me the resulting values
for each of the variables:
Msize = 10
NumInList = 5
Curr is set to 3 (pos)
For a list array
After the remove method what will be the new values in the list, and what value will curr point to?
Array element and position.
Before After
7
5
2
8
9
0
0
0
0
0
I think the remove only executes once but not sure.

I'm not sure just how this works so if you could trace the code through so I could see what takes place it might help me understand.

Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Ronald,
Part of your code in your for loop is missing... without it I am not sure what it is doing...
-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
The code should look like this:
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Ronald,
Use "& lt" (without the space) in place of < when you are writing HTML. The < is interpreted as the beginning of a tag and will not be displayed correctly.
[This message has been edited by Bodie Minster (edited January 26, 2001).]
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Ronald,
What your code is doing is re-adjusting the listArray after you remove the object.
E.g. Given your list looks like following:
[0] -> a
[1] -> b
[2] -> c // curr index is here
[3] -> d
[4] -> e
[5] -> null // numInList index is here

And supposing curr is pointed to [2]. After you invoke remove(), your list shifts everything up to fill the [2] slot.
[0] -> a
[1] -> b
[2] -> d
[3] -> e
[4] -> e // numInList index is here
[5] -> null
NOTE Here's a JAVA gotcha. Notice when you shifted everything up, you created an extra reference to the last object, since all you did was shift everything it and substract one from numInList index. Your list is still keeping a reference to the object "e" around, so it will never get GC if your list stays around for the life of your application. For example, if we move curr index to the last position ([3]) and execute remove(), your list will now look like this:
[0] -> a
[1] -> b
[2] -> d
[3] -> e // numInList index is here
[4] -> e
[5] -> null
This is because curr is at position 3 and subtracting one from numInList (4-1) is also 3, so no shifting is required. You're already at the end of the list. You still have to subtract one from numInList to reflect the fact that you removed one element from your list.
To fix this problem, I would suggest modifying your code like this:

-Peter

[This message has been edited by Peter Tran (edited January 26, 2001).]
 
wood burning stoves
 
subject: explain method action please