Results 1 to 2 of 2
- 07-07-2012, 09:35 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 5
- Rep Power
- 0
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!
- 07-07-2012, 10:12 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Re: how to change an instance variable of an object from multiple arraylist reference
Carefully check line #10 in your first class again:
It doesn't do what you think it does ...Java Code:testIndex = testIndex++;
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
How to change object attributes in an ArrayList
By L3v3L in forum New To JavaReplies: 1Last Post: 05-11-2012, 04:31 AM -
Creating an instance variable in one class that connects to another instance variable
By SpicyElectricity in forum New To JavaReplies: 1Last Post: 04-21-2012, 06:03 PM -
Elementary question on variable references
By Shayke_ in forum New To JavaReplies: 1Last Post: 02-09-2011, 04:35 PM -
Multiple references to the same object
By jlowery2663 in forum Advanced JavaReplies: 6Last Post: 08-31-2009, 07:40 AM -
Help me finish an instance method which references a class variable?
By trueblue in forum New To JavaReplies: 20Last Post: 06-03-2009, 05:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks