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 += "'" + it.next().toString() + "',";
}
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 += "'" + it.next().toString() + "'";
} else
Myarray += "'" + it.next().toString() + "',";
}
}
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
it.next().hasNext()
? (i know this one doesn't works)
regards,
elise
-
need method to fill a string (3 messages)
- Posted by: e d
- Posted on: April 25 2001 07:33 EDT
Threaded Messages (3)
- need method to fill a string by Geoffrey Wiseman on April 25 2001 08:49 EDT
- need method to fill a string by Tony Brookes on April 25 2001 23:00 EDT
- need method to fill a string by e d on April 26 2001 08:17 EDT
-
need method to fill a string[ Go to top ]
- Posted by: Geoffrey Wiseman
- Posted on: April 25 2001 08:49 EDT
- in response to e d
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( it.next( ).toString( ) );
arrayString.append( "'" );
if( it.hasNext( ) )
arrayString.append( "," );
}
arrayString.append("); </script>" );
-
need method to fill a string[ Go to top ]
- Posted by: Tony Brookes
- Posted on: April 25 2001 23:00 EDT
- in response to e d
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.
first=false;
} else {
sb.append(",");
}
sb.append("'").append(it.next().toString()).append("'");
}
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. :-)
Chz
Tony
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. -
need method to fill a string[ Go to top ]
- Posted by: e d
- Posted on: April 26 2001 08:17 EDT
- in response to Tony Brookes
really thank you for the time you took to answer me