Difference between revisions of "FillArray Method"
(Added quotations to student quotes) |
|||
(7 intermediate revisions by 4 users not shown) | |||
Line 10: | Line 10: | ||
− | + | {{CodeBlock | |
+ | |Code=public class PrintArray{ | ||
public static void main (String [] args){ | public static void main (String [] args){ | ||
String [] comp1010 = { "Adam", "Bob", "Carl", "David", "Edward"}; | String [] comp1010 = { "Adam", "Bob", "Carl", "David", "Edward"}; | ||
Line 34: | Line 35: | ||
} | } | ||
} | } | ||
− | + | }} Use the printArray method to print out the all class lists after creating the new list. | |
− | |SideSectionTitle= | + | |SideSectionTitle=...By Students |
|SideSection= | |SideSection= | ||
− | + | "Using methods with arrays is just the beginning of the myriad ways in which methods will assist you in future programming. I remember when I first began learning about methods, they seemed like a complete waste of time. I mean, why section off this small bit of code instead of just putting the couple of lines in the main? All I can say is learn this stuff well, because I couldn't have been more wrong. Methods are the building blocks that you will used for everything else you do in programming." | |
<BR> | <BR> | ||
− | |Solution= To create the fillArray method, use a | + | |Solution= To create the fillArray method, first create a new array in the new method. Then use a for loop to cycle through the entire array. Inside the loop it should prompt the user to enter a name, and insert the user input into the array. |
− | |SolutionCode= | + | |SolutionCode=import javax.swing.*; |
class Students{ | class Students{ | ||
Line 66: | Line 67: | ||
public static String[] fillArray(int size){ | public static String[] fillArray(int size){ | ||
String[] names = new String[size]; //an Array to hold the entered names | String[] names = new String[size]; //an Array to hold the entered names | ||
− | |||
for (int i=0;i<names.length;i++){ | for (int i=0;i<names.length;i++){ | ||
− | + | names[i] = JOptionPane.showInputDialog(null, "Please enter a name:");//insert the user input name into the array | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
return names; | return names; | ||
} | } | ||
} | } | ||
− | |||
}} | }} |
Latest revision as of 15:31, 8 December 2011
Back to the Program-A-Day homepage
ProblemToday we'll look at returning a new array from a different method.
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 all class lists after creating the new list. |
...By Students"Using methods with arrays is just the beginning of the myriad ways in which methods will assist you in future programming. I remember when I first began learning about methods, they seemed like a complete waste of time. I mean, why section off this small bit of code instead of just putting the couple of lines in the main? All I can say is learn this stuff well, because I couldn't have been more wrong. Methods are the building blocks that you will used for everything else you do in programming."
| |
---|---|---|
SolutionTo create the fillArray method, first create a new array in the new method. Then use a for loop to cycle through the entire array. Inside the loop it should prompt the user to enter a name, and insert the user input into the array. | ||
Code |