如何将一个String数组转换为Java中不同的Class ArrayList。

huangapple go评论70阅读模式
英文:

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(&quot;weekday&quot;);

I've made my ArrayList<WeekDay> weekday2 the length of my String[] weekdays.

ArrayList&lt;WeekDay&gt; weekdays2 = new ArrayList&lt;WeekDay&gt;(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 &lt; weekdays.length; i++) {
			weekdays2.add(new WeekDay().valueOf(weekdays2, weekdays));
		}

I know I'm definitely missing something.

error:

The method valueOf(Class&lt;T&gt;, String) in the type Enum&lt;WeekDay&gt; is not applicable for the arguments (ArrayList&lt;WeekDay&gt;, 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 &lt; weekdays.length; i++) {
        weekdays2.add(WeekDay.valueOf(weekdays[i]));
    }

huangapple
  • 本文由 发表于 2020年9月3日 10:09:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63715819.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定