Question: JAVA Whats's wrong with my code?? I have 7 error messages saying the same thing (error: class, interface, or enum expected). Here's my code I

JAVA Whats's wrong with my code?? I have 7 error messages saying the same thing (error: class, interface, or enum expected).

Here's my code I included the errors where they apply):

import java.util.*;

public class SwapList{

public static void main(String args[]){ Integer array1[] = {1,2,3,4,5};

ArrayList list1 = new ArrayList(Arrays.asList(array1));

System.out.println("List1 is : "+list1);

Integer array2[] = {6, 7, 8, 9, 10, 11, 12};

ArrayList list2 = new ArrayList(Arrays.asList(array2));

System.out.println("List2 is : "+list2);

ArrayList alternateList = alternate(list1, list2);

System.out.println("Alternate List is : "+alternateList);

} }

public static ArrayList alternate(ArrayList list1, ArrayList list2) //error: class, interface, or enum expected {

ArrayList result = new ArrayList();

Iterator it1 = list1.iterator(); //error: class, interface, or enum expected Iterator it2 = list2.iterator(); //error: class, interface, or enum expected

while(it1.hasNext() || it2.hasNext()) //error: class, interface, or enum expected { if(it1.hasNext()) result.add((Integer) it1.next());

if(it2.hasNext()) //error: class, interface, or enum expected result.add((Integer) it2.next());

} //error: class, interface, or enum expected return result;

} //error: class, interface, or enum expected

/* Here is the problem I wish to solve:

Write a method called alternate that accepts two Lists as its parameters and returns a new List containing alternating elements from the first two lists, in the following order: First element from the first list First element from the second list Second element from the first list Second element from the second list Third element from the first list Third element from the second list ... If the lists do not contain the same number of elements, the remaining elements from the longer list should sit consecutively at one end. For example, for a first list of (1, 2, 3, 4, 5) and a second list of (6, 7, 8, 9, 10, 11, 12), a call of alternate(list1, list2) should return a list containing (1, 6, 2, 7, 3, 8, 4, 9, 5, 10, 11, 12). */

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!