Change class vector size

1

I have to use the public static String vetor[] = new String [15]; vector in the class ... however this class is used in a servlet. And the size I need the vector is what the user chooses ... how can I pass the parameter that defines the vector size. I know that this 15 is only representative ... I need a variable, but how to declare a vector of the class using a variable that comes from the servlet?

    
asked by anonymous 02.10.2014 / 15:46

2 answers

5

You can solve this by instantiating the vector in the class constructor and receiving the size as an argument:

public class Teste
{
   public static String vetor[];
   ...
   public Teste(int tamanhoVetor)
   {
       vetor = new String [tamanhoVetor];
   }
}

In Servlet could be something like (the way to get the value may be different):

...
Teste testando = new Teste(Integer.valueOf(request.getAttribute("txtTamanho").toString()));
...
    
02.10.2014 / 15:50
1

I do not know if you are working with JSP, but if you are, you need to use the Request for this

   request.setAttribute("key", "valor") //set uma variavel de request

   RequestDispatcher rd = request.getRequestDispatcher("/teste.jsp"); 
   rd.forward(request, response);

   String valor = (String) request.getAttribute("key"); //pega o request na sua classe jsp

   String meuArray[] = new String[valor]; //Set o valor passado no seu Array =)
    
02.10.2014 / 15:56