Difference between revisions of "PrintArray Method"
(removed () in first sentence) |
m (Removed <pre> tags from SolutionCode.) |
||
Line 33: | Line 33: | ||
|Solution=Write a method that takes an array as an input and loops through it, printing out each element as it goes! It's an easy solution, and once you've got your method written you can easily print another array by just adding one more line of code (a method call!) to your program. | |Solution=Write a method that takes an array as an input and loops through it, printing out each element as it goes! It's an easy solution, and once you've got your method written you can easily print another array by just adding one more line of code (a method call!) to your program. | ||
− | |SolutionCode= | + | |SolutionCode=public class PrintArray{ |
public static void main (String [] args){ | public static void main (String [] args){ | ||
String [] comp1010 = { "Adam", "Bob", "Carl", "David", "Edward"}; //class list for comp 1010 | String [] comp1010 = { "Adam", "Bob", "Carl", "David", "Edward"}; //class list for comp 1010 | ||
Line 59: | Line 59: | ||
} | } | ||
} | } | ||
− | |||
}} | }} |
Revision as of 12:02, 9 April 2010
Back to the Program-A-Day homepage
ProblemToday we'll look at sending an array to another method.
public class PrintArray{ public static void main (String [] args){ //class list for comp 1010 String [] comp1010 = { "Adam", "Bob", "Carl", "David", "Edward"}; //class list for comp 1020 String [] comp1020 = { "Amy", "Beth", "Cindy", "Dawn", "Ellen"}; //class list for comp 1260 String [] comp1260 = { "Mike", "Mary", "Matthew", "Megan", "Moe"}; } } Now add to this code by writing a method which will take an array of Strings as an input. Inside this method you want to print out the entire contents of the array. This method should be void, as you are not returning anything. |
Arrays and Methods
| |
---|---|---|
SolutionWrite a method that takes an array as an input and loops through it, printing out each element as it goes! It's an easy solution, and once you've got your method written you can easily print another array by just adding one more line of code (a method call!) to your program. | ||
Code |