2

i'm out of my league.

ok so if i can do this:

int[]teams = new int[3];
for (int i = 0; i < 3; i++)
{
teams[i] = i;
}

how do i do the similar thing but naming multiple arrays, ie

for (int i = 0; i < 3; i++)
{
int[]i = new int[3];
}

I have read you can't name an array with a variable so I can't just see how to produce arrays with different names (basically more than one) using a loop.

Thanks

3
  • 5
    Please clarify your question as it doesn't make sense (at least to me). What exactly are you trying to do and why? And which programming language are you using? C++ or Java? They're definitely not the same. Commented Jan 9, 2011 at 20:46
  • sorry its not very clear, im using java, and this is just a little example i put together. i am basically trying to create a set of arrays named with names read from a list. Commented Jan 9, 2011 at 20:54
  • 2
    If you are using Java why did you tag it as C++? It's almost as if you want to get answers that are of no use to you. Commented Jan 9, 2011 at 20:56

3 Answers 3

3

You'd do the following (Java):

int teams[][] = new teams[3][3]

You'd do the following (C++):

int** teams = new int*[3];
for(int i=0; i<3; i++){
   teams[i] = new int[3];
}

or you could just do

int teams[3][3];
for(int i=0; i<3; i++){
    for(int j=0; j<3; j++){
        teams[i][j] = //whatever you want
    }
}

Edit in response to your comment below:

What you're looking for is a MultiMap. There you're going to get:

MultiMap foo = new MultiMap();

foo.put("Bob", 1);
foo.put("Bob", 2);

etc...

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

3 Comments

ah that was silly of me, eventually the name of the array will need to be a string. so i cant just use it as an index on a 2d array...
@outofmyleague what do you mean by "name of the array"? Do you mean you'd like to look-up the integer via a string?
i want to create set of arrays ie String[]john=new String[3]; String[]mark = newString[3]; from a list of names from text file.
2

You can make an array of arrays (sometimes called a multidimensional array):

int [][] arr = new int[137][42];

1 Comment

as mentioned above naming from an int was just because it was easy to show it changing in a couple of lines of code, it will eventually be from a String. had thought of putting all the names into a string array of their own then indexof() to get the index no.....
1

You cannot dynamically generate variable names, but you can achieve the same effect with a Map:

//Store mappings from array name (String) to int arrays (int[])
Map<String, int[]> namedArrays = new HashMap<String, int[]>();
for (int i = 0; i < 3; i++)
{
  //This is going to be the name of your new array
  String arrayName = String.valueOf(i); 
  //Map an new int[] to this name
  namedArrays.put(arrayName, new int[3]);
}

//If you need to access array called "2" do
int[] array2 = namedArrays.get("2")

The advantage of doing it this way is that you can have multiple arrays with same names as long as they are in different maps. But note that when you map two or more arrays to the same name in the same map, the previous array will be overriden (lost).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.