Given an array of Student type and size 10, create a linked list of students by linking students with an odd index first and then linking students with an even index. Write a loop to print out the students in the linked list.

Respuesta :

Answer:

Explanation:

I have written the following code as a method in Java. You simply add the String Array as a parameter and it will create a linked list, placing the students in an even index first and then the odd indexed students second. Once the linked list is populated it prints it out to the system.

public static void StudentList(String[] students) {

       LinkedList<String> names = new LinkedList<String>();

       for (int x = 0; x < students.length; x++) {

           if ((x % 2) == 0) {

               names.add(students[x]);

           }

       }

       for (int x = 0; x < students.length; x++) {

           if ((x % 2) != 0) {

               names.add(students[x]);

           }

       }

       for (int x = 0; x < students.length; x++) {

           System.out.println(names.get(x));

       }

   }