• ArrayList in JAVA

    Fierro Member

    I have never used arraylists before … I am trying to use each of the methods walls, update, drawMe and attractTo for each object in the arraylist.

    How do I do it? What I have here is obviously not the way.

    listSize = particle_list.size();
    
    for (int i = 0; i < listSize; i++)
          {
             particle_list.attractTo( ourPlayer );
             particle_list.walls();
             particle_list.update(i);
             particle_list.drawMe();
          }
    
  • ShikhaTan Member

    In order to access an element within an arraylist, you have to do arraylistname.get(index);

    For your instance, what would probably make it work is the following:

    
    listSize = particle_list.size();
    
    for (int i = 0; i < listSize; i++)
    {
             particle_list.get(i).attractTo( ourPlayer );
             particle_list.get(i).walls();
             particle_list.get(i).update(i);
             particle_list.get(i).drawMe();
    }
    
  • SapnaVishwas Member

    Also it isn’t necessary to create a variable just to store the list size.
    Why not just compare i with particle_list.size(); ?

    But as stated you need to use the .get() method.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.
en_USEnglish