0
public static boolean binarySearch(ArrayList<Student> students, int search) {
    int first = 0;
    int last = students.size() - 1;
    int mid;
    while (first <= last) {
        mid = first + (last - first) / 2;
        if (search == students.getTotal(i)) {
            return true;
        } else if (students.compareTo(students.get(mid)) < 0) {
            last = mid - 1;
        } else {
            first = mid + 1;
        }
    }

    return false;
}

In this method I am supposed to return true if search is found, and false if search is not found.
However I am unable to retrieve information from the ArrayList.

There are two errors here.

One at

search == students.getTotal(i)

Another at

students.compareTo(students.get(mid))

For the first one I am supposed to access students and compare the i to search but I am unable to retrieve the data in students.

In the second one I am supposed to get if the search were to continue in the LEFT or RIGHT side.

Can anyone give me any tips on how to solve this? I might know that I am using the wrong way to get data but I need a direction to go to.

POH
  • 11
  • 1
  • 3

2 Answers2

1

TL;DR: The problem is that you confuse the ArrayList students and its members.
You apply methods to students, the ArrayList, that you should apply to its individual members.

A more in-depth explanation:

As is pointed out in comments by @Darshan, ArrayList does not have a method getTotal. Even if it did, this code would always return the same value.

You probably meant

search == students.get(i).getTotal()  

This takes the i-th student, and applies getTotal() to that instance of Student. Returning the getTotal result for that particular student.

You essentially make the same mistake in the second part:

students.compareTo(students.get(mid)) < 0)

Here, again you take the ArrayList students and try to apply a method to the ArrayList, rather than to an individual student.

The solution is again: take the specific student from the list (Student #i, identified by students.get(i), and apply the compareTo method to that.

The official documentation for ArrayLists is here.

  • i still get an error of "The method elementAt(int) is undefined for the type ArrayList" is there any website i can read up on for the calling of elements ? – POH Feb 13 '17 at 15:55
  • @POH My bad, in ArrayLists it's called `get`. See the [official documentation for ArrayList](https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html). I've updated the answer. – S.L. Barth is on codidact.com Feb 13 '17 at 15:57
  • i tried your coding for "students.compareTo(students.get(mid))" but it wasnt part of the documentation that you gave me and there isnt any way to compare two strings is there an alternative method that i can follow – POH Feb 14 '17 at 06:37
  • @POH That documentation is for ArrayLists. An ArrayList is a container, it contains elements. A bit like a child's toy box: you could do things with the box (move it, stack it on another box, shove it...) without caring which toys are actually inside it. Or, you could take a toy out of it and do something with that toy. Similarly, you have an ArrayList of students. `get` takes an individual student from the list. Then you need to find operations that operate on the Student class. BTW the documentation for java Strings is [here](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html). – S.L. Barth is on codidact.com Feb 14 '17 at 08:09
1

First of all I think it's mid=(first+last)/2.

Second of all, I would use an array because it's easier. But if you specifically want to use an ArrayList then you can do the following:

public static boolean binarySearch(ArrayList<Student> students, int search) {
    int first = 0;
    int last = students.size() - 1;
    int mid;
    while (first <= last) {
        mid = ( first + last ) / 2;
        if (search == students.get(i)) {
            return true;
        } else if (search.compareTo(students.get(mid)) > 0) {
            last = mid - 1;
        } else {
            first = mid + 1;
        }
    }

    return false;
}

Explanation: with the ArrayList.get you get just the element of the ArrayList at the specific location i that you want to compare to the mid element.
The mistake you are making is that you compare the whole list students to a specific element of the list (the middle one), where you should compare the value that you are looking for with the middle element of the list to see if it is greater or less.

  • Welcome to Stack Overflow! I took the liberty of editing your answer; you can put variable names in `code markdown` by putting backticks around them. Anyway, you've got the right answer - OP is confusing the list and the individual elements. – S.L. Barth is on codidact.com Feb 13 '17 at 13:49
  • all the website i went to find binarysearch were all mid = first + (last - first) / 2; is there any website i can look at for converting array to arraylist ? – POH Feb 13 '17 at 15:54