Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2011
    Rep Power

    Default ArrayList, incrementing and decrementing fields

    I am trying to figure out how to change values stored in an arraylist. I have managed to get the values out, but the problem I am having now is I use the for each loop to go through the list and find certain values. I have that figured out, however I cannnot seem to change specific values in the list. Can anyone explain how to do this?

    I have an arraylist of objects (Beepers) that store integers denoting the street and avenue that piles of beepers are on. I can get the information but I now I can't seem to get it to use the specific field I want. I don't understand what it's doing. Sure would appreciate some help with this. There are other files used with this but they just call the methods here in this program.

    Java Code:
    import java.util.ArrayList;
    public class MyWorld{
    /************** declare variables ****************/
    	 ArrayList<Beepers> beeperList = new ArrayList<Beepers>();
    	 boolean found=false;
    	 int index;
    /*********** Constructor ************/
    public MyWorld(){ /* add walls and beepers here */
    	beeperList.add(new Beepers (10,101,55));
    	beeperList.add(new Beepers (1,1,100));
          /********** Inner Class "Beepers" **************/
    		public class Beepers{
    		/******** declare variables *************/
    		private int street, avenue, count;
    			public Beepers(int s, int a, int c){ // Beepers class constructor
    		/************Beeper Class Methods ********************************/
    			public int getStreet(){
    				return street;
                            public int getAvenue(){
    				return avenue;
                            public int getCount(){
    				return count;
                            public int incrementCount(){
    				return count;
                            public int decrementCount(){
    				return count;
    			public  String toString(){
    				return "  St: " +beeperList.get(0).getStreet() +" Ave: " + avenue +" Count "+count;
    	public boolean found(int s, int a){ System.out.println("*** found() ***");
    					int karelStreet=s;
    					int karelAvenue=a;
    					for(Beepers e : beeperList){
    						if (e.getStreet()==karelStreet && e.getAvenue()==karelAvenue){
    							int index = beeperList.indexOf(e);  System.out.println("index= "+index);
    							found = true;
    		return found;
    	public void pickBeeper(int s, int a){ System.out.println("*** World pickBeeper() ***");
    		if(found(s, a)){
    Last edited by Norm; 11-07-2011 at 10:49 PM. Reason: added code tags

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Washington, DC
    Rep Power

    Default Re: ArrayList, incrementing and decrementing fields

    I don't think you're using the index variable very clearly. You have a class variable, but then a different local variable with the same name.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    Join Date
    Nov 2011
    Rep Power

    Default Re: ArrayList, incrementing and decrementing fields

    ok I tried so many things I don't even remember what i did. When it searches the arraylist for a match I want it to stop and use that object. How do I do that?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: ArrayList, incrementing and decrementing fields

    Save the reference to the found object in a variable that you can work with.

Similar Threads

  1. Replies: 8
    Last Post: 08-13-2011, 07:02 PM
  2. Incrementing by more than 1
    By davidq23 in forum New To Java
    Replies: 2
    Last Post: 07-27-2011, 02:12 AM
  3. Help with incrementing numbers in a GUI
    By jdg951 in forum New To Java
    Replies: 6
    Last Post: 01-18-2011, 04:23 AM
  4. Incrementing and loops
    By Rubidoux in forum New To Java
    Replies: 1
    Last Post: 04-24-2010, 03:04 PM
  5. Arraylist errors with private fields
    By DJRome in forum New To Java
    Replies: 5
    Last Post: 03-02-2010, 01:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts