Java Bubble Sort ArrayList Exercise Issue -
Java Bubble Sort ArrayList Exercise Issue -
i have java project trying utilize bubble sort sort arraylist
of books rating.
i have 3 classes.
books library sorti'm having problem getting bubble sort work, have tried either returns error, or random characters/numbers. if help appreciate it.
books
public class books { string title; int rating; public video(string ptitle, int prating) { title = ptitle; rating = prating; } public string gettitle() { homecoming title; } public int getrating() { homecoming rating; } }
library
import java.util.arraylist; public class library { public static void main (string [] args) { arraylist<book> booklist = new arraylist<books>(); book book1 = new book ("thisbook 5", 5); book book2 = new book ("thisbook 2", 2); book book3 = new book ("thisbook 3", 3); book book4 = new book ("thisbook 4", 4); book book5 = new book ("thisbook 1", 1); booklist.add(book1); booklist.add(book2); booklist.add(book3); booklist.add(book4); booklist.add(book5); system.out.println("original sequence"); (int cnt = 0; cnt < booklist.size(); cnt++) { system.out.println(booklist.get(cnt).gettitle() + " " + booklist.get(cnt).getrating()); } system.out.println( "\r" + "sorted rating"); sortalgorithm sortobject = new sortalgorithm(); sortobject.calc(booklist); system.out.println(booklist.get().gettitle() + " " + booklist.get().getrating()); } }
i've been searching different bubble sort algorithms nil seems work. think problem has been both class names , bubble sort algorithm. i'm banging head here. if can help me i'd grateful.
this complicated code no reason..
public class sort { public static void calc(arraylist<book> booklist) { (i = 0 ; < booklist.size() ; i++) { (j = i+1; j < booklist.size() ; j++) { if (booklist.get(i).getrating() > booklist.get(j).getrating()) { collections.swap(booklist, i, j); } system.out.println(booklist.get(out).gettitle() + " " + booklist.get(out).getrating()); } } } }
hope helps
java arraylist bubble-sort
Comments
Post a Comment