1

I want to create 10 arrays inside a for loop, using the names 1, 2, 3, ..., 10 for the arrays.

I tried like this, but it's not working:

int n = 10;
for(int i = 0; i < n; i++)
{
    String [] i = new String[];
}
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Prasanth
  • 51
  • 1
  • 3
  • 10

6 Answers6

5
int n = 10;
int m = 5;

String[][] arrayOfArrays = new String[n][];

for(int i=0;i<n;i++)
{
    arrayOfArrays[i] = new String[m];
}
Khaled.K
  • 5,828
  • 1
  • 33
  • 51
2

You should use a Map to map number with array

Map<Integer,String[]> map = new HashMap<>(10);
for(int i=0; i < n; i++)
{
   map.put(i,new String[10]);
}
femtoRgon
  • 32,893
  • 7
  • 60
  • 87
Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
  • please fix that `o` from the OPs code and make it a `0` its bothering me. – leigero Mar 11 '14 at 08:10
  • 2
    He didn't ask for a map – Khaled.K Mar 11 '14 at 08:10
  • 4
    @KhaledAKhunaifer He asked for how to dynamically name variables based on the value of that iterator, which isn't supported. A map is an appropriate solution. – femtoRgon Mar 11 '14 at 08:12
  • 1
    @Youngistan I disagree. This is just about the only way to generate a bunch of anonymous variables. You either have to name them (obviously the OP can't or doesn't want to) or store them in a list or map and access their index. – leigero Mar 11 '14 at 08:14
  • ok, if i do like this how to get the value of 2nd index of array name 2 – Prasanth Mar 11 '14 at 08:20
  • @Prasanth String val = map.get(2)[1] – Kumar Abhinav Mar 11 '14 at 08:21
  • @leigero then why to use map simply go with Set,List then there is so many solutions – Kick Mar 11 '14 at 08:24
  • 2
    @Youngistan what collection you use is irrelevant. The fact of the matter is that he needs to store these in some sort of collection and they can't be created dynamically like he tried. Therefore, this is very much a proper answer. – leigero Mar 11 '14 at 08:29
0

you can't declare a variable with leading numbers.

Mc Kevin
  • 962
  • 10
  • 31
  • 2
    This is not the issue at all. While what you say it true, a variable cannot be used as a variable name in Java - it is simply not supported by the language. – Boris the Spider Mar 11 '14 at 08:10
  • @BoristheSpider OP claimed that he wanted arrays with names 1,2 and 3, so I think this answer is more related than the fact that a variable cannot be used as a variable name in Java – eis Mar 11 '14 at 08:13
  • @McKevin I think what Boris is trying to say... is that while your statement is true, its not an answer to this question, and has little at all to do with the issue at hand. – leigero Mar 11 '14 at 08:16
0

In you code i is already defined within the scope of the for loop. Also as soon as you exit the loop, the created variables will be out of scope.

Furthermore, variables beginning with an Integer are invalid in Java.

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

You can use ArrayList for creating an array of array, else go for a 2D String array.

ArrayList<String[]> x = new ArrayList<String[]>();
    int n =10;
    for(int i=0;i<n;i++){
        x.add(new String[5]);
    }
MjZac
  • 3,476
  • 1
  • 17
  • 28
0

you might want to use a 2D array which might offers what you need.

Read this Syntax for creating a two-dimensional array

String [][] test = new String [10][10];

It is as if the first [] can be your 'i' like what you required, and the 2nd [] can be what you need to store the variable. It is normally use for cases like if you need an "array of array", meaning 100x array.

Community
  • 1
  • 1
Sky
  • 3,350
  • 2
  • 14
  • 12