PrintArray Method

From CompSciWiki
Revision as of 12:00, 1 April 2010 by DavidS (Talk | contribs)

Jump to: navigation, search

Back to the Program-A-Day homepage

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.

 

SideSectionTitle

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

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.

Code

SolutionCode goes here. Please DO NOT put your code in <pre> tags!

Back to the Program-A-Day homepage