Answers
// References.java
import java.util.Scanner;
public class References {
/**
* method that accepts an array of strings as a parameter. It loops through the array
* and asks the user to input names. It populates the array of strings with the names.
* @param stringArray, an array of Strings
*/
public static void readStudentNames(String[] stringArray)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("readStudentNames Method: Putting values inside stringArray parameter");
// loop over the array, accepting strings for each array element
for(int i=0;i<stringArray.length;i++)
{
System.out.print("Enter name "+(i+1)+": ");
stringArray[i] = keyboard.nextLine();
}
}
/**
* method that accepts an ArrayList of strings as a parameter. It loops through the ArrayList
* and asks the user to input names. It populates the ArrayList with the names.
* @param stringArray, an ArrayList of Strings
*/
public static void readStudentNames(ArrayList<String> stringList)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("readStudentNames Method: Putting values inside stringList parameter");
// loop 5 times, to input 5 names into the array list
for(int i=0;i<5;i++)
{
System.out.print("Enter name "+(i+1)+": ");
stringList.add(keyboard.nextLine());
}
}
public static void main(String[] args) {
System.out.println("\t****** Passing References - Reading 5 Names *******");
System.out.println("\nMain Method: Passing an Array, nameArray, by-value");
String[] nameArray = new String[5];
readStudentNames(nameArray);
System.out.println("\nMain Method: The contents of nameArray are:");
for(int i=0;i<nameArray.length;i++)
System.out.println(nameArray[i]);
System.out.println("\nMain Method: Passing an ArrayList, nameArrayList, by-value");
ArrayList<String> nameArrayList = new ArrayList<String>();
readStudentNames(nameArrayList);
System.out.println("\nMain Method: The contents of nameArray are:");
for(String name : nameArrayList)
System.out.println(name);
}
}
// end of References.java
Output:
// References.java
import java.util.Scanner;
public class References {
/**
* method that accepts an array of strings as a parameter. It loops through the array
* and asks the user to input names. It populates the array of strings with the names.
* @param stringArray, an array of Strings
*/
public static void readStudentNames(String[] stringArray)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("readStudentNames Method: Putting values inside stringArray parameter");
// loop over the array, accepting strings for each array element
for(int i=0;i<stringArray.length;i++)
{
System.out.print("Enter name "+(i+1)+": ");
stringArray[i] = keyboard.nextLine();
}
}
/**
* method that accepts an ArrayList of strings as a parameter. It loops through the ArrayList
* and asks the user to input names. It populates the ArrayList with the names.
* @param stringArray, an ArrayList of Strings
*/
public static void readStudentNames(ArrayList<String> stringList)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("readStudentNames Method: Putting values inside stringList parameter");
// loop 5 times, to input 5 names into the array list
for(int i=0;i<5;i++)
{
System.out.print("Enter name "+(i+1)+": ");
stringList.add(keyboard.nextLine());
}
}
public static void main(String[] args) {
System.out.println("\t****** Passing References - Reading 5 Names *******");
System.out.println("\nMain Method: Passing an Array, nameArray, by-value");
String[] nameArray = new String[5];
readStudentNames(nameArray);
System.out.println("\nMain Method: The contents of nameArray are:");
for(int i=0;i<nameArray.length;i++)
System.out.println(nameArray[i]);
System.out.println("\nMain Method: Passing an ArrayList, nameArrayList, by-value");
ArrayList<String> nameArrayList = new ArrayList<String>();
readStudentNames(nameArrayList);
System.out.println("\nMain Method: The contents of nameArray are:");
for(String name : nameArrayList)
System.out.println(name);
}
}
// end of References.java
Output: