Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Inner for loop in Android Studio

I have a Java code that I write in Android Studio. I have two arraylists (dates and visitors). I want to add one date and the corresponding number of visitors of the date, but when I run my code, it adds the date successfully, but it adds all the number of visitors. I have 6 dates, and the first date is 2021-12-28 and the code adds 6 number of visitors to this date, then it goes to the second date and adds 6 number of visitors again. Here is my code:

ArrayList<PieEntry> barEntries = new ArrayList<>();
for (int i = 0; i < dates.size(); i++){
    for (int a = 0; a < visitors.size(); a++){
        String date= dates.get(i);
        int numberofvisitors = visitors.get(a);
        barEntries.add(new PieEntry(numberofvisitors , date));
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In your code, you are looking at all visitors every time you look at one date. That is what the inner loop is doing.

You want to look at one date, and one visitor count each time.

Get rid of the inner loop and use your i variable to reference your date and your visitor list.

ArrayList<PieEntry> barEntries = new ArrayList<>();
for (int i = 0; i < dates.size(); i++){                         
    String date= dates.get(i);                              
    int numberofvisitors =  visitors.get(i);                             
    barEntries.add(new PieEntry(numberofvisitors , date));
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading