Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: ArrayList problem, probably very simple.

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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);
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ArrayList problem, probably very simple.

    Please explain what the problem is. Show the program's output and explain what is wrong with it and what you want it to be.
    exact same
    Why do you expect them to be different?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList problem, probably very simple.

    Quote Originally Posted by Norm View Post
    Please explain what the problem is. Show the program's output and explain what is wrong with it and what you want it to be.

    Why do you expect them to be different?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.



    Hi, and thanks for the fast reply. I don't understand why they would end up the same, unless I've missed something. The reason is that I only add 5 variables to the ArrayList called infectedpercell, and I don't even add anything to healthypercell, since I only run the loop 5 times. I've been trying to debug this code for hours now.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ArrayList problem, probably very simple.

    Can you post the program's output that shows what you are talking about?

    			healthypercell.add(nr); //<<<<<<< adds something here
    I don't even add anything to healthypercell
    Did you try using Find to see it add() was called?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList problem, probably very simple.

    Quote Originally Posted by Norm View Post
    Can you post the program's output that shows what you are talking about?

    			healthypercell.add(nr); //<<<<<<< adds something here

    Did you try using Find to see it add() was called?
    That is where I initiliaze the ArrayList<ArrayList<Integer>> with zeros. I don't really understand what you mean by using Find, but I'll google it.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ArrayList problem, probably very simple.

    what you mean by using Find
    "Find" is often a menu item in and editor that is used to find a String in the text of the file being edited.
    I used it to find the source line I posted.

    You still need to do this:
    Can you post the program's output that shows what you are talking about?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList problem, probably very simple.

    Quote Originally Posted by Norm View Post
    "Find" is often a menu item in and editor that is used to find a String in the text of the file being edited.
    I used it to find the source line I posted.

    You still need to do this:
    Can you post the program's output that shows what you are talking about?
    Oh, that find. Yeah but it was only called to initiate to zeros. The output is an ArrayList<ArrayList<Integer>> that shows the number of occurances in each cell. Since there are 10.000 cells, I will just give an example.

    [[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] (First ArrayList healthypercell)
    [[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] (Second ArrayList infectedpercell)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ArrayList problem, probably very simple.

    Where are the contents of the two arraylists printed out?
    For debugging: Ideally their contents should be printed before and changes made and after and changes are made so the results of the code between the two print statements can be seen.

    I will just give an example.
    Is that what was printed by the program or something you made up using copy and paste of bits here and there? It needs to be exactly what the program printed, not copied and pasted bits and pieces.

    Print the contents of the arraylists at the start of the Model constructor and print them again at the end to show what changes the constructor made to their contents.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList problem, probably very simple.

    Quote Originally Posted by Norm View Post
    Where are the contents of the two arraylists printed out?
    For debugging: Ideally their contents should be printed before and changes made and after and changes are made so the results of the code between the two print statements can be seen.


    Is that what was printed by the program or something you made up using copy and paste of bits here and there? It needs to be exactly what the program printed, not copied and pasted bits and pieces.

    Print the contents of the arraylists at the start of the Model constructor and print them again at the end to show what changes the constructor made to their contents.
    Hi. Thanks for your time, but my mate found the error. I used the same ArrayList in all ArrayList<ArrayList>

    infectedpercell.add(nr);
    curedpercell.add(nr);
    healthypercell.add(nr);

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ArrayList problem, probably very simple.

    Cross posted at: ArrayList problem, probably very simple.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Arraylist indexof problem
    By spydevil in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 31st, 2012, 03:47 PM
  2. ArrayList problem
    By Renhik in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 8th, 2012, 11:48 AM
  3. ArrayList Sorting Problem
    By coke32 in forum Object Oriented Programming
    Replies: 3
    Last Post: April 29th, 2012, 08:53 AM
  4. Problem with ArrayList
    By waltersk20 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2010, 08:37 PM
  5. ArrayList Problem
    By Marty in forum Collections and Generics
    Replies: 16
    Last Post: August 31st, 2010, 03:47 AM