0

This is my first time here and I am quite new to coding on Android Studio. I am trying to fill up an ArrayList using for loop but my coding on the for loop has some issues. I am also not sure what information I have to show. I think the problem might be adding variables to the array in the loop. Some of my code:


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

                if(beverage.get(i).getCalorie()>=(inputcalorie-100)&&beverage.get(i).getCalorie()<=(inputcalorie+100)){


                    foodtest=beverage.get(i).getFood();
                    calorietest=beverage.get(i).getCalorie();
                    Log.d("MyActivity","foodtest=" + foodtest);
                    Log.d("MyActivity","calorietest=" + calorietest);

                        foodis.add(new foodis(foodtest,calorietest));

                        Log.d("MyActivity","food=" + foodis.get(i).getFooddisplay());
                        Log.d("MyActivity","cal=" + foodis.get(i).getCaldisplay());


            }

Edit: I made an beveragearraylist with data from a csv file (shown below)

Coffee,0
Coffee With Milk,150
Milo,371
Milo 50% Less Sugar,140
Apple Juice,46
Orange Juice ,45
Milk,149
Low Fat Milk,110

Class foodis:


public class foodis {

    public String fooddisplay;
    public Double caldisplay;

    public foodis(String fooddisplay, Double caldisplay) {
        this.fooddisplay = fooddisplay;
        this.caldisplay = caldisplay;
    }

    public String getFooddisplay() {
        return fooddisplay;
    }

    public void setFooddisplay(String fooddisplay) {
        this.fooddisplay = fooddisplay;
    }

    public Double getCaldisplay() {
        return caldisplay;
    }

    public void setCaldisplay(Double caldisplay) {
        this.caldisplay = caldisplay;
    }

}

Class beverage:

public class beverage {
    private String food;
    private Double calorie;


    public String getFood() {

        return food;
    }

    public void setFood(String food) {

        this.food = food;
    }

    public Double getCalorie() {

        return calorie;
    }

    public void setCalorie(Double calorie)
    {
        this.calorie = calorie;
    }




    @Override
    public String toString() {
        return "FoodSample{" +
                ", food='" + food + '\'' +
                ", calorie=" + calorie +
                '}';
    }
}
4
  • I want to clarify that when I changed the upper limit of the loop to foodis.size() the loop doesnt work at all. I thought that foodis.size() initial value will be 0 . Commented Jul 16, 2020 at 17:41
  • What are the arrays foodis and beverage ? How are they initialized ? Commented Jul 16, 2020 at 17:52
  • The meaning here is unclear. It would probably be a lot simpler to say for (Beverage b: beverages) instead of trying to somehow mix the contents of beverages and the count of foodis. Commented Jul 16, 2020 at 17:54
  • I edited the post and added some code related to the arrays. Currently, I am trying to add data from beverages into foodis through the for loop but for loop doesnt work. Commented Jul 17, 2020 at 3:01

1 Answer 1

3

Arrays in Java start from 0 (Zero), so foodis.size() will return 4 if there are 4 entries in the list but the index ends at 3 as it starts from 0.

java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 
Sign up to request clarification or add additional context in comments.

5 Comments

"so foodis.size() will return 3 if there are 4 entries in the list." – if there are 4 entries in the list then the size is 4 and the size method will report as such; the last index, however, will be 3.
You got the point, I will re-phrase it in the answer ;)
Thanks for the answer! But the for loop doesn't work when i use foodis.size() , the java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 only happens when I change foodis.size() to an Integer. To clarify, Arrays in Java start from 0 but does it work the same way for Arraylist? Because the for loop should work if the ArrayList size starts from 0.
I guess you edited the initial question/code, as now I see that you are adding to the same array you are looping trough..
@deftVG An ArrayList is zero based; this is a property of all List implementations, not just ArrayList. As for your code, you seem to be iterating over the indices of foodis yet you're accessing beverage with those indices, thus you're assuming beverage has the same size as foodis.

Your Answer

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.