Results 1 to 4 of 4
  1. #1
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default ArrayList problem, probably very simple.

    Hi.
    I hope you can help me with debugging this piece of code, cause I'm going crazy. I've only implemented the code needed. 'public Model' is the constructor. I have highlighted where the problem is.




    private ArrayList<ArrayList<Integer>> healthypercell = new ArrayList<ArrayList<Integer>>();
    private ArrayList<ArrayList<Integer>> curedpercell = new ArrayList<ArrayList<Integer>>();

    public Model (){
    for (int i = 0; i < rowcol; i++) {
    ArrayList<Character> chr = new ArrayList<Character>();
    ArrayList<Integer> nr = new ArrayList<Integer>();
    //ArrayList<ArrayList<Agent>> tempag = new ArrayList<ArrayList<Agent>>();
    for (int j = 0; j < rowcol; j++) {
    chr.add(input);
    nr.add(0);
    }
    infectedpercell.add(nr);
    curedpercell.add(nr);
    healthypercell.add(nr);
    lattices.add(chr);
    }

    //System.out.println("hej");
    for (int i = 0; i < 5; i++) {
    System.out.println("hej");
    if (infected > 0) {
    sirsend = 'i';
    }
    else {
    sirsend = 's';
    }
    agx = (int)(Math.random()*100);
    agy = (int)(Math.random()*100);
    agents.add(new Agent(agx,agy,sirsend));
    if (infected <= 0) {
    temp = healthypercell.get(agx).get(agy);
    temp2 = temp+1;
    healthypercell.get(agx).set(agy, temp2);
    }
    else if (infected > 0) {
    //System.out.println(infectedpercell);
    counter += 1;
    temp = infectedpercell.get(agx).get(agy);
    temp2 = temp+1;
    //System.out.println(temp2);
    infectedpercell.get(agx).set(agy, temp2);
    //if (counter == 5)


    }
    if (infected >= 0)
    infected -= 1;
    }

    //System.out.println(infectedpercell); Somehow both infectedpercell and healthypercell ends up with the exact same Arraylists.
    //System.out.println(healthypercell);
    }
    Last edited by tigertomas; 11-04-2013 at 09:49 PM.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: ArrayList problem, probably very simple.

    Your two ArrayLists are Lists of Lists. And you are adding the same reference to both of them.

    Java Code:
    infectedpercell.add(nr);//<--------
    curedpercell.add(nr);
    healthypercell.add(nr);//<---------
    lattices.add(chr);
    Since nr is the same for both, when you update one, the other gets updated too.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default Re: ArrayList problem, probably very simple.

    Quote Originally Posted by jim829 View Post
    Your two ArrayLists are Lists of Lists. And you are adding the same reference to both of them.

    Java Code:
    infectedpercell.add(nr);//<--------
    curedpercell.add(nr);
    healthypercell.add(nr);//<---------
    lattices.add(chr);
    Since nr is the same for both, when you update one, the other gets updated too.

    Regards,
    Jim
    Wow, that was the problem. Nice job man, and thanks a lot!! Have a good one. I really appreciate the help :):

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: ArrayList problem, probably very simple.

    That's not it completely. The problem is that the nr arraylist is the one that is updated, not the infectedpercell arraylist:
    Java Code:
    infectedpercell.get(agx).set(agy, temp2);  //  note: get() returns the nr arraylist which is updated
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Java: simple ArrayList question
    By bronzefish in forum New To Java
    Replies: 7
    Last Post: 01-23-2013, 01:47 PM
  2. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  3. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 05:12 AM
  4. Simple ArrayList Q
    By melbourn1te in forum New To Java
    Replies: 2
    Last Post: 04-05-2011, 01:57 AM
  5. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM

Posting Permissions

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