118k views
4 votes
3.1 Write a Java program called PrintNames that will do the following:

1. Create String array called names[] which will hold 5 elements.
2. Populate the array with any 5 names 3. Output the names in the array using a loop structure​

User Mch
by
8.1k points

1 Answer

4 votes

Answer:

public class PrintNames {

public static void main(String[] args) {

// Create a String array to hold 5 names

String[] names = new String[5];

// Populate the array with names

names[0] = "Magnus Carlsen";

names[1] = "Joe Rogan";

names[2] = "The Rock";

names[3] = "Lebron James";

names[4] = "Marion Tinsley";

// Output the names using a loop

for (int i = 0; i < names.length; i++) {

System.out.println(names[i]);

}

}

}

User Seb DA ROCHA
by
6.8k points