0

I want to sort out my Arraylist<Book> b on the values returned by the method numPoints() in class Book. show() is a method of class Book, that prints out the code. Everything else works okay, just the method sort() is the problem i think.

public class som {
private static ArrayList<Book> b;
public static void main(String[] args) {
b = new ArrayList<Book>();
...
}
...
public static void sort() {
 Book mon = null;
 int indMax = 0;
 for(int i=0; i<b.size()-1; i++){
 mon = b.get(i);
 indMax = i;
 for(int j=i+1; j<b.size(); j++){
 if(b.get(j).numPoints() > mon.numPoints()){
 mon = b.get(j);
 }
 }
 if(indMax != i){
 b.set(indMax, b.get(i));
 b.set(i, mon);
 }
 }
 for(int s=0; s<b.size(); s++) {
 b.get(j).show();
 }
}
class Book {
...
 public double numPoints() {
 ...
 }
 public void show() {
 ...
 }
...
}

i also tried the following code, but neither of them work properly.

public static void sort() {
 ArrayList<Book> o = new ArrayList<Book>();
 double vMax=0;
 int iMax=0;
 while(b.size()!=0) {
 for(int i = 0; i<b.size(); i++) {
 if(vMax<b.get(i).numPoints()) {
 vMax=b.get(i).numPoints();
 iMax=i;
 }
 }
 o.add(b.get(iMax));
 b.remove(iMax);
 }
 for(int j=0; j<o.size(); j++) {
 o.get(j).show();
 }

The first one just doesn't work properly, but in the second one terminal prints out :

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2

can somebody please help me figure out what is wrong?

Gilles Gouaillardet
8,44111 gold badges26 silver badges34 bronze badges
asked Jan 14, 2018 at 0:10
3
  • class Object is going to be extremely confusing. You should name the class something besides Object. As to sorting the list, please read the Oracle Java tutorial on "Object Ordering" Commented Jan 14, 2018 at 0:11
  • okay, thanks. I actually named it differently in my original code, but because it wasn't in english, i renamed it for this question so it wouldn't be confusing. guess i chose the wrong name to do this haha Commented Jan 14, 2018 at 0:14
  • i edited the question, so instead of Object there is a class Book Commented Jan 14, 2018 at 0:21

2 Answers 2

1

It seems that you are interested in having working code, not implementing sorting from scratch.

Just use already implemented sort method of list:

ArrayList<Book> list;
list.sort(Comparator.comparing(Book::numPoints));

The sort method accepts Comparator.Comparator's job is to compare objects.

Function Comparator.comparing creates comparators. It accepts one parameter that is a Function. Function's job is to provide substitute for an object that would be compared. so the substitute can be compared instead.

Because you want to compare Books by what Book.numPoints method returns, we need to create a function that for each Book returns result of Book.numPoints method. Because Function is a functional interface we can use method reference (Book::numPoints) to provide the function.

Then you can call show on all of them:

list.forEach(Book::show)

Not only it is simple, but it is easy to modify.

answered Jan 14, 2018 at 1:07

Comments

1

For java>= java 8

b.sort((b1,b2)-> b1.numPoints()-b2.numPoints());

For java < java 8

Collections.sort(b,new Comparator<Book>{
 @Override
 public int compare(Book b1, Book b2) {
 return b1.numPoints() - b2.numPoints();
 }
});
answered Jan 14, 2018 at 1:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.