• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to calculate number of arguments in a function  RSS feed

 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like function have five arguments

public static String[] Savestate;

public void func(String a, String b, String c, String d, String e)
{
}

now i want to put those five argument into a static string array.

how to calculate number of arguments in function?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The number of arguments is fixed when you write the code, in your example it's 5. There's no easy way to calculate it at runtime (nor to dynamically access the arguments). (You could do it with reflection, but that would be very complicated).

You could use varargs. Inside the method, the varargs argument behaves like an array:



Please UseCodeTags when you post source code.
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to declare String array having unlimited size?

 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you like do that?
If you are trying to assign similar information to same array,
would you consider to call the method several times (5 in your case).

Array can't have unlimited size, it's at least limited by your machine's memory.
you should use List instead (e.g. ArrayList).
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList is just as limited as an array, since it uses an array internally. But in the end both use an int for indexing, allowing up to 2^31 elements (theoretically; memory usually runs out long before that boundary is met).

I agree with Jesper, use varargs.
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir Arraylist is easy sortout but i want to solve it using string arrays...

well thanks for help and varargs data....
i will check on it later....
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote: . . . allowing up to 2^31 elements . . .
Last time I tried, I could only count 2^31 - 1 elements. Or maybe I lost count at 2^30
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You forgot 0. There are for ints 2^31 negative values, 2^31 - 1 positive values, and 0. The latter falls in the allowed range for array indexes.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Azrael Noor wrote:How to declare String array having unlimited size?

Perhaps you mean a size that's "unknown" until runtime?

If so, note that the varargs array length is determined by the number of arguments passed, so you do not need to specify this in advance.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you try initialising an array larger than new int[2147483647]


You get a compiler error.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:But you try initialising an array larger than new int[2147483647]


You get a compiler error.

Touché.
 
Slowik Arte
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried reflection mechanism build in Java.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Slowik Arte wrote:Have you tried reflection mechanism build in Java.

Have you tried breaking an egg with a sledgehammer? It'll work, but the results will be very messy.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch , Slowik Arte
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!