Java 数组和 ArrayList

发布于 2024-12-06 02:52:49 字数 266 浏览 0 评论 0原文

当我尝试使用 Arrays.asList(myArray) 从数组创建 ArrayList myArrayList 时,我没有获取元素的列表myArray。相反,我得到了Array列表。

myArrayList 的大小为 1 。当我尝试执行 myArrayList.toArray() 时,我得到一个二维数组。如何获取列表中 myArray 的元素?迭代是唯一的选择吗?

When I try to create an ArrayList myArrayList from an array, using Arrays.asList(myArray), I am not getting the List of elements in myArray. Instead I get list of Array.

The size of myArrayList is 1 . When I try to do myArrayList.toArray(), I am getting a two dimensional array. What to do to get the elements of myArray in a list? Is iterating the only option??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

霊感 2024-12-13 02:52:49

首先,asList 方法是正确的方法:

Integer[] myArray = new Integer[3];
List<Integer> myArrayList = Arrays.asList(myArray);
System.out.println(myArrayList.size()); // prints 3, as expected

问题可能是您调用 varargs asList 方法的方式使得 java 将您的参数解释为第一个 varargs 值(而不是值数组)。

Object myArray = new Integer[3];
List<Object> myArrayList = Arrays.asList(myArray);
System.out.println(myArrayList.size()); // prints 1 - java invoked it as an array of Integer[]

要解决此问题,请尝试将参数转换为 Object[] 以强制调用可变参数,例如:

Object myArray = new Integer[3];
List<Object> myArrayList = Arrays.asList((Object[]) myArray); // Note cast here
System.out.println(myArrayList.size()); // prints 3, as desired

Firstly, the asList method is the right method:

Integer[] myArray = new Integer[3];
List<Integer> myArrayList = Arrays.asList(myArray);
System.out.println(myArrayList.size()); // prints 3, as expected

The problem may be that you are calling the varargs asList method in such a way that java is interpreting your parameter as the first varargs value (and not as an array of values).

Object myArray = new Integer[3];
List<Object> myArrayList = Arrays.asList(myArray);
System.out.println(myArrayList.size()); // prints 1 - java invoked it as an array of Integer[]

To fix this problem, try casting your parameter as Object[] to force the varargs invocation, eg:

Object myArray = new Integer[3];
List<Object> myArrayList = Arrays.asList((Object[]) myArray); // Note cast here
System.out.println(myArrayList.size()); // prints 3, as desired
唔猫 2024-12-13 02:52:49

myArray 的类型是什么?您不能将Arrays.asList 与基本类型的数组(例如int[])一起使用。在这种情况下你需要使用循环。

What is the type of myArray? You cannot use Arrays.asList with an array of primitive type (such as int[]). You need to use loop in that case.

神回复 2024-12-13 02:52:49

There are different ways by which you can achieve it.3 example of converting array to arraylist and arraylist to array in java might help.

不爱素颜 2024-12-13 02:52:49

这行得通吗?

Object[] myArray = new Object[4]; //change this to whatever object you have
ArrayList<Object> list = new ArrayList<Object>();
for (Object thing : myArray) list.add(thing);

Would this work?

Object[] myArray = new Object[4]; //change this to whatever object you have
ArrayList<Object> list = new ArrayList<Object>();
for (Object thing : myArray) list.add(thing);
一个人练习一个人 2024-12-13 02:52:49

尝试在方法调用中提供泛型类型。下面给出了 2 个 String 元素的列表。

    String[] strings = new String[]{"1", "2"};
    List<String> list = Arrays.<String>asList(strings);
    System.out.println(list.size());

Try providing the generic type in the method call. The following gives me a list of 2 String elements.

    String[] strings = new String[]{"1", "2"};
    List<String> list = Arrays.<String>asList(strings);
    System.out.println(list.size());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文