How to define an empty list of string in jshell in a shortest possible way?

Anthony Kong :

I want to create an empty list of type List<String> in jshell

I am hoping jshell supports some sort of syntax shorthand for example:

  List<String> args = []

However it failed with this error message:

|  Error:
|  illegal start of expression
|  List<String> args = [];
|                      ^

Of course I can just use the standard java expression:

import com.google.common.collect.ImmutableList
List<String> args = ImmutableList.of()

but I want to know if it can be more terse.

Naman :

If you're initialising a List you can do it as:

List<String> list = List.of();

If instead it was an array, you could have initialized it as :

String[] arr = {}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=133511&siteId=1