3

how do I sort an array of objects? this is my code for sorting the array, I receive a "not a statement" error for: Movie temp = movies[b]; what do i declare the temp variable as if it is to hold the exact value/reference of movies[b]; which could be any of three different object types which are in the same array? I am new to programming so I apologize if i seem to be ignorant; please feel free to correct me or ask questions if I phrased the questions incorrectly.

public static String bubbleSort(Movie[] movies) {
    for (int a=1; a<movies.length; a++) {
        for(int b=0; b<movies.length - a; b++) {
            if (((movies[b].getTitle()).compareTo((movies[b+1].getTitle()))) > 0)
                //swap movies[b] with movies[b+1]
                Movie temp = movies[b];
            movies[b] = movies[b+1];
            movies[b+1] = temp;
        }
    }
}
1
  • You'll want to put brackets around the 3 statements following the if clause. Without the brackets, only the first statement after the clause - Movie temp = movies[b]; - will execute conditionally. The other two statements will always execute. Commented Mar 30, 2012 at 20:19

4 Answers 4

3

When an array is defined as Movie[] it can only contains objects of type Movie. So you can only have Movies in there. However, to make this general, you should define the type as Object and the array as Object[].

However, in your code, you are assuming that you really do have Movie objects because you're using Movie.getTitle(). You will not be able to access that from references of Object. I would recommend having your objects implement Comparable and using the type Comparable as the type of the array and your temporary variable.

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

Comments

2

The Movie temp = movies[b]; is a declaration, not a statement. You want this:

        if (((movies[b].getTitle()).compareTo((movies[b+1].getTitle()))) > 0)
        {
            //swap movies[b] with movies[b+1]
            Movie temp = movies[b];
            movies[b] = movies[b+1];
            movies[b+1] = temp;
        }

Note all I did was add braces around all the swap code, making it into a block which can contain a declaration. I think this is what you intended, but just omitted the braces.

1 Comment

exactly what i needed!
2

You were missing a { after the test, and incorrectly promising to return a String. Try this,

public static void bubbleSort(Movie[] movies) {
    for (int a = 1; a < movies.length; a++) {
        for (int b = 0; b < movies.length - a; b++) {
            if (((movies[b].getTitle())
                    .compareTo((movies[b + 1].getTitle()))) > 0) {
                // swap movies[b] with movies[b+1]
                Movie temp = movies[b];
                movies[b] = movies[b + 1];
                movies[b + 1] = temp;
            }
        }
    }
}

2 Comments

Yes! that was exactly it, I actually figured that out and came back to delete the post. thank you a ton for the quick response! I appreciate it greatly!
The next time you are in this situation solve it by reducing the method to just a single statement/expression or control structure and then re-add the rest of the code one part at a time until the compile error occurs. You will soon be past this stage on onto greater glories!
1

Use the Collections api and dont reinvent the wheel doing your own sorting implementation.

5 Comments

Implementing algorithms oneself is a good way to both learn a language and an algorithm. Go ahead Aiden and reinvent all the wheels you want!
While true, it might not give you real world practice with the language.. better to learn the API you will use day and day out like the Collections API. But sure if you just want to learn the language maybe. Even then there are probably better things than bubble sort...
If its homework it should be tagged as such. Should we do that?
Thank you for the information. The point of writing this is, indeed, to learn the language!
When it comes to Java learning the language to a VERY wide extend is learning to use the core API's, which is why I suggested to look at the collections api. Do that asap in parallel to getting the hang of Java the language..

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.