gpt4 book ai didi

Java for循环只存储最后一个值

转载 作者:行者123 更新时间:2023-11-30 06:17:51 25 4
gpt4 key购买 nike

我在 java android 中有一个 for 循环,它运行完美,但它只存储到它循环遍历的列表中的最后一项。我应该将 user_idlesson_id 设为列表吗?我不知道从这里该去哪里,但这就是我所做的。该逻辑工作正常,但它只保存两个变量中列表中的一项。

Android Studio 代码:

// loop to add every user and lesson selected to watch
String user_id = null;
int lesson_id = 0;

for (int i = 0; i<attendees.size(); i++){
for (int j = 0; j<lesson_iDs.size(); j++){
user_id = attendees.get(i);
lesson_id = lesson_iDs.get(j);
}
}
// get the date for the classWatch
Calendar localCalendar = Calendar.getInstance(TimeZone.getDefault());
Date currentTime = localCalendar.getTime();

// to show in patient activity (sends to SQLite)
watch = new Watch(currentTime, user_id, lesson_id);

最佳答案

这正常吗?如果您在循环之外使用 user_id 和 Lesson_id,则使用最后的值。难道你不想做这样的事情:

// loop to add every user and lesson selected to watch
String user_id = null;
int lesson_id = 0;
// get the date for the classWatch
Calendar localCalendar = Calendar.getInstance(TimeZone.getDefault());
Date currentTime = localCalendar.getTime();

for (int i = 0; i<attendees.size(); i++){
for (int j = 0; j<lesson_iDs.size(); j++){
user_id = attendees.get(i);
lesson_id = lesson_iDs.get(j);
// to show in patient activity (sends to SQLite)
watch = new Watch(currentTime, user_id, lesson_id);
// Do something with watch...
}
}

关于Java for循环只存储最后一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48809435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com