Difference between revisions of "Return Array"
Line 4: | Line 4: | ||
<BR><BR> | <BR><BR> | ||
Example: | Example: | ||
− | int[] array = {4889123, 4889124, 4889125};<BR> | + | <BR> |
− | returnArray(array);<BR> | + | int[] array = {4889123, 4889124, 4889125}; |
+ | <BR> | ||
+ | returnArray(array); | ||
+ | <BR> | ||
Output: | Output: | ||
+ | <BR> | ||
2044889123 | 2044889123 | ||
+ | <BR> | ||
2044889124 | 2044889124 | ||
+ | <BR> | ||
2044889125 | 2044889125 | ||
Revision as of 12:02, 1 April 2010
Back to the Program-A-Day homepage
ProblemWrite a method which will take an array of phone numbers and add the 204 area code to each one. Print this array in main.
|
SideSectionTitle
An image or By Students section | |
---|---|---|
SolutionThe solution...
To solve this problem we are going to create a skeleton structure. We know that we will have two methods: main & returnArray. We know that returnArray will accept an integer array and return that array:
public static void main() { } public static int[] returnArray( int[] array ) { }
public static void main() { int[] array = {4889123, 4889124, 4889125}; int[] tempArray; tempArray = returnArray(array); for( int i = 0; i < tempArray.length; i++ ) // This will loop through every item in the array { System.out.print( tempArray[i] + "/" ); // Print the changed value } } public static int[] returnArray( int[] n ) { }
public static int[] returnArray( int[] array ) { for( int i = 0; i < array .length; i++ ) // This will loop through every item in the array { array [i] = i+2040000000; // add 2040000000 to each element to give it an area code } return array; // Return the changed array }
public static void main() { int[] array = {4889123, 4889124, 4889125}; int[] tempArray; tempArray = returnArray(array); for( int i = 0; i < tempArray.length; i++ ) // This will loop through every item in the array { System.out.print( tempArray[i] + "/" ); // Print the changed value } } public static int[] returnArray( int[] n ) { for( int i = 0; i < array .length; i++ ) // This will loop through every item in the array { array [i] = i+2040000000; // add 2040000000 to each element to give it an area code } return array; // Return the changed array } | ||
CodeSolutionCode goes here. Please DO NOT put your code in <pre> tags! |