-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?

AKB
  • 5,918
  • 10
  • 53
  • 90
  • 6
    [`java.lang.Comparable`](http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html), [`java.util.Comparator`](http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html), [`Collections#sort`](http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort%28java.util.List,%20java.util.Comparator%29). – Luiggi Mendoza Jul 25 '13 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? – Rohit Jain Jul 25 '13 at 20:58
  • 2
    Sounds more like you're trying to find the _minimum_ rather than sort all 8 balls. – DaoWen Jul 25 '13 at 20:58
  • If you are using an Array List use Collections.sort. Look at this question [here](http://stackoverflow.com/questions/15666382/sorting-arraylist) – troy_frommer Jul 25 '13 at 20:59
  • 1
    @troy_frommer [What does it mean to “program to an interface”?](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) – Luiggi Mendoza Jul 25 '13 at 20:59
  • Or maybe you are looking for the algorithm to find the different weight ball in fewest possible weighing? – Germann Arlington Jul 25 '13 at 21:00
  • @Germann Arlington, yes, i am looking into that kind of sort – AKB Jul 25 '13 at 21:04
  • @RohitJain Are you sure Singleton wouldn't work here? Singleton is good for everything. – Reinstate Monica -- notmaynard Jul 25 '13 at 21:07
  • 1
    You should attempt the problem or show some more effort before asking a question – Paul Renton Jul 25 '13 at 21:08
  • @iamnotmaynard singleton has nothing to do with this. But if you're seriously thinking on using a design pattern no matter what, strategy could do it. – Luiggi Mendoza Jul 25 '13 at 21:08

1 Answers1

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.

Jan Dörrenhaus
  • 6,581
  • 2
  • 34
  • 45
  • 1
    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?](http://meta.stackexchange.com/a/118694/182862), section 10. – Luiggi Mendoza Jul 25 '13 at 21:04
  • 1
    Attacking people who just want to help, however, will improve the entire SO site. – Jan Dörrenhaus Jul 25 '13 at 21:09
  • 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. – Luiggi Mendoza Jul 25 '13 at 21:15
  • 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? – AKB Jul 25 '13 at 21:33
  • 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. – Jan Dörrenhaus Jul 25 '13 at 21:36
  • List myBallsList = new ArrayList(); myBallsList.add("ball1");myBallsList.add("ball2")....; is it like this? – AKB Jul 25 '13 at 22:33
  • Yup. Add your ball objects, then run the sort, using the comparator. Or, if you just have a List of Integer objects instead of Ball objects, you can sort that immediately, and not even write any Comparator. – Jan Dörrenhaus Jul 25 '13 at 22:40