Java:我可以将对象列表转换为String []列表,反之亦然吗? [英] Java: Can I convert List of Object to List of String[] and vice versa?

查看:104
本文介绍了Java:我可以将对象列表转换为String []列表,反之亦然吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是否可以不经过列表并转换对象?

Is this possible without going through the list and casting the objects?

我还需要转换 List< Object> 列表< T> (T =预定义对象)如果可能?

I also need to convert List<Object> to List<T> (T = predefined Object) if it's possible?

,我试图使用 List< Object> 作为在我的代码中广泛使用的类方法的返回类型。

for clarification, I'm trying to use List<Object> as a return type of a class method that is widely used in my code.

推荐答案

否。这不是一个有效的转换,因为不是所有的 Object 都是 String []

No. This is simply not a valid conversion, because not all Objects are String[].

您可以在两行代码中自行确定。

听起来你需要更一般的写法。类似的东西:

It sounds like you need to write the method more generically. Something like this:

public <T> List<T> getGenericList()
{
    return new ArrayList<T>();
}

这可以返回 List< String [] ; 像这样:

List<String[]> listOfStringArr = getGenericList();

这篇关于Java:我可以将对象列表转换为String []列表,反之亦然吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆