0

Ok so I want to find an algorythm that searches through some objects (in my case 4) and finds the object with the smallest member variable. Lets say you can get that value with object.getIntegerValue()

In my case I have 4 android layouts and want to find the layout with the smallest amount of childs.

I think there will be plenty of solutions but I just want to find a fast one. No matter how dirty and so on....

My Code so far is short, dirty and doesnt always return the object with the smallest member variable, but only has to serve as an Code Example here:

private LinearLayout layoutChanger(){
    int one, two, three;
    one = layoutOne.getChildCount();
    if ((two = layoutTwo.getChildCount()) <= one) {
        if ((three = layoutThree.getChildCount()) <= two) {
            if ((layoutFour.getChildCount()) <= three)
                return layoutFour;
            return layoutThree;
        }
        return layoutTwo;
     }
     return layoutOne;
 }

Edit: I know how to do this I rather wanted to get suggestions on how to speed up things...

Is Comparable really a fast one? Or should I distinct myself of OOP solutions to get better performance?

2
  • 3
    Implement Comparable? Commented Mar 24, 2014 at 14:18
  • Your code has a few problems. If three is the smallest, your method will never indicate that as long as two > one. Instead, it will return the value of two and will never test the value of three. Commented Mar 24, 2014 at 19:16

3 Answers 3

3

Just an example:

    int childCount;
    Layout[] myLayouts = {layoutOne,layoutTwo,layoutThree};
    Layout selected;
    for(Layout layout:myLayouts){
        if(childCount=0 || childCound>layout.getChildCount()) {
            selected = layout;
            childCount = layout.getChildCount();
        }
    }
    return layout;
Sign up to request clarification or add additional context in comments.

2 Comments

I don't like creating the array myLayouts. You could create an array of ints populated with getChildCount(). It'd be more memory efficient than creating an array just for the sake of looping through them.
I think the best way was to convert this to a method that will receive an indeterminate array of objects, something like getMinLayout(Layout... layouts)
1

The following is not Java code, this is just (Java like) pseudo code to give the OP an idea...

lowestPossibleValue = ?;
currentLowestValue = MAX;
foreach (object : collection) {
    if (object.getValue == lowestPossibleValue) {
        foundObject = object;
        break;
    } else {
        if (object.getValue < currentLowestValue) {
            foundObject = object;
        }
    }
}

// foundObject contains your result

5 Comments

Tha answer here is also correct, but this isn't Java
@GhostDerfel I never suggested that it was Java, the solution is too obvious and in such cases I prefer to avoid "giving codez" so the asker will have a chance to think at least a little
I think you misinterpreted my question cause I'm not trying to find a obvious solution, but a fast one.
The fast one is the obvious one - you have to check all elements to get the lowest value - there is no way around it. (Sort will have to do the same thing). The only way around iterating over all elements is if you know what is the lowest possible value and any of your entries have that value, then you can abandon the rest of the iterations.
@GermannArlington you have good intentions doing thing that way, but I think the best practice in this case is to make it clear in your answer this is just a pseudo code, since some one new to Java can see your answer and this can cause some confusion in their head
0
private LinearLayout layoutToggler(LinearLayout[] layoutArr){
        int currentChildCount;
        int minChildCount = MAX_VAL;
        LinearLayout retLayout = null;
        for(LinearLayout layout:layoutArr){
            if((currentChildCount = layout.getChildCount()) == MIN_VAL ){
                retLayout = layout;
                break;
            }
            else if(currentChildCount < minChildCount) {
                retLayout = layout;
                minChildCount = currentChildCount;
            }
        }
        return retLayout;
    }

With thanks to Arlington since it is his idea brought to a working solution.

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.