Difference between revisions of "ReverseArray Method"
From CompSciWiki
Line 30: | Line 30: | ||
System.out.println("COMP 1260:"); | System.out.println("COMP 1260:"); | ||
printArray(comp1260); | printArray(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<names.length;i++){ | ||
reversed[i] = names[j]; | reversed[i] = names[j]; | ||
Line 45: | Line 55: | ||
return reversed; | return reversed; | ||
} | } | ||
− | |||
</pre> | </pre> | ||
}} | }} |
Revision as of 19:21, 5 April 2010
Back to the Program-A-Day homepage
ProblemStuff about the problem |
SideSectionTitle
An image or By Students section | |
---|---|---|
SolutionStuff about the Solution | ||
Code |