-4

I am new to sorting in Java, scenario is i am having 8 balls, in that one ball is having less weight, by using java code i need to identify which one is having less weight.

I have used following code, but not sure, this will work in all scenarios:

public class SortingMarble {
    public static void main(String[] args){
        List<Integer> list = Arrays.asList(20, 30, 90, 50, 45, 100,65,90);
        List<Integer> copy = new ArrayList<Integer>(list);
        Integer smallest = Collections.min(copy); // 20    
        System.out.println(smallest);
    }
}

Is there any java design pattern I can use here? is there any collection API to sort out this?

I have to use collections to sort out this. I used List Api, is there any other specific api in collection to sort?

Here i have given number, How can I make it to 8 different balls, where one ball is weighing less than the rest 7 balls?

10
  • 6
    java.lang.Comparable, java.util.Comparator, Collections#sort. Commented Jul 25, 2013 at 20:57
  • Well, you certainly don't need a Design Pattern here. Anyways, have you tried anything? At least bothered digging into Java Collections API? Or reading a Data Structure book? Commented Jul 25, 2013 at 20:58
  • 2
    Sounds more like you're trying to find the minimum rather than sort all 8 balls. Commented Jul 25, 2013 at 20:58
  • 1
    @troy_frommer What does it mean to “program to an interface”? Commented Jul 25, 2013 at 20:59
  • 1
    You should attempt the problem or show some more effort before asking a question Commented Jul 25, 2013 at 21:08

1 Answer 1

1
private class BallsComparator implements Comparator<Ball>
{
    public int compare(Ball o1, Ball o2)
    {
        return new Integer(o1.getWeight()).compareTo(o2.getWeight);
    }
}

Collections.sort(myBallsList, new BallsComparator());

This assumes that your balls have a getWeight() method that gives an int or Integer value. If not, adjust accordingly.

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

7 Comments

Well, this will improve the lazy behavior of OP from more lazy to the most lazy. Anyway, note that plain code answers are not accepted, based on What is an acceptable answer?, section 10.
Attacking people who just want to help, however, will improve the entire SO site.
I'm not attacking you, but noting that the rules on the site is that OP must show an effort instead of asking a gimme the codez question. About your answer, I'm not against it but telling you to improve it by following the rules on meta.
good solution, but how will I send values to parameter? i.e compare(4,4). I have total 8 balls, need to divide this by 2?
You don't. You just tell Collections.sort to sort your list of balls, and to do that using your BallsComparator. No need to call that yourself.
|

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.