Difference between revisions of "Return Array"
Line 31: | Line 31: | ||
</pre> | </pre> | ||
<BR> | <BR> | ||
− | Now that we have the skeleton we can start by creating an array in main, we will pass that array to our returnArray method | + | Now that we have the skeleton we can start by creating an array in main, we will pass that array to our returnArray method. returnArray will return an array which we will store in a temporary array and print: |
<pre> | <pre> | ||
public static void main() | public static void main() |
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! |