0

I am practicing object orientation here entering in basketball player names and how many points scored and rebounds grabbed.

How would I go through each element in an array of objects to find the last player with an even amount of points?

This is the code I have so far to enter the information. What do I need to do in my second forloop to examine each element and then display the last element that fits my criteria?

class basketballObj
{
    public static void main (String[]args)
    {
        Basketball bbArray[];
        String theName;
        int thePoints;
        int theRebounds;
        int index;
        int noOfElements = 0;

        bbArray = new Basketball[3];

        for(index = 0; index < bbArray.length; index++)
            {
                System.out.println("Enter a name ");
                theName = EasyIn.getString();
                System.out.println("Enter points scored ");
                thePoints = EasyIn.getInt();
                System.out.println("Enter rebounds grabbed ");
                theRebounds = EasyIn.getInt();
                bbArray[index] = new Basketball(theName, thePoints, theRebounds);
                noOfElements++; 
            }
        for(index = 0; index < bbArray.length; index++)
            {
                if(bbArray[index].getPoints() % 2 == 0)
                    {

                    }
            }
    }
}

3 Answers 3

4

Well if you want to find the last player with an even amount of points, you don't actually want to go through each element ;-). Try:

for(index = bbArray.length-1; index >= 0; index--)
        {
            if(bbArray[index].getPoints() % 2 == 0)
                {
                   //add whatever relevant code here.
                   break; //this line breaks the for loop (because you've found a player with an even amount of score
                }
        }

we start at bbArray.length-1 because while you array contains 3 elements, arrays are zero-indexed. Meaning that to get the first element, you will have to call bbArray[0]. Similarly call bbArray[2] for the last element.

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

1 Comment

You will want to start at bbArray.length - 1 and have your condition be index >= 0
1

Simple. Iterated your array backwards.

boolean found = false;
for(int index=array.length-1; index>-1 && !found; index--) {
    if(array[index].getPoints()%2 == 0) {
        // found element. Break out of for loop
        found=true;
    }
}

Comments

1

You've pretty much got it.

Create a temporary, uninitialized variable Basketball temp; before the for loop that iterates through the bbArray and then set it equal to the bbArray[index] if the if condition is met.

If you want to save the index it was found at then create an int indexFound; as well.

Looping through it backwards as user2651804 suggested yields this:

public class basketballObj
{
    public static void main(String[] args)
    {

    ...

        Basketball temp;
        int indexFound = -1;

    ...


        for(index = bbArray.length - 1; index >= 0; index++)
            {
                if(bbArray[index].getPoints() % 2 == 0)
                    {
                        temp = bbArray[index];
                        indexFound = index;
                        break;
                    }
            }


        //note temp will be null if no scores were even
        //if (temp != null)
        //you can use the above if statement if you don't want to use indexFound

        //you can also just check if indexFound == -1
        if (indexFound != -1)
            {
                System.out.println("Found at index: " + indexFound);
                //
            }
    }
}

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.