Difference between revisions of "PrintArray Method"
Line 3: | Line 3: | ||
|Problem= Pretend you're a computer science professor. You have a program which stores the names of all the students in each of your classes in separate arrays, based on the class they are in. How would you go about printing the contents of each of these arrays? | |Problem= Pretend you're a computer science professor. You have a program which stores the names of all the students in each of your classes in separate arrays, based on the class they are in. How would you go about printing the contents of each of these arrays? | ||
− | + | The most obvious way to print out your arrays would be to copy and paste a loop into your main method to print each of the arrays. Unfortunately, that would use a lot of redundant code. Think about it. You'd be writing a for loop for each array that you'd want to print out. By this method, if you've got two arrays to print, you'd need two loops to print them. If you have ten arrays, you'll need ten loops. If you've got one hundred arrays, you'll need one hundred loops. It's just not good coding. | |
Line 13: | Line 13: | ||
An image or By Students section | An image or By Students section | ||
− | |Solution= | + | |Solution=Write a method that takes an array as an input and prints out its contents! 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. |
}} | }} |
Revision as of 12:00, 1 April 2010
Back to the Program-A-Day homepage
ProblemPretend you're a computer science professor. You have a program which stores the names of all the students in each of your classes in separate arrays, based on the class they are in. How would you go about printing the contents of each of these arrays? The most obvious way to print out your arrays would be to copy and paste a loop into your main method to print each of the arrays. Unfortunately, that would use a lot of redundant code. Think about it. You'd be writing a for loop for each array that you'd want to print out. By this method, if you've got two arrays to print, you'd need two loops to print them. If you have ten arrays, you'll need ten loops. If you've got one hundred arrays, you'll need one hundred loops. It's just not good coding. |
SideSectionTitle
An image or By Students section | |
---|---|---|
SolutionWrite a method that takes an array as an input and prints out its contents! 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. | ||
CodeSolutionCode goes here. Please DO NOT put your code in <pre> tags! |