java - Naming a variable with a variable -


i'm working on assignment uses oop. program create bunch of different lemurs based on user puts in. share similar traits parent class , own unique traits in own class.

part of program allow user decide number of objects (or lemurs) created. want create objects in progressive manner, l1, l2, l3... etc.

here's have far. wanted use lemcounter keep track of lemur number , attach object name, every time new object created.

//main section of code  static int numlems, typelems, loop, lemcounter; static string alllems[]; public static void main(string[] args) {     //ask number of lemurs     asknl();     //initalize length of array total number of lemurs     alllems = new string[numlems];     //ask lemurs user wants generate     //set lemur counter , lemur string nothing     lemcounter = 0;     for(int = 0; < numlems; i++){         //run method asks lemur want         asktl();         //run method check lemur user wanted         checktl();         //use lemcounter keep track of lemur number         lemcounter++;     } }   //method asking how many lemurs, sake of cleaniness in main method  static int asknl(){     do{         try{             string numlemsstr = joptionpane.showinputdialog("how many lemurs generate?");             numlems = integer.parseint(numlemsstr);             loop = 2;         }         catch(numberformatexception e){             joptionpane.showmessagedialog(null, "not acceptable input, please try again.");             loop = 1;         }     }while(loop==1);     return numlems; }  //method asking type of lemur  static int asktl(){     do{         try{             string typelemsstr = joptionpane.showinputdialog("what type of lemur lemur "+ lemcounter+1                     + "\n1 - tree lemur"                     + "\n2 - desert lemur"                     + "\n3 - jungle lemur");             typelems = integer.parseint(typelemsstr);             if(typelems > 3){                 joptionpane.showmessagedialog(null, "not acceptable input, please try again.");                 loop = 1;             }             else{                 loop = 2;             }         }         catch(numberformatexception e){             joptionpane.showmessagedialog(null, "not acceptable input, please try again.");             loop = 1;         }     }while(loop==1);     return typelems; }  //method decide lemur user wanted static string[] checktl(){     if(typelems==1){ //i'm not sure need put in name proceed linearly         treelemur l = new treelemur();     }     return alllems; } 

don't confuse variable name exists isn't important you'd think object "name" doesn't exist. instance, give variable name based on number , had:

lemur lemur1 = new lemur(); lemur lemur2 = lemur1; 

then "name" of lemur object being "named" here? lemur1 or lemur2? note both refer the same lemur object.

use array or arraylist<lemur> if want sequential collection of lemurs can accessed number. use map<string, lemurl> if want have lemur associated string.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -