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));
}
}
CodePudding user response:
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));
}