博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java数组到列表(ArrayList)的转换
阅读量:2529 次
发布时间:2019-05-11

本文共 3739 字,大约阅读时间需要 12 分钟。

Sometimes we need to convert Array to List in java, here we will learn two different ways to achieve this. Since List is an interface and ArrayList is the most popular implementation, it’s the same as converting an Array to ArrayList. This situation can come when you are invoking some third party classes returning an array and then you need to change them to list, or to add some more data to the list.

有时我们需要用Java将Array转换为List ,在这里我们将学习两种不同的方法来实现这一点。 由于List是接口,而ArrayList是最流行的实现,因此它与将Array转换为ArrayList相同。 当您调用某些第三方类返回一个数组,然后需要将它们更改为列表,或向列表中添加更多数据时,可能会出现这种情况。

列出的Java数组 (Java Array to List)

There are two built-in ways to convert Array to List in Java.

有两种内置方法可以将Java中的Array转换为List。

  1. Arrays.asList(T… a): This is the simplest way to convert to but this method returns the underlying representation of the array in the form of ArrayList. The returned ArrayList is fixed-sized and any attempt to modify that will result in UnsupportedOperationException at runtime. Also, any change in the array will change the elements in ArrayList also.

    Arrays.asList(T…a) :这是 转换为的最简单方法,但是此方法以的形式返回数组的基础表示形式。 返回的ArrayList是固定大小的 ,任何修改尝试都会在运行时导致UnsupportedOperationException 。 同样,数组中的任何更改也会更改ArrayList中的元素。
  2. Collections.addAll(ArrayList<T> strList, T[] strArr): This is the best way to convert array to ArrayList because the array data is copied to the list and both are independent object. Once the array is copied, you can modify both the objects independently. is a very useful class in that provides a lot of utility methods.

    Collections.addAll(ArrayList <T> strList,T [] strArr) :这是将数组转换为ArrayList的最佳方法,因为数组数据已复制到列表,并且两者都是独立的对象。 复制数组后,您可以独立修改两个对象。 是中非常有用的类,它提供了许多实用程序方法。

Now let’s see both these methods usage in action.

现在,让我们看看这两种方法的用法。

package com.journaldev.util;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.List;public class ArrayToArrayList {    /**     * This class shows different methods to convert Array to ArrayList     *      * @param args     */    public static void main(String[] args) {        String[] strArr = {"1", "2", "3", "4"};        List
strList = new ArrayList
(); //return the list representation of array //any change in array elements will change the arrayList elements also strList = Arrays.asList(strArr); System.out.println("Original ArrayList from Arrays.asList()"); for (String str : strList) System.out.print(" " + str); //change the array element and see the effect is propogated to list also. strArr[0] = "5"; System.out.println("\nChange in array effect on ArrayList"); for (String str : strList) System.out.print(" " + str); //below code will throw java.lang.UnsupportedOperationException because // Arrays.asList() returns a fixed-size list backed by the specified array. //strList.add("5"); strList = new ArrayList
(); Collections.addAll(strList, strArr); //change both the array and arraylist and check if they are independent? strList.add("5"); strArr[0] = "1"; System.out.println("\nArray to ArrayList using Collections.addAll()"); for (String str : strList) System.out.print(" " + str); }}

The output of the above program is:

上面程序的输出是:

Original ArrayList from Arrays.asList() 1 2 3 4Change in array effect on ArrayList 5 2 3 4Array to ArrayList using Collections.addAll() 5 2 3 4 5
Java Array To List Arraylist

Java Array To List

Java数组列表

So now you know which method to use to convert Array to ArrayList based on the requirements.

现在,您知道了根据要求使用哪种方法将Array转换为ArrayList

Here is the video tutorial explaining it in Eclipse.

这是在Eclipse中解释它的视频教程。

. 找到更多Java Array示例。

翻译自:

转载地址:http://xmozd.baihongyu.com/

你可能感兴趣的文章
精通ASP.NET Web程序测试
查看>>
vue 根据不同属性 设置背景
查看>>
51Nod1601 完全图的最小生成树计数 Trie Prufer编码
查看>>
Codeforces 1110D. Jongmah 动态规划
查看>>
android驱动在win10系统上安装的心酸历程
查看>>
优雅的程序员
查看>>
oracle之三 自动任务调度
查看>>
Android dex分包方案
查看>>
ThreadLocal为什么要用WeakReference
查看>>
删除本地文件
查看>>
FOC实现概述
查看>>
base64编码的图片字节流存入html页面中的显示
查看>>
这个大学时代的博客不在维护了,请移步到我的新博客
查看>>
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>