need method to fill a string


Web tier: servlets, JSP, Web frameworks: need method to fill a string

  1. need method to fill a string (3 messages)

    hi all,
    i need to make a javascript array with the results of database query. something like
    var myArray = new Array('result1', result2');
    my jsp code is :
         Iterator it = myListCurCached.iterator();
         String Myarray = "<script> var IDLangArray = new Array(";
         while (it.hasNext())
    Myarray += "'" + + "',";
         Myarray += "); </script>";
    the problem is that with my code i obtain something like :
    var myArray = new Array('result1', result2',);
    (there is the last coma of course and it bugs)i thoink i should do something like :
    while (it.hasNext())
            if (there is something after the next element){
    Myarray += "'" + + "'";
            } else
    Myarray += "'" + + "',";

    so i could terminate correctly the array. what could i do to "detect" the "if (there is something after the next element)" is there a tip like
    ? (i know this one doesn't works)


    Threaded Messages (3)

  2. need method to fill a string[ Go to top ]

    This should do the trick:

         Iterator it = myListCurCached.iterator();
         StringBuffer arrayString = new StringBuffer( );
         arrayString.append( "<script> var IDLangArray = new Array(" );
         while( it.hasNext( ) )
              arrayString.append( "'" );
              arrayString.append( ).toString( ) );
              arrayString.append( "'" );
              if( it.hasNext( ) )
                   arrayString.append( "," );
         arrayString.append("); </script>" );
  3. need method to fill a string[ Go to top ]

    I would do it the other way round. In other words, append comma BEFORE the next value from the iterator, UNLESS this is the first element in the iterator.

    In other words...

    boolean first = true; // Before we start, we're on line 1!
    StringBuffer sb = new StringBuffer();

    while (it.hasNext()) {
       if (first) {
         // Do not append comma, but reset first flag
         // so we go the other way next time.
       } else {

    This makes more sense to me because the first element is a known position in the loop, whereas the last element is not, hence the need to double call it.hasNext().

    This should be quicker because.

    1) You are only making one call to it.hasNext() per loop.
    2) You are doing a simple boolean comparisson in your if statement, no complex evaluation.
    3) A StringBuffer is a lot more efficient for building strings. myString = String1 + String2 results in:

      a) new StringBuffer(String1);
      b) StringBufferCreatedInA.append(String2);
      c) Set myString to point to StringBufferCreatedInA.toString().

    This happens for each and every a + b you perform, which is a lot of object creation and destruction. Create one buffer, append to it until your're done, then .toString() it.

    This is slightly off topic, but it's more efficient so I thought I'd mention it. :-)



    PS. The other way to do it of course is to simply remove the trailing comma before you append the ")", since you know it will always be there if the iterator was not empty.
  4. need method to fill a string[ Go to top ]

    really thank you for the time you took to answer me