How to pass an "Array of Class" as a parameter into a Method (in JAVA)?
-
There are my codes... public static void main(String[] args) { IceCream[] flavour = new IceCream[2]; flavour[0] = new IceCream("Mint", false); flavour[1] = new IceCream("Chocolate", false); fnMainMenu(flavour); //what is the correct way to pass in the array of class? } private static void fnMainMenu(IceCream[] flavour) //what should be in the brackets? { } class IceCream { some declaration of variables here } In C++ we can create an array of Struct to hold values and pass the array of Structs to other functions. But Java only have classes. I intend to pass an array of class to a Method, but I am not sure of the syntax. Anyone could enlighten me? Thank you!!
-
Answer:
void fnMainMenu( IceCream[] f ) { // do whatever } IceCream findFlavor( IceCream [] f ) { for( int i = 0; i<f.length() ) { if( f[i].getFlavor().equalsIgnoreCase("Rocky… ) return f[ i ]; return null; } // be cautious, Java passes Objects by reference. Java may be your learning tool for what an Object really is, c++ fails to make the whole point of Objects emphatic. You may think of Objects in c++ as 'final', but not in Java unless you declare 'final'.
Executor at Yahoo! Answers Visit the source
Related Q & A:
- How To Connect Two Different Network Segments Using A Switch And A Router?Best solution by Super User
- Why cannot we use static keyword inside a method in java?Best solution by Stack Overflow
- How to pass a variable from php to a modal?Best solution by Stack Overflow
- How to pass a parameter to a function that is called on an event?Best solution by Stack Overflow
- How much does it cost to trade in a DS for a DS Lite?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.