954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Increment Variable Name Inside Loop

Hi All! First thanks for viewing my thread. I have spent a half day and change trying to find a solution to this specific problem.
Basically I am trying to figure out how to change the variable name while in a loop. The last loop is where my problem is with some comment code describing a little bit about whats going on. Thanks for the help in advance.

//Class Constructor method
public Display(Main inMain, int numGui) {
System.out.println("Display(Main inMain)--from Display");
//place gui's inside an arraylist
guiArray.add(gui);
guiArray.add(gui2);
guiArray.add(gui3);
guiArray.add(gui4);


this.main = inMain;
//find the number of guis to delete out of four possible
int numDelete = -numGui + 4 ;

System.out.println("Start Size" + guiArray.size());
for(int j = 0; j

djdanjo82
Newbie Poster
18 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 
Basically I am trying to figure out how to change the variable name while in a loop.

Change the variable name of what? And why?guiArray.get(n) = new Gui(this);

You can't use get this way. I'm not sure what you are trying to do, but you can do this:

Gui someGUI = guiArray.get (n);


or this:

Gui someGUI = new Gui (this);
guiArray.set (n, someGUI);


What are you trying to do?

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

I am working on a fairly good size open source application so to display all the code is not sensible. Basically my part is: the user will choose how many GUI's to display on a very large GUI. ( The constructor method will bring in the "int numGui" variable which is "the number of GUI's from 1-4 he wants to see" Then from the GUI arraylist which already contains the max 4 GUI's the code will remove however many that is needed so the amount that is left over matches the user defined "int numGui".
That may sound a bit complicated. I will display the previous constructor code that works but is not scalable. ((I'm simply trying to figure out a dynamic way to display a user defined number of GUIs from an already existing GUI class" Hope that answers some of your questions. Either way your input makes me think out of the box which is what I need now. Thanks Again!
Dan


DmoDisplay(DmoMain inMain) {
System.out.println("DmoDisplay(DmoMain inMain)--from Display");

this.main = inMain;

//The below line is what I cannot replicate in my code
//dynamically through a loop .
//It is very possible there is a much simpler way of doing
//all of this
gui = new DmoGui(this);


}


In my previous post if I knew of a way to increment the variable name inside that loop I think I would be fine.
I thought the below line would work
guiArray.get(n) = new Gui(this);

Since the below line basically equals
gui = new DmoGui(this);

djdanjo82
Newbie Poster
18 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Also just so I do not confuse you any more.

DmoGui(this);
Gui(this);

Are the same thing but variable name changed.

djdanjo82
Newbie Poster
18 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 
I am working on a fairly good size open source application so to display all the code is not sensible. Basically my part is: the user will choose how many GUI's to display on a very large GUI. ( The constructor method will bring in the "int numGui" variable which is "the number of GUI's from 1-4 he wants to see" Then from the GUI arraylist which already contains the max 4 GUI's the code will remove however many that is needed so the amount that is left over matches the user defined "int numGui". That may sound a bit complicated. I will display the previous constructor code that works but is not scalable. ((I'm simply trying to figure out a dynamic way to display a user defined number of GUIs from an already existing GUI class"

This makes sense. I can visualize it.In my previous post if I knew of a way to increment the variable name inside that loop I think I would be fine.
I thought the below line would work
guiArray.get(n) = new Gui(this);

Since the below line basically equals
gui = new DmoGui(this);

"Increment the variable name" - I'm having trouble visualizing what you mean by this term. How do you increment a name, variable or otherwise? Does the name "aaa" turn into "aab", as in "increment" it alphabetically or something?Also just so I do not confuse you any more.

DmoGui(this);
Gui(this);

Are the same thing but variable name changed.

Thanks for trying not to confuse me more, but I think I am more confused. :confused: DmoGui and Gui aren't variable names above, they are class names.


Are you trying to accomplish this below?

ArrayList <Gui> GetGuisToDisplay (ArrayList<Gui> guis, int numGuis)

// assumes that guis contains four elements.  ArrayList returned should contain first numGuis elements of guis, which are the Guis to be displayed.  Assumes numGuis will be in the range of 0 to 4.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Misperception object reference & accessors.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You