0

This is very strange. I have this snippet of code that is being executed in android:

private int doStuff(String assignment, List<String> assignments) {
    int iterations = 0;
    for (int i = 0; i < assignments.size(); i++) {
        iterations++;
        if (assignments.get(i) != assignment) {
            return i;
        }
    }
    Log.d(TAG,String.valueOf("Iterated "+ iterations +" over list with size "+assignments.size()));
    return 0;
}

Nothing comes out from the logging. I have here a link to a video where I am debugging though the steps. The size of the list is 3 but it stops after 1 iteration. I have run clean. I have run adb uninstall. I have restarted the computer. I don't get it, why does this stop looping in the first iteration?

Note: I reach the last line of the method. I never reach "return i;". Even if string comparison is bad then it shouldn't reach the last row of the method in the way it does on the video.

https://youtu.be/Q5MDCz4YzbA

why_vincent
  • 2,172
  • 9
  • 33
  • 49
  • 2
    `if (!assignments.get(i).equals(assignment)) {` – Andy Turner Oct 12 '16 at 11:42
  • Your if (assignments.get(i) != assignment) is true and is returning in the first iteration itself. – Febi M Felix Oct 12 '16 at 11:43
  • No, look at the video. The debugger shows that I reach the last row that returns 0 at the end of the method. – why_vincent Oct 12 '16 at 11:44
  • Event better bevause of impossibility to get null pointer exception if (!TextUtils.equals(assignments.get(i),assignment)) – loshkin Oct 12 '16 at 11:46
  • That is not relevant. I am not looking for an improvement on the boolean condition. – why_vincent Oct 12 '16 at 11:48
  • Please give your inputs, your expected outputs, and your actual outputs. If you never reach the `return i;` or the logging statement, there must be an exception being thrown in the code. Don't use offsite resources (like youtube videos): include *all* relevant information in your question. – Andy Turner Oct 12 '16 at 11:50
  • if (!assignments.get(i).equalsIgnoreCase(assignment)) { Try this line – Reena Oct 12 '16 at 11:51
  • 1
    @why_vincent `assignments.get(i) != assignment` evaluates to true and `return i` statement executes. I watched the video and yes you are right, the debugger looks like it returns 0 but this always happens while debugging. Believe me `return 0` statement does not execute, instead it returns `i`. You need to change your boolean condition do the following: `!assignments.get(i).equals(assignment)` – yrazlik Oct 12 '16 at 11:54

0 Answers0