I am interested in how to call a variable that changes, such as customer1
, customer2
, customer3
, etc. These customer variables are populated in this particular Class
. I would like the program to automatically type that number when I perform something such as customer1.returnName();
if the user types a 1
. without me making a case by case code. I would like the user to type the number of the customer and it automatically uses that number. What function is best for this?
Asked
Active
Viewed 106 times
0

robotlos
- 536
- 2
- 10
-
7Use an array, or a `Map
` – Elliott Frisch Mar 31 '16 at 23:59 -
1You don't want a "progression" with a variable, but rather you want an "association", an association of a number with a specific Customer. An array or ArrayList would work well too if your numbers are monotonically increasing always and are stable. – Hovercraft Full Of Eels Apr 01 '16 at 00:04
-
The solution from @ElliottFrisch is a good one. However, also see why this question is problematic at [Increment Variable Names](http://stackoverflow.com/questions/7762848/increment-variable-names), or [Generating variable name dynamically](http://stackoverflow.com/questions/13286516/generating-variable-name-dynamically) – KevinO Apr 01 '16 at 00:06
1 Answers
1
If you have numbered variables, then use a list. Something like
ArrayList<Customer> customers = new ArrayList<Customer>();
customers.add(new Customer("name"));
customers.get(0).returnName();

OneCricketeer
- 179,855
- 19
- 132
- 245