4

How to use Array list in Beanshell Sampler-Jmeter?

Das Prakash
  • 426
  • 1
  • 7
  • 16
  • 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? – timbre timbre Aug 17 '16 at 16:53
  • Question was corrected........@Kiril – Das Prakash Aug 18 '16 at 10:24

2 Answers2

8

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.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks Dmitri. I was was using the Generics <>Angle Brackets and was getting error. Thanks for mentioning those, to avoid using in Jmeter Beanshell. – Das Prakash Aug 19 '16 at 07:10
0

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();
Matthias Seifert
  • 2,033
  • 3
  • 29
  • 41
Deepak N
  • 1,408
  • 3
  • 15
  • 37