• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

How to design the two methods, simple case of deciding about how to pass arguments

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Suppose I have two methods as given here below



Now the question is as here below
In the above two methods, I pass only one map argument.
Suppose that I donno how many arguments to pass to anotherMethod while coding and if that will be determined only during execution, how to design my "testMethod" and "anotherMethod"?

Regards,
Sriram
 
Saloon Keeper
Posts: 14268
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would you not know how many arguments to pass? A method has some purpose, so it should always know ahead of time what parameters it requires.

Can you explain what scenario you have in mind in which the amount of parameters can only be determined at runtime?
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sriram Sharma wrote:Hi All,

Suppose I have two methods as given here below



Now the question is as here below
In the above two methods, I pass only one map argument.
Suppose that I donno how many arguments to pass to anotherMethod while coding and if that will be determined only during execution, how to design my "testMethod" and "anotherMethod"?

Regards,
Sriram



May be you should take a look at var-args...
It works, if you are passing variable no of arguments of same types....

Eg: -

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sriram Sharma wrote:In the above two methods, I pass only one map argument.
Suppose that I donno how many arguments to pass to anotherMethod while coding and if that will be determined only during execution, how to design my "testMethod" and "anotherMethod"?


While I agree with Stephan's comments, R.Jain has pretty much covered it: Have a look at varargs, viz:A couple of other points:
1. ALWAYS supply types for your maps (unless you're still using Java 1.4, in which case update it; it is officially deceased). Eg:
Map<Integer, String> hm1 = new HashMap<Integer, String>();

2. Don't make assumptions about the type of Map you're using. You've correctly defined your HashMap as a Map, but all your names indicate that the map is, in fact, a HashMap. The whole point of using interfaces is that you can change the assignment later if you want; and
Map<Integer, String> hm1 = new TreeMap<Integer, String>();
is going to look rather odd.

Winston
 
Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic