What is the purpose of the following method? Assume that both q1 and q2 are not empty and contains the same number of elements. public class QueueEx { public static void method1(ArrayQueue q1, ArrayQueue q2) { Iterator iter1 = q1.iterator(); while (iter1.hasNext()) { int a = iter1.next(); int b = q2.poll(); if (a > b ) q2.offer(a); else q2.offer(b); } } } Corresponding elements of q1 and q2 are compared. q1 will remain unchanged and q2 will contain the elements which are greater among the corresponding elements of q1 and q2. All elements of q1 which are greater than the first element of q1 will be added to q2. Corresponding elements of q1 and q2 are compared. q1 will contain the smaller and q2 will contain the greater among the corresponding elements of q1 and q2. Corresponding elements of q1 and q2 are compared, q1 will contain the greater and q2 will contain the smaller among the corresponding elements of q1 and q2.
What is the purpose of the following method? Assume that both q1 and q2 are not empty and contains the same number of elements.
public class QueueEx
{
public static void method1(ArrayQueue<Integer> q1, ArrayQueue<Integer> q2)
{
Iterator<Integer> iter1 = q1.iterator();
while (iter1.hasNext())
{
int a = iter1.next();
int b = q2.poll();
if (a > b )
q2.offer(a);
else
q2.offer(b);
}
}
}
-
Corresponding elements of q1 and q2 are compared. q1 will remain unchanged and q2 will contain the elements which are greater among the corresponding elements of q1 and q2.
-
All elements of q1 which are greater than the first element of q1 will be added to q2.
-
Corresponding elements of q1 and q2 are compared. q1 will contain the smaller and q2 will contain the greater among the corresponding elements of q1 and q2.
-
Corresponding elements of q1 and q2 are compared, q1 will contain the greater and q2 will contain the smaller among the corresponding elements of q1 and q2.
Step by step
Solved in 2 steps