Difference between revisions of "PrintArray Method"

From CompSciWiki
Jump to: navigation, search
 
(15 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
{{1010PrAD|ProblemName=printArray Method
 
{{1010PrAD|ProblemName=printArray Method
  
|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= Today we'll look at sending an array to another method.
 +
 
 +
 
 +
Pretend you're a computer science professor. You have a program which uses arrays to store the names of all the students in each of your classes, (one array for each class). 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.
 
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.
 +
  
 
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:
  
<pre>public class PrintArray{
+
{{CodeBlock
 +
|Code=public class PrintArray{
  
 
public static void main (String [] args){
 
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.
 +
 +
|SideSectionTitle=Arrays and Methods
 +
|SideSection=
 +
[[Image:Wiki_chars01.jpg|center]]<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.
 +
 +
|SolutionCode=public class PrintArray{
 +
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
 
String [] comp1020 = { "Amy", "Beth", "Cindy", "Dawn", "Ellen"}; //class list for comp 1020
 
String [] comp1020 = { "Amy", "Beth", "Cindy", "Dawn", "Ellen"}; //class list for comp 1020
 
String [] comp1260 = { "Mike", "Mary", "Matthew", "Megan", "Moe"}; //class list for comp 1260
 
String [] comp1260 = { "Mike", "Mary", "Matthew", "Megan", "Moe"}; //class list for comp 1260
}
 
}</pre>
 
  
Now write a method that you can call to print each of the arrays.
+
System.out.println("COMP 1010:");
 +
printArray(comp1010); //note that since the method is void, you just use the name of the method
 +
System.out.println("COMP 1020:"); //followed by the name of the array in brackets
 +
printArray(comp1020);
 +
System.out.println("COMP 1260:");
 +
printArray(comp1260);
  
 +
System.out.println("--End of Processing--");
 +
}
  
|SideSection=
+
public static void printArray(String [] courseList){ //note that the method is void, and the input variable is an array of Strings
[[Image:classroom.jpg|float|267px]]
+
//We called the array "courseList", since this can refer to any of the arrays
<BR>
+
//we send it
Taken from http://www.flickr.com/photos/ijames/112866961/
+
  
An image or By Students section
+
for (int i=0; i<courseList.length;i++){ //for loop to go through each element in the array
 
+
System.out.println(courseList[i]); //print the current element
|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.
+
 
+
<pre> public static void printArray(String [] courseList){
+
for (int i=0; i<courseList.length;i++){
+
System.out.println(courseList[i]);
+
 
}
 
}
System.out.println();
+
System.out.println(); //add a blank line after the list of students
}</pre>
+
}
 
+
}
 
}}
 
}}

Latest revision as of 03:27, 6 December 2011

Back to the Program-A-Day homepage

Problem

Today we'll look at sending an array to another method.


Pretend you're a computer science professor. You have a program which uses arrays to store the names of all the students in each of your classes, (one array for each class). 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.


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:

 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

Wiki chars01.jpg

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.

Code

Solution Code

Back to the Program-A-Day homepage