Difference between revisions of "ReverseArray Method"

From CompSciWiki
Jump to: navigation, search
 
(Added quotations to student quotes)
 
(23 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 
{{1010PrAD|ProblemName=reverseArray Method
 
{{1010PrAD|ProblemName=reverseArray Method
  
|Problem= Stuff about the problem
+
|Problem= Today we'll look at sending an array to a different method, modifying the array, and returning the array to the original method.
  
  
 +
Let's again expand on the printArray program with another additional twist. Recall you created a method to print the names of students stored in different arrays for different classes. You have now discovered that it would be useful to be able to print the names of the students in reverse order, and must add a method to the program to do this.
 +
 +
 +
Take the sample solution from printArray below and add a reverseArray method to it. This method should accept an array of Strings as input and return a reversed version of this String array as output.
 +
 +
 +
{{CodeBlock
 +
|Code=public class PrintArray{
 +
public static void main (String [] args){
 +
String [] comp1010 = { "Adam", "Bob", "Carl", "David", "Edward"};
 +
String [] comp1020 = { "Amy", "Beth", "Cindy", "Dawn", "Ellen"};
 +
String [] comp1260 = { "Mike", "Mary", "Matthew", "Megan", "Moe"};
 +
 +
System.out.println("COMP 1010:");
 +
printArray(comp1010);
 +
System.out.println("COMP 1020:");
 +
printArray(comp1020);
 +
System.out.println("COMP 1260:");
 +
printArray(comp1260);
 +
 +
System.out.println("--End of Processing--");
 +
}
 +
 +
public static void printArray(String [] courseList){
 +
 +
for (int i=0; i<courseList.length;i++){
 +
System.out.println(courseList[i]);
 +
}
 +
System.out.println();
 +
}
 +
}
 +
}} Use the printArray method to print out the class lists in their original order, and then in reversed order.
 +
 +
|SideSectionTitle=...By Students
 
|SideSection=
 
|SideSection=
[[Image:classroom.jpg|float|267px]]
 
<BR>
 
Taken from http://www.flickr.com/photos/ijames/112866961/
 
  
An image or By Students section
+
"This problem was actually given to me during an interview for computer science coop.
 +
The question was a little more complicated, but it came down to reversing strings, the more difficult part was to do it "in place". Surprisingly, I had a hard time remembering how to do this!
 +
It just goes to show you, what you learn in Comp 1010 is very relevant and will come back to haunt you in the future!"
  
|Solution=Stuff about the Solution
+
 
|SolutionCode=<pre>public class PrintArray{
+
|Solution=To make your reverseArray method work, use a for loop to cycle through a second array from start to finish, and use a seperate int, decrimented each loop, to cycle through the original array from end to start. In each iteration of the loop, copy the entry from the old array to the new one, and return the new array.
 +
|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 30: Line 64:
 
                 System.out.println("COMP 1260:");
 
                 System.out.println("COMP 1260:");
 
printArray(comp1260);
 
printArray(comp1260);
                 comp1010 = reverseArray(comp1260);
+
                 comp1260 = reverseArray(comp1260);
 
System.out.println("COMP 1260 reversed:");
 
System.out.println("COMP 1260 reversed:");
 
                 printArray(comp1260);
 
                 printArray(comp1260);
 
System.out.println("--End of Processing--");
 
System.out.println("--End of Processing--");
 +
}
 +
 +
public static void printArray(String [] courseList){ //note that the method is void, and the input variable is an array of Strings
 +
//We called the array "courseList", since this can refer to any of the arrays
 +
//we send it
 +
 +
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
 +
}
 +
System.out.println(); //add a blank line after the list of students
 
}
 
}
  
 
public static String[] reverseArray(String[] names){
 
public static String[] reverseArray(String[] names){
String[] reversed = new String[names.length];
+
String[] reversed = new String[names.length];   //create a new array to hold the reversed version of the array
int j = names.length-1;
+
int j = names.length-1;                         //int j will be used to cycle backwards throught the original array
for (int i=0;i<names.length;i++){
+
for (int i=0;i<reversed.length;i++){
 
reversed[i] = names[j];
 
reversed[i] = names[j];
 
j--;
 
j--;
Line 46: Line 90:
 
}
 
}
 
}
 
}
</pre>
 
 
}}
 
}}

Latest revision as of 15:32, 8 December 2011

Back to the Program-A-Day homepage

Problem

Today we'll look at sending an array to a different method, modifying the array, and returning the array to the original method.


Let's again expand on the printArray program with another additional twist. Recall you created a method to print the names of students stored in different arrays for different classes. You have now discovered that it would be useful to be able to print the names of the students in reverse order, and must add a method to the program to do this.


Take the sample solution from printArray below and add a reverseArray method to it. This method should accept an array of Strings as input and return a reversed version of this String array as output.


 public class PrintArray{
	public static void main (String [] args){
		String [] comp1010 = { "Adam", "Bob", "Carl", "David", "Edward"};	
		String [] comp1020 = { "Amy", "Beth", "Cindy", "Dawn", "Ellen"};
		String [] comp1260 = { "Mike", "Mary", "Matthew", "Megan", "Moe"};

		System.out.println("COMP 1010:");
		printArray(comp1010);		
		System.out.println("COMP 1020:");
		printArray(comp1020);				
		System.out.println("COMP 1260:");
		printArray(comp1260);				

		System.out.println("--End of Processing--");
	}

	public static void printArray(String [] courseList){
								
		for (int i=0; i<courseList.length;i++){		
			System.out.println(courseList[i]);	
		}
		System.out.println();				
	}
} 
Use the printArray method to print out the class lists in their original order, and then in reversed order.
 

...By Students

"This problem was actually given to me during an interview for computer science coop. The question was a little more complicated, but it came down to reversing strings, the more difficult part was to do it "in place". Surprisingly, I had a hard time remembering how to do this! It just goes to show you, what you learn in Comp 1010 is very relevant and will come back to haunt you in the future!"

Solution

To make your reverseArray method work, use a for loop to cycle through a second array from start to finish, and use a seperate int, decrimented each loop, to cycle through the original array from end to start. In each iteration of the loop, copy the entry from the old array to the new one, and return the new array.

Code

Solution Code

Back to the Program-A-Day homepage