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[];
}
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[];
}
int n = 10;
int m = 5;
String[][] arrayOfArrays = new String[n][];
for(int i=0;i<n;i++)
{
arrayOfArrays[i] = new String[m];
}
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]);
}
you can't declare a variable with leading numbers.
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.
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]);
}
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.