Difference between revisions of "PrintArray Method"
m (Removed <pre> tags from SolutionCode.) |
|||
(One intermediate revision by one other user not shown) | |||
Line 12: | Line 12: | ||
So, back to our imaginary situation where you're a computer science professor. You've got your program set up with three arrays of Strings, representing the COMP 1010, COMP 1020, and COMP 1260 classes, respectively: | So, back to our imaginary situation where you're a computer science professor. You've got your program set up with three arrays of Strings, representing the COMP 1010, COMP 1020, and COMP 1260 classes, respectively: | ||
− | + | {{CodeBlock | |
+ | |Code=public class PrintArray{ | ||
public static void main (String [] args){ | public static void main (String [] args){ | ||
Line 22: | Line 23: | ||
String [] comp1260 = { "Mike", "Mary", "Matthew", "Megan", "Moe"}; | 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. | 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. | ||
Line 28: | Line 29: | ||
|SideSectionTitle=Arrays and Methods | |SideSectionTitle=Arrays and Methods | ||
|SideSection= | |SideSection= | ||
− | [[Image:Wiki_chars01.jpg|center]] | + | [[Image:Wiki_chars01.jpg|center]]<BR> |
− | <BR> | + | |
|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. |
Latest revision as of 03:27, 6 December 2011
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 |