How to create variable dynamically?

Asked

Viewed 930 times

2

I would like to create a variable for each iteration of a for. Example:

for(u=0;u<tamanho;u++)
{
  int variavel[u] = "valor"
}

It is possible?

UPDATE

This is the real code.

  SearchConstraint c1 = factory.createConstraint("metadata#version", f1, f1, ConstraintType.MUST);
  SearchConstraint c2 = factory.createConstraint("regiao_codigo", "1", "1", ConstraintType.MUST);
  SearchConstraint[] constraints = new SearchConstraint[2];
  constraints[0] = c1;
  constraints[1] = c2;
  dataset = factory.getDataset("ibv", null, constraints, null);

Note that it has two variables C1 and C2. I need to create them dynamically.

  • I need to create "u" number of variables with each for iteration. It can’t be vector, I have to create distinct variables. The code is just an example.

  • If you’re placing C1 and C2 inside a vector at the end of the accounts, why can’t you use that vector to create them in your iteration ?

  • 1

    You know you can create a vector using a variable as size, right? new SearchConstraint[u]

  • Is Searchconstraint a Java type? I think this Searchconstraint is the tool I use.

  • Searchconstraint is either Fluig or is ECM 3.0

  • gives yes @mgibsonbr, I got.

  • 1

    @Rafaeltscs that simple information of yours cleared my mind. I did. Turn it into an answer for me to accept it.

Show 2 more comments

1 answer

6


Through this snippet of your code:

constraints[0] = c1;
constraints[1] = c2;

I realize you’re putting the variables C1 and C2 into a vector anyway. So, using your example code, why not create these variables already within this vector?

for(u=0;u<tamanho;u++)
{
    constraints[u] = "valor"
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.