Difference between revisions of "Return Array"
From CompSciWiki
Line 20: | Line 20: | ||
public static int[] returnArray( int[] n ) | public static int[] returnArray( int[] n ) | ||
{ | { | ||
− | for( int i = 0; i < n.length; i++ ) | + | for( int i = 0; i < n.length; i++ ) // This will loop through every item in the array |
{ | { | ||
− | n[i] = i+1; | + | n[i] = i+1; // Change the items to i+1 - we don't want start at i because we don't want 0 |
− | System.out.print( n[i] + "," ); | + | System.out.print( n[i] + "," ); // Print the changed value |
} | } | ||
− | return n; | + | return n; // Return the changed array |
} | } | ||
</pre> | </pre> | ||
}} | }} |
Revision as of 12:45, 30 March 2010
Back to the Program-A-Day homepage
ProblemWrite a method which will take an array of ints as a parameter.
The method should change the contents of the array to 1-5. Print the array to show the contents have been changed and return it.
|
SideSectionTitle
An image or By Students section | |
---|---|---|
SolutionThe solution... public static int[] returnArray( int[] n ) { for( int i = 0; i < n.length; i++ ) // This will loop through every item in the array { n[i] = i+1; // Change the items to i+1 - we don't want start at i because we don't want 0 System.out.print( n[i] + "," ); // Print the changed value } return n; // Return the changed array } | ||
CodeSolutionCode goes here. Please DO NOT put your code in <pre> tags! |