View previous topic :: View next topic |
Author |
Message |
kot1990 Expert Cheater
Reputation: 1
Joined: 06 Sep 2009 Posts: 131 Location: Greece
|
Posted: Mon Aug 23, 2010 10:21 am Post subject: Any java expert for this??... |
|
|
I am trying to loop through an arrayList() and then remove some items from it
Code: | for(L2Attackable theMob : mobList)
{
if (theMob.isDead())
{
mobList.remove(theMob);
continue;
}
} |
However the remove() function fails. I also tried to do it like this but also the same:
Code: | for(L2Attackable theMob : mobList)
{
if (theMob.isDead())
{
mobList.remove(mobList.indexOf(theMob));
continue;
}
} |
EDIT: the list is not empty and i am able to complete any other operations with the objects found in the list than remove().
|
|
Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Mon Aug 23, 2010 12:43 pm Post subject: |
|
|
I'm not sure how Java handles iterators, but surely every time you attempt to remove something from the list the iterator being used (in the for loop) is being invalidated. Consider adding all of the mobs to be despawned to a separate list, and then afterwards removing them from the original list.
|
|
Back to top |
|
 |
XSV GTH Moderator
Reputation: 9
Joined: 12 Oct 2005 Posts: 1007 Location: USA
|
Posted: Mon Aug 23, 2010 1:52 pm Post subject: |
|
|
Maybe stick to basics and use a Loop like cometjack suggested, instead of a It.
//Original
for(L2Attackable theMob : mobList)
{
if (theMob.isDead())
{
mobList.remove(theMob);
continue;
}
}
my 2 cents:
//New
for(i=theMob.size();i>0;i--)
{
if (theMob.get(i).isDead())
{
mobList.get(i).remove();
continue;
}
}
sorry if syntax is wrong, haven't used java lately. But you get the idea Xd
_________________
Last edited by XSV on Mon Aug 23, 2010 3:22 pm; edited 3 times in total |
|
Back to top |
|
 |
TROLOLOLOLOLOLOLOLOLOLOLO Expert Cheater
Reputation: -1
Joined: 27 Dec 2009 Posts: 100
|
Posted: Mon Aug 23, 2010 1:54 pm Post subject: |
|
|
Maybe try:
Code: | for(L2Attackable theMob : mobList)
{
if (theMob.isDead())
{
mobList.remove((int) mobList.get(theMob));
continue;
}
} |
EDIT: Yeah I removed it, I didn't think it would work lol.
EDIT2: The loop I suggested was:
Code: | for (short s = mobList.size(); s > 0; s--)
{
if (theMob[s].isDead()) mobList.remove(theMob[s]);
} |
Like I said not sure if that will work, but eh, you get the idea.
|
|
Back to top |
|
 |
kot1990 Expert Cheater
Reputation: 1
Joined: 06 Sep 2009 Posts: 131 Location: Greece
|
Posted: Mon Aug 23, 2010 2:26 pm Post subject: |
|
|
thank you, I will try all that and then post..
|
|
Back to top |
|
 |
TROLOLOLOLOLOLOLOLOLOLOLO Expert Cheater
Reputation: -1
Joined: 27 Dec 2009 Posts: 100
|
Posted: Mon Aug 23, 2010 3:20 pm Post subject: |
|
|
kot1990 wrote: | thank you, I will try all that and then post.. |
Eh, np Post if you get any errors. Not sure in that case if I can change an Object to an int, but we'll see xD
|
|
Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Mon Aug 23, 2010 3:43 pm Post subject: |
|
|
Haven't had time to read the question properly but using iterators or enhanced for loops or even regular forward looping and removing items will fuck it up. You can go backward instead though.
Code: | for( int i = arr.length - 1; i >= 0; i-- )
if( lala )
arr[i].remove(); |
|
|
Back to top |
|
 |
kot1990 Expert Cheater
Reputation: 1
Joined: 06 Sep 2009 Posts: 131 Location: Greece
|
Posted: Tue Aug 24, 2010 7:53 am Post subject: |
|
|
Solved. Thanks for all help, but now I have another strange issue here. As you know java has Garbage Collection. I created a class that initiates 1.000.000 integers (close to 4MB), then in main created 2 objects of this class (close to 8MB). But the garbage colletion didn't work as I removed any references to these 2 objects.
Code: |
public class animal
{
int[] myArray;
animal()
{
myArray = new int[1000000];
}
}
import java.util.*;
import javax.swing.*;
public class arraytest
{
public static void main(String[] args)
{
animal dog = new animal(); //+4MB
JOptionPane.showInputDialog("go");
animal cat = new animal(); //+4MB
JOptionPane.showInputDialog("go");
dog = null; //still 8MB
JOptionPane.showInputDialog("go");
cat = null; //still 8MB!!!
JOptionPane.showInputDialog("go");
}
}
|
I was looking at the task manager for memory consumption..
|
|
Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Tue Aug 24, 2010 4:33 pm Post subject: |
|
|
The garbage collector is not as simple as you think it is. It is not the case that an object having no references to it is immediately garbage collected. Rather, it means that it can be garbage collected by the system at any point from then on. A lot of garbage collectors do not operate immediately once the reference count goes down to 0. In some cases, the extra memory is simply not needed and won't be needed for quite a while so it will just slow the process down to continuously free these regions of memory. In most languages you can force garbage collection or at least hint to the garbage collector that you want it to do some work. In Java, you can do it with System.gc(). However I would advise you not to do this since you'll probably end up slowing it down. Don't worry about the collection. Someone has gone and put a lot of time into designing it so it balances between reclaiming memory and wasting computation. If you fiddle with it, you'll most likely slow it down.
|
|
Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Tue Aug 24, 2010 7:08 pm Post subject: |
|
|
As an extension for what Slugsnack said, the garbage collector will most likely not bother freeing of that extra 4MB of space anyways. Instead of reallocating more space for new objects, they will use this space and simply write over the old objects as there is an overhead to allocating memory.
|
|
Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Tue Aug 24, 2010 8:05 pm Post subject: |
|
|
I've had some weird experience with the gc where code was malfunctioning if i didn't call the gc. Probably a bad feature on my part (even though anyone i showed the code too didn't have a clue) but calling the gc was good enough.
_________________
|
|
Back to top |
|
 |
kot1990 Expert Cheater
Reputation: 1
Joined: 06 Sep 2009 Posts: 131 Location: Greece
|
Posted: Wed Aug 25, 2010 10:11 am Post subject: |
|
|
thanks for all replies, but how will I be sure that something will be garbage collected? I read somewhere that I have to remove all references and then it will be collected. However in my case I forced the gc with System.gc() and it didn't work the way I did it. I also tried to do it like this.
Code: |
public class animal
{
int[] myArray;
void eatMemory()
{
myArray = new int[5000000];
}
void clear()
{
myArray = null;
}
}
public class arraytest
{
public static void main(String[] args)
{
JOptionPane.showInputDialog("go");
animal dog = new animal();
dog.eatMemory();
JOptionPane.showInputDialog("go");
animal cat = new animal();
cat.eatMemory();
JOptionPane.showInputDialog("go");
cat.clear();
cat = null;
JOptionPane.showInputDialog("go");
dog.clear();
dog = null;
System.gc();
JOptionPane.showInputDialog("go");
}
}
|
Because I am working now on a java application that will have many connections, the objects that will be created will be too many. So I don't want to run out of memory because of some objects that are not removed.
Last edited by kot1990 on Wed Aug 25, 2010 10:59 am; edited 1 time in total |
|
Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Wed Aug 25, 2010 10:58 am Post subject: |
|
|
kot1990 wrote: | thanks for all replies, but how will I be sure that something will be garbage collected? |
The whole point of garbage collection is so that you don't have to worry about the garbage.
So don't worry about it.
|
|
Back to top |
|
 |
kot1990 Expert Cheater
Reputation: 1
Joined: 06 Sep 2009 Posts: 131 Location: Greece
|
Posted: Wed Aug 25, 2010 11:00 am Post subject: |
|
|
Flyte wrote: | kot1990 wrote: | thanks for all replies, but how will I be sure that something will be garbage collected? |
The whole point of garbage collection is so that you don't have to worry about the garbage.
So don't worry about it. |
ok, I will continue then and see the results.
|
|
Back to top |
|
 |
|