0

Hi guys thanks for helping in advance this is not the real code it is just the idea i want to reach

for (int i = 1, i< some number ; i ++ )

float  [][] sts + i = new float[9][9];

The idea is to have 2 dimensional arrays with dynamic names initializing in a loop

sts1 sts2 sts3 . . . . .

fadi khzam
  • 13
  • 3

3 Answers3

2

For your problem, where naming is sequential based on the value of i, an ArrayList could do the job as you can iterate over it.

However, a more general approach that enables you accessing your arrays by some String name (even if this was random and not sequential as in your case) would be to use a Map<String, float[][]>, where the key String of the Map is the name you have given to your array.

Map<String, float[][]> myMap = new HashMap<String, float[][]>();

for(int i = 0; i < someNumber; ++i)
{
    myMap.put("sts" + i, new float[9][9]);
}

then access each array by myMap.get(_aName_);

Kostas Kryptos
  • 4,081
  • 2
  • 23
  • 24
1

If you create every 2 dimensional array in the loop, then the variabl (like sts1) will only be local to the loop. So after the loop the variable is out of scope (but I think you want to use them after the loop, that's why you want different names). So to use the created variables, you have to use an array. And if you use an array, the question of naming ceases.

ArrayList<float[][]> l = new ArrayList<float[][]>();

for(int i = 0; i < someNumber; ++i)
{
    l.add(new float[9][9]);
}
ProgrammingIsAwsome
  • 1,109
  • 7
  • 15
0

You can't create variable names dynamically in Java. Just do this

float[][][] sts = new float[someNumber][9][9];

Then you can use sts[0], sts[1] where you wanted to use sts1, sts2 etc.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116