Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

explain method action please

 
Ronald Pruitt
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ronald,
Part of your code in your for loop is missing... without it I am not sure what it is doing...
-Nate
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code should look like this:
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic