1

Trying to print output as:

First max

First min

Second Max

Second min

Third Max ... and so on

This is my code:

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;


public class Arraylist1 {
public static void main(String args[]){
 List <Integer>list= new ArrayList <Integer> ( );
 list.add(20);
 list.add(30);
 list.add(70);
 list.add(50);
 list.add(60);
 list.add(40);
 for(int i=0;i<list.size();i++) {  
  if(i%2==0){
    Object num=Collections.max(list);
    System.out.println(num);
    list.remove(num);     
  }
 else if(i%2!=0)
    {
    Object num1=Collections.min(list);
    System.out.println(num1);
    list.remove(num1);
    }
}
}
}


 70
 20
 60

Why it printing only first three numbers?

My expected output:

 70 20 60 30 50 40
2
  • in the for loop for one interation it only executes either max() or min() cases. I suppose i can help. Commented Apr 2, 2017 at 3:24
  • And hint for the next time: please spend some time to properly format/indent all your input. And as said: please do not forget to accept one of the answers. It is not really polite to ask for help, receive that, and then move on without looking back, is it. Commented Apr 2, 2017 at 7:04

3 Answers 3

1
for(int i=0;i<list.size();i++)

You looping condition is incorrect.

You don't loop 6 times.

You only loop 3 times because the size changes as you remove an item from the list in each iteration.

So you probably want:

int max = list.size();

for(int i = 0; i < max; i++)

That is you want to fix the loop to be the initial number of entries in the list.

Sign up to request clarification or add additional context in comments.

4 Comments

can you please help me to wrtie the same code by using comparator interface?
@Swati, You got lots of help on your original question and you still haven't "accepted" an answer to let people know the problem has been solved. You didn't even bother to acknowledge the help/explanation I provided, so I think I'll skip this next question. In any case its one question per topic so all suggestions relate to the original topic otherwise the posting get too confusing trying to understand what all the suggestions mean. Also, I have no idea what you are now asking.
I am new to stackover flow. so have no idea how to accept the answers. can u please help me in this regards @camickr
@Swati, You click on the "checkmark" beside that answer that best help solve the problem. Just noticed you figured it out easy enough on your next question. Why haven't you accepted an answer in this question yet?
1

Your problem is that your loop iterates until until reachig list.size().

But you keep decreasing that size on the one hand; but on the other hand, you keep re-computing that loop condition!

You have to assign that size to a helper variable like initialSize and compare against that fixed value instead!

4 Comments

Glad to hear that. So don't forget about accepting later on:-)
for(int i=0;i>list.size();i++) , what went wrong if I iterate in this manner. Though it worked for 3 elements
can you please help me to wrtie the same code by using comparator interface?
You got three answers by now that show how to solve this problem. But if you have another question, then ask another question. Don't do that in comments. Finally: computing min/max repeatedly is very inefficient. You can simply sort the list once, and then print the elements at last/first/second to last/second... index instead.
0

With Every list.remove(), the size of list gets decremented. So the loop only runs 3 times. You can use a while loop instead.

public static void main(String args[]){
    List<Integer>list= new ArrayList <Integer> ();
    list.add(20);
    list.add(30);
    list.add(70);
    list.add(50);
    list.add(60);
    list.add(40);
    int i =0;
    while(!list.isEmpty()) {  
        if(i%2==0){
            Object num=Collections.max(list);
            System.out.println(num);
            list.remove(num);
        }
        else if(i%2!=0) {
            Object num1=Collections.min(list);
            System.out.println(num1);
            list.remove(num1);
        }
        i++;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.