英文:
How can I convert a String array to a different Class ArrayList in Java
问题
我正在尝试找出如何编写逻辑将字符串数组转换为ArrayList工作日对象
String[] weekdays = request.getParameterValues("weekday");
我已经将我的ArrayList<WeekDay> weekdays2的长度设置为与我的String[] weekdays相同。
ArrayList<WeekDay> weekdays2 = new ArrayList<WeekDay>(weekdays.length);
我知道有一种方法可以利用枚举,这就是我正在尝试做的。
for (int i = 0; i < weekdays.length; i++) {
weekdays2.add(WeekDay.valueOf(weekdays[i]));
}
我知道我肯定遗漏了一些内容。
错误:
Enum<WeekDay>类型中的方法valueOf(Class<T>, String)对于参数(ArrayList<WeekDay>, String[])不适用。
英文:
I'm trying to figure out how to write logic to convert an array of string into an ArrayList weekday object
String[] weekdays = request.getParameterValues("weekday");
I've made my ArrayList<WeekDay> weekday2 the length of my String[] weekdays.
ArrayList<WeekDay> weekdays2 = new ArrayList<WeekDay>(weekdays.length);
I know there is a way to utilize enums, and that's what I'm trying to do.
for (int i = 0; i < weekdays.length; i++) {
weekdays2.add(new WeekDay().valueOf(weekdays2, weekdays));
}
I know I'm definitely missing something.
error:
The method valueOf(Class<T>, String) in the type Enum<WeekDay> is not applicable for the arguments (ArrayList<WeekDay>, String[])
答案1
得分: 1
Code to get list of WeekDay may be such:
for (int i = 0; i < weekdays.length; i++) {
weekdays2.add(WeekDay.valueOf(weekdays[i]));
}
英文:
Code to get list of WeekDay may be such:<br>
for (int i = 0; i < weekdays.length; i++) {
weekdays2.add(WeekDay.valueOf(weekdays[i]));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论