Sting arrays of flexible length

Sting arrays of flexible length

Dirk Draheim
Vastuste arv 0

A simple way to realize Sting arrays of flexible length (for example useful as return values of methods and Web services) is to declare the String array in terms of a variable length 'n' and to defer the declaration 'new String[n];' after the concrete length of the String is known (it is not a "nice" solution but works for our purposes), see the following example:


    @WebMethod // compiler directive to turn this into a web method
    public String[] getStrings (){
      int n;
      /*
      n is created dynamically, e.g., by determining the length
      of a JDBC database query result set, for example:
        resultSet.last(); size = resultSet.getRow();
      */
      n=7; // ... n set to some value, see comment before
      String[] s = new String[n];
      for (int i=0;i<n;i++){
        s[i]="BLA ";
      }
      return s;
    }