Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Thomas van den Berg
914 PointsHow do I create a variable using an string following an interger as name?
I am trying to write a maze solver for fun in java, but as the maze gets bigger, the options also grow, so my question is: Can I do something like:
class MazeSolverOptions {
public static void main(String[] args){
for(int currentCount=1; currentCount<110; currentCount++){
String solutioncurrentCount = "";
}
int interger = 25
solutioninterger = "Hello"
}
}
2 Answers
andren
28,558 PointsNo you cannot programmatically generate variables. But you can store multiple values within a single variable by using either Arrays or Lists. You can also use a Map, which let's you store things in key-value pairs. You can learn about all three of those things (and other data structures) by going though the Java Data Structures course.
Samuel Ferree
31,723 PointsOne of the beginner java courses should cover arrays
String[] solutions = new String[50]; //create an array of 50 strings
int current = 10;
solutions[current] = "some string"; //set the string at index 10 to be 'some string'