Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Any java expert for this??...

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
kot1990
Expert Cheater
Reputation: 1

Joined: 06 Sep 2009
Posts: 131
Location: Greece

PostPosted: Mon Aug 23, 2010 10:21 am    Post subject: Any java expert for this??... Reply with quote

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
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Mon Aug 23, 2010 12:43 pm    Post subject: Reply with quote

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
View user's profile Send private message
XSV
GTH Moderator
Reputation: 9

Joined: 12 Oct 2005
Posts: 1007
Location: USA

PostPosted: Mon Aug 23, 2010 1:52 pm    Post subject: This post has 1 review(s) Reply with quote

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
View user's profile Send private message
TROLOLOLOLOLOLOLOLOLOLOLO
Expert Cheater
Reputation: -1

Joined: 27 Dec 2009
Posts: 100

PostPosted: Mon Aug 23, 2010 1:54 pm    Post subject: Reply with quote

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
View user's profile Send private message
kot1990
Expert Cheater
Reputation: 1

Joined: 06 Sep 2009
Posts: 131
Location: Greece

PostPosted: Mon Aug 23, 2010 2:26 pm    Post subject: Reply with quote

thank you, I will try all that and then post..
Back to top
View user's profile Send private message
TROLOLOLOLOLOLOLOLOLOLOLO
Expert Cheater
Reputation: -1

Joined: 27 Dec 2009
Posts: 100

PostPosted: Mon Aug 23, 2010 3:20 pm    Post subject: Reply with quote

kot1990 wrote:
thank you, I will try all that and then post..


Eh, np Smile 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
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Mon Aug 23, 2010 3:43 pm    Post subject: Reply with quote

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
View user's profile Send private message
kot1990
Expert Cheater
Reputation: 1

Joined: 06 Sep 2009
Posts: 131
Location: Greece

PostPosted: Tue Aug 24, 2010 7:53 am    Post subject: Reply with quote

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
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Tue Aug 24, 2010 4:33 pm    Post subject: Reply with quote

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
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Tue Aug 24, 2010 7:08 pm    Post subject: Reply with quote

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
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Tue Aug 24, 2010 8:05 pm    Post subject: Reply with quote

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
View user's profile Send private message
kot1990
Expert Cheater
Reputation: 1

Joined: 06 Sep 2009
Posts: 131
Location: Greece

PostPosted: Wed Aug 25, 2010 10:11 am    Post subject: Reply with quote

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
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Wed Aug 25, 2010 10:58 am    Post subject: Reply with quote

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
View user's profile Send private message
kot1990
Expert Cheater
Reputation: 1

Joined: 06 Sep 2009
Posts: 131
Location: Greece

PostPosted: Wed Aug 25, 2010 11:00 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites