0

I'm writing a program that has an ArrayList<String> s. Is there a way to get each value from the ArrayList and set them to separate variables? For example:

String s0 = s.get(0);
String s1 = s.get(1);
String s2 = s.get(2);
.
.
.
String sn = s.get(n);

I will know what 'n' is but it will change based on user input. I just don't know how to/if I can dynamically create variables based on some number n. Any help would be much appreciated!

3
  • How would you reference a dynamically created variable? It wouldn't exist at compile time. Commented Nov 7, 2013 at 19:31
  • Why would you want to do this? Just get the value from the arraylist when you need it. What are you actually trying to acccomplish? Commented Nov 7, 2013 at 19:35
  • I'm trying to calculate the number of differences between each combination of strings in my ArrayList and put those in a 2d array. For example s1 = AAA s2 = ABA s3 = ABC s1s2 = 1 s1s3 = 2 s2s3 = 1 and then put those values into a difference matrix. I guess I can just compare the values as I fill the matrix based on i and j since [0][0] would be s1s1 [0][1] would be s1s2 ect. I just didn't know if there was an easier way Commented Nov 7, 2013 at 19:41

1 Answer 1

6

You cannot. This is what arrays (and ArrayLists) are for!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.