4
votes

How to use Array list in Beanshell Sampler-Jmeter?

2
Can you please invest into formatting your question so that it's readable? For example what is rs.fetchAns("1") - we don't have your source code so we don't know what that code meansor what it returns. Then return type is string. - is it an exception you are getting or what? - thiscommunityistoxic
Question was corrected........@Kiril - Das Prakash

2 Answers

8
votes

Just like in Java, i.e. the following code:

ArrayList myList = new ArrayList();
myList.add("something");
myList.add("something else");

for (int i = 0; i < myList.size(); i++) {
  log.info(myList.get(i));
}

Will print myList contents to jmeter.log file:

Beanshell ArrayList


Remember that Beanshell doesn't support Generics so avoid using diamond operators elsewise you'll get errors. If there is no particular reason for sticking to Beanshell I would suggest considering switching to JSR223 Test Elements and Groovy language - see Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! guide for explanation, benchmarks and scripting best practices.

0
votes

You can use arraylist easily in beenshell. it is same as Java so import a package for ArrayList and then create ArrayList object.

//importing arraylist package from java
import java.util.ArrayList;

//creating arraylist object
ArrayList lines = new ArrayList();