Java中数组到ArrayList的转换
以下方法可用于将 Array 转换为 ArrayList:
方法一:使用 Arrays.asList() 方法
Syntax: public static List asList(T... a)
// Returns a fixed-size List as of size of given array.
// Element Type of List is of same as type of array element type.
// It returns an List containing all of the elements in this
// array in the same order.
// T represents generics
请注意,这里有一个数组参数和 List 返回值。
- 返回由指定数组支持的固定大小的列表。
- 返回的列表是可序列化的并实现了 RandomAccess。
- 由于返回的 List 是固定大小的,因此我们不能在其中添加更多元素,但我们可以使用 ArrayList 类中定义的 set(index, new Element) 方法将现有元素替换为新元素。
Java
// Java program to demonstrate conversion of
// Array to ArrayList of fixed-size.
import java.util.*;
class GFG
{
public static void main (String[] args)
{
String[] geeks = {"Rahul", "Utkarsh",
"Shubham", "Neelam"};
// Conversion of array to ArrayList
// using Arrays.asList
List al = Arrays.asList(geeks);
System.out.println(al);
}
}
Java
// Java program to demonstrate error
// if we add more element(s) to
// a fixed-size List.
import java.util.*;
class GFG
{
public static void main (String[] args)
{
String[] geeks = {"Rahul", "Utkarsh",
"Shubham", "Neelam"};
// Conversion of array to ArrayList
// using Arrays.asList
List al = Arrays.asList(geeks);
System.out.println(al);
// Adding some more values to the List.
al.add("Shashank");
al.add("Nishant");
System.out.println(al);
}
}
Java
// Java program to demonstrate how to add
// one or more element(s) to returned
// resizable List.
import java.util.*;
class GFG
{
public static void main (String[] args)
{
String[] geeks = {"Rahul", "Utkarsh",
"Shubham", "Neelam"};
List al =
new ArrayList(Arrays.asList(geeks));
System.out.println(al);
// Adding some more values to the List.
al.add("Shashank");
al.add("Nishant");
System.out.println("\nArrayList After adding two" +
" more Geeks: ");
System.out.println(al);
}
}
Java
// Java program to demonstrate how to
// add all elements of array to arrayList.
import java.util.*;
class GFG
{
public static void main (String[] args)
{
String[] geeks = {"Rahul", "Utkarsh",
"Shubham", "Neelam"};
List al = new ArrayList();
// adding elements of array to arrayList.
Collections.addAll(al, geeks);
System.out.println(al);
}
}
Java
// Adding null to a list
import java.util.*;
class GFG
{
public static void main (String[] args)
{
String[] geeks = {"Rahul", "Utkarsh",
"Shubham", "Neelam"};
List al = new ArrayList();
Collections.addAll(null, geeks);
System.out.println(al);
}
}
Java
import java.util.*;
class GFG
{
public static void main (String[] args)
{
String[] geeks = {"Rahul", "Utkarsh",
"Shubham", "Neelam"};
List al = new ArrayList();
Collections.addAll(al, null);
System.out.println(al);
}
}
Java
// Java program to convert a ArrayList to
// an array using add() in a loop.
import java.util.*;
class GFG
{
public static void main (String[] args)
{
String[] geeks = {"Rahul", "Utkarsh",
"Shubham", "Neelam"};
List al = new ArrayList();
// Array to ArrayList Conversion
for (String geek : geeks)
al.add(geek);
System.out.println(al);
}
}
输出:
[Rahul, Utkarsh, Shubham, Neelam]
如果我们在转换后的列表中添加更多元素会怎样?
由于返回的 List 是固定大小的 List,我们无法添加更多元素。尝试添加更多元素会导致 UnsupportedOperationException。
考虑以下示例。
Java
// Java program to demonstrate error
// if we add more element(s) to
// a fixed-size List.
import java.util.*;
class GFG
{
public static void main (String[] args)
{
String[] geeks = {"Rahul", "Utkarsh",
"Shubham", "Neelam"};
// Conversion of array to ArrayList
// using Arrays.asList
List al = Arrays.asList(geeks);
System.out.println(al);
// Adding some more values to the List.
al.add("Shashank");
al.add("Nishant");
System.out.println(al);
}
}
输出:
[Rahul, Utkarsh, Shubham, Neelam]
运行时错误
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
at GFG.main(File.java:16)
因此建议创建新的 ArrayList 并将Arrays.asList(array reference)作为参数传递给它(即作为 ArrayList 的构造函数参数)。
考虑以下示例:
Java
// Java program to demonstrate how to add
// one or more element(s) to returned
// resizable List.
import java.util.*;
class GFG
{
public static void main (String[] args)
{
String[] geeks = {"Rahul", "Utkarsh",
"Shubham", "Neelam"};
List al =
new ArrayList(Arrays.asList(geeks));
System.out.println(al);
// Adding some more values to the List.
al.add("Shashank");
al.add("Nishant");
System.out.println("\nArrayList After adding two" +
" more Geeks: ");
System.out.println(al);
}
}
[Rahul, Utkarsh, Shubham, Neelam]
ArrayList After adding two more Geeks:
[Rahul, Utkarsh, Shubham, Neelam, Nishant, Shashank]
&nbap;
方法 2:使用 Collections.addAll() 方法
Syntax: public static boolean addAll(Collection c, T... a)
// Adds all of the specified elements to the specified collection.
// Elements to be added may be specified individually or as an array.
// T is generics
请注意,有一个集合参数c要插入其中的元素,数组参数a包含要插入c的元素。
返回类型是布尔类型。如果集合因调用而改变,则返回 true。
如果集合c不支持 add 方法,则抛出 UnsupportedOperationException ;如果元素(或数组的元素)中值的某些方面阻止将其添加到集合c中,则抛出 IllegalArgumentException 。
考虑以下示例:
Java
// Java program to demonstrate how to
// add all elements of array to arrayList.
import java.util.*;
class GFG
{
public static void main (String[] args)
{
String[] geeks = {"Rahul", "Utkarsh",
"Shubham", "Neelam"};
List al = new ArrayList();
// adding elements of array to arrayList.
Collections.addAll(al, geeks);
System.out.println(al);
}
}
输出 :
[Rahul, Utkarsh, Shubham, Neelam]
将 null 添加到列表中
注意:如果指定的集合或指定的数组为空,则抛出 NullpointerException。
Java
// Adding null to a list
import java.util.*;
class GFG
{
public static void main (String[] args)
{
String[] geeks = {"Rahul", "Utkarsh",
"Shubham", "Neelam"};
List al = new ArrayList();
Collections.addAll(null, geeks);
System.out.println(al);
}
}
将 null 添加到列表末尾
Java
import java.util.*;
class GFG
{
public static void main (String[] args)
{
String[] geeks = {"Rahul", "Utkarsh",
"Shubham", "Neelam"};
List al = new ArrayList();
Collections.addAll(al, null);
System.out.println(al);
}
}
运行时错误
Exception in thread "main" java.lang.NullPointerException
at java.util.Collections.addAll(Collections.java:5401)
at GFG.main(File.java:11)
方法3:使用手动方法通过add()方法转换Array
如果我们不想在内置方法中使用Java ,我们可以使用此方法。这是将所有数组元素添加到 List 的手动方法。
Syntax: public boolean add(Object obj)
// Appends the specified element to the end of this list.
// Returns true.
Java
// Java program to convert a ArrayList to
// an array using add() in a loop.
import java.util.*;
class GFG
{
public static void main (String[] args)
{
String[] geeks = {"Rahul", "Utkarsh",
"Shubham", "Neelam"};
List al = new ArrayList();
// Array to ArrayList Conversion
for (String geek : geeks)
al.add(geek);
System.out.println(al);
}
}
输出:
[Rahul, Utkarsh, Shubham, Neelam]