Java 如何将对象类转换为 arrayList ?爪哇
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25055508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert object class to arrayList ? java
提问by david
I have this code :
我有这个代码:
ArrayList<Integer> users = new ArrayList<Integer>();
Map <String,Object> hashMap = new HashMap <String,Object> ();
hashMap.put("users",user);
// now I want to get users as array list
hashMap.get("users")[i];//error
How to users as array ? thank you
如何将用户作为数组?谢谢你
回答by Ninad Pingale
Because it is ArrayList
and not array,You can get it like this-
因为它是ArrayList
而不是数组,你可以这样得到它-
public static void main(String args[]) throws Exception
{
ArrayList<Integer> users = new ArrayList<Integer>();
users.add(5);
users.add(10);
Map <String,Object> hashMap = new HashMap <String,Object> ();
hashMap.put("users",users);
ArrayList<Integer> no = ((ArrayList<Integer>) hashMap.get("users"));
for (Integer integer : no) {
System.out.println(integer);
}
}
output-
输出-
5
10
Now to convert ArrayList to Array -
现在将 ArrayList 转换为 Array -
Integer[] arr = no.toArray(new Integer[no.size()]);
回答by EpicPandaForce
List<Integer> users = new ArrayList<Integer>();
Map<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("users",user);
....
....
....
List<Integer> users = (List<Integer>)hashMap.get("users"); //assuming this is another method and 'users' is not defined here yet
Integer[] usersArray = users.toArray(new Integer[users.size()]);
System.out.println("" + usersArray[i]);
Please note that this is an atrocity and you shouldn't be storing that List as an Object, you are pretty much circumventing what Generics are for.
请注意,这是一种暴行,您不应该将该 List 存储为对象,您几乎是在规避泛型的用途。
回答by Simon Verhoeven
This code will work if you want to access it as an ArrayList:
如果您想将其作为 ArrayList 访问,此代码将起作用:
List<Integer> users = new ArrayList<Integer>();
Map<String, List<Integer>> hashMap = new HashMap<String, List<Integer>> ();
hashMap.put("users",users);
hashMap.get("users");
Your code won't work since you were storing it as an Object. So you would need a cast. And even then you use []
for arrays, not ArrayLists
由于您将其存储为对象,因此您的代码将无法工作。所以你需要一个演员。即便如此,你也使用[]
数组,而不是 ArrayLists
If you want to use it as an array you can change the last line to:
如果要将其用作数组,可以将最后一行更改为:
hashMap.get("users").toArray()
And then you can use [1]
, or just do .get(1) on the initial code.
然后你可以使用[1]
, 或者只是在初始代码上执行 .get(1) 。
Alternatively, if you want to use hashMap as a property bag as someone validly mentioned you would need to do a cast:
或者,如果您想像有人有效提到的那样使用 hashMap 作为属性包,您需要进行强制转换:
((ArrayList<Integer>) hashMap.get("users"))
Then you can use users as an ArrayList
as you are getting an Object
by default from the HashMap
.
然后你可以使用的用户作为一个ArrayList
为你得到一个Object
从默认HashMap
。
回答by Ruchira Gayan Ranaweera
You can try something like following
您可以尝试以下操作
List<Users> usersList = new ArrayList<>();
Map<String,List<Users>> hashMap = new HashMap <> ();
hashMap.put("user",usersList);
Now
现在
hashMap.get("user")
Will return a List
of Users
, Now you can use
将返回 a List
of Users
,现在您可以使用
hashMap.get("user").get(0); // to get 0th index user