Java 101, how do I count the number of arguments passed to main?

for instance

public static void main(String[] args) { int count = 0; for (String s: args) { System.out.println(s); count++; } } 

Is there a way to do something like

int count = args.length ()? or args.size ()?

+6
source share
6 answers

It will be:

 int count = args.length; 
+12
source

Is there a way to do something like

int count = args.length ()? or args.size ()?

Yes, args.length .

There is nothing special about args ; it is a regular array of String objects and can be considered as such.

+4
source

All arrays have a โ€œfieldโ€ called length

 int count = args.length; 
+1
source
 public static void main(String[] args) { System.out.println(args.length); } 
+1
source

"args" is just an array, so you can use its length property.

Like this:

 System.out.println(args.length); 

and you are good to go.

+1
source

Just use int length=args.length

args is just an array, and you can use its length property.

+1
source

Source: https://habr.com/ru/post/921310/


All Articles