Results 1 to 2 of 2
  1. #1
    smande3 is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default how to change an instance variable of an object from multiple arraylist references?

    I can not figure out why i dont get the output when i compile and run

    1
    2
    3

    Instead i get

    0
    0
    0

    I'm trying to figure out how to build an array list of objects than build a sub-arraylist off of that one and be able to use the reference on either array to edit one of the objects instance variables.

    so i made this as a very basic example as a way to test before i wrote all the code and i cant even get the output to be correct for the first system.out.println which is basically using a getter setter method.

    Im new to java and im sure its a very basic rookie mistake that once pointed out ill understand but i see no issue in my code here it is.

    1. Test Class is supposed to make Test objects that have the instance variable testIndex

    Java Code:
    public class Test {
    	
    	int testIndex;
    
    	public Test() {
    		testIndex = 0;	
    		}
    	
    	public void incrementTestIndex() {
    		testIndex = testIndex++;
    		}
    	
    	public int getTestIndex() {
    		return testIndex;
    		}
    	}
    ---------------------------------------------------------------------
    2. This is the primary ArrayList which only has 1 element added for simplicity of my question

    Java Code:
    import java.util.ArrayList;
    
    public class Primary {
    
    ArrayList<Test> primary;
    	
    	public Primary() {
    		primary = new ArrayList<Test>();
    		}
    
    	public void addTestToPrimary(Test a) {
    		primary.add(a);
    		}
    	
    	public Test getTest(int a) {
    		Test primaryTest = primary.get(a);
    		return primaryTest;
    		}
    }
    ---------------------------------------------------------------------------------
    3. this is supposed to hold the Test object passed from the primary ArrayList

    Java Code:
    import java.util.ArrayList;
    
    public class Pass {
    	
    ArrayList<Test> pass;
    
    public Pass() {
    	pass = new ArrayList<Test>();
    	}
    public void addTestToPass(Test a) {
    	pass.add(a);
    	}
    public Test getTest(int a) {
    	Test passTest = pass.get(a);
    	return passTest;
    	}
    
    }
    -----------------------------------------------------------------

    4. here is the main method it should first

    Java Code:
    import java.util.ArrayList;
    
    public class Main {
    
    	public static void main (String[] args) {
    		Test test = new Test();
    		test.incrementTestIndex();
    		System.out.println(test.getTestIndex());
    		Primary primaryReference = new Primary();
    		primaryReference.addTestToPrimary(test);
    		primaryReference.getTest(0).incrementTestIndex();
    		System.out.println(test.getTestIndex());
    		Pass passReference = new Pass();
    		passReference.addTestToPass(primaryReference.getTest(0));
    		passReference.getTest(0).incrementTestIndex();
    		System.out.println(test.getTestIndex());
    		
    	}
    }

    I need each arraylist in Primary and Passed to be in there own class for when i make the actual application im trying to do what it actually is would be a texas hold em app. one array list for players at the table and a secondary list for players still in the hand...

    I need to be able to take all the players info from its player object pass it to Table ArrayList than pass it from table to active in hand.

    Please Help!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to change an instance variable of an object from multiple arraylist reference

    Carefully check line #10 in your first class again:

    Java Code:
    testIndex = testIndex++;
    It doesn't do what you think it does ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. How to change object attributes in an ArrayList
    By L3v3L in forum New To Java
    Replies: 1
    Last Post: 05-11-2012, 04:31 AM
  2. Replies: 1
    Last Post: 04-21-2012, 06:03 PM
  3. Elementary question on variable references
    By Shayke_ in forum New To Java
    Replies: 1
    Last Post: 02-09-2011, 04:35 PM
  4. Multiple references to the same object
    By jlowery2663 in forum Advanced Java
    Replies: 6
    Last Post: 08-31-2009, 07:40 AM
  5. Replies: 20
    Last Post: 06-03-2009, 05:33 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
  •