superuserdo
superuserdo

Reputation: 1657

Weird: For loop returns last iteration under certain conditions

There is a weird problem I am facing. Whenever I update my array of objects by say adding an extra object so that the size is 5, my for loop changes its behavior. And when i revert it, it goes back to normal. Here is my code

String[] a = new String[4];
    a[0] = "EVENT 1";
    a[1] = "EVENT 2";
    a[2] = "EVENT 3";
    a[3] = "EVENT 4";

    LatLng[] b = new LatLng[4];
    b[0] = new LatLng(32.546586510595404, -80.70999909192324);
    b[1] = new LatLng(32.54606196179351, -80.71023747324944);
    b[2] = new LatLng(32.54620949145382, -80.70962257683277);
    b[3] = new LatLng(32.494091, -80.739474);

I then add the objects to a list by a for loop (that acts correctly) and then i have another for loop that retrieves these objects from a getter setter class

for(int i = 0; i <= event.size(); i++) {
            latB = event.get(i).getmLatLng().latitude;
            lngB = event.get(i).getmLatLng().longitude; }

So the behavior of the for loop goes from normal to only retrieving the last iteration for the appointed number of times. I have been looking for days on what exactly is causing the problem but i dont see anything wrong, especially since when i revert the arrays back to the way they were, the problem ceases. What is causing this problem?

EDIT*

Okay so this is the code that goes in between the first two. Here is the event class that creates Event Objects

public class Event{
public LatLng mLatLng;

public Event()
{

}
public Event(String id, LatLng latlng)
 {
        mLatLng = latlng;
        mId = id;


 }



public void setmLatLng(LatLng latLng)
{
    this.mLatLng = latLng;
}

public LatLng getmLatLng()
{
    return this.mLatLng;
}}

Then in the same activity as before, I add those events to a list and store the list in an sql Lite database

int TOTALEVENTS = a.length-1;



    for(int i = 0; i <= TOTALEVENTS; i++)
    {

        EVENTS.add(i, new Event(new String(EVENTNAME[i]), EVENTLOCATION[i]));

    } db.addEvents(EVENTS);

Then I go to the code that I had before and the SQL Lite Database returns the stored list

List<Event> event = new ArrayList<Event>();
        event = db.getEvents();

Upvotes: 1

Views: 94

Answers (2)

superuserdo
superuserdo

Reputation: 1657

So I have not fixed this problem but I have found a solid work around. I converted all of my arrays to lists. However the root of the problem after I debugged it was getting the events from the sqllite db. The events were being stored correctly but not retrieved correctly for some very, very strange reason. And to add to the mystery of this bug, the problem was with the number 3. If the size of the lists or array was equal to 4, the events would not be retrieved correctly resulting in the bug. So after converting the arrays to lists and increasing the size of the lists, the bug ceased.

Upvotes: 0

Nir Alfasi
Nir Alfasi

Reputation: 53525

I'm not sure how it works at all since you're using <= in the for loop while you should be using <:

for(int i = 0; i <= event.size(); i++)

should be:

for(int i = 0; i < event.size(); i++)

Further, you didn't show us important part of the code: you're using event.get(i)... - the get() is a list annotation - not an array. That means that you're doing some kind of "translation" from array to the object which is called event in your code. How does this translation performed ? the bug could be there as well.

Upvotes: 1

Related Questions