Difference between revisions of "Return Array"
Line 48: | Line 48: | ||
for( int i = 0; i < tempArray.length; i++ ) // This will loop through every item in the array | for( int i = 0; i < tempArray.length; i++ ) // This will loop through every item in the array | ||
{ | { | ||
− | System.out. | + | System.out.println( tempArray[i] ); // Print the changed value |
} | } | ||
} | } | ||
Line 61: | Line 61: | ||
for( int i = 0; i < array .length; i++ ) // This will loop through every item in the array | for( int i = 0; i < array .length; i++ ) // This will loop through every item in the array | ||
{ | { | ||
− | array [i] | + | array [i] += 2040000000; // add 2040000000 to each element to give it an area code |
} | } | ||
return array; // Return the changed array | return array; // Return the changed array | ||
Line 79: | Line 79: | ||
for( int i = 0; i < tempArray.length; i++ ) // This will loop through every item in the array | for( int i = 0; i < tempArray.length; i++ ) // This will loop through every item in the array | ||
{ | { | ||
− | System.out. | + | System.out.println( tempArray[i] ); // Print the changed value |
} | } | ||
} | } | ||
Line 87: | Line 87: | ||
for( int i = 0; i < array .length; i++ ) // This will loop through every item in the array | for( int i = 0; i < array .length; i++ ) // This will loop through every item in the array | ||
{ | { | ||
− | array [i] | + | array [i] += 2040000000; // add 2040000000 to each element to give it an area code |
} | } | ||
return array; // Return the changed array | return array; // Return the changed array |
Revision as of 12:06, 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.println( tempArray[i] ); // Print the changed value } }
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] += 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.println( 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] += 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! |