Java程序将数组转换为列表
java.util包的Arrays类提供了一种称为的方法asList()。此方法接受数组作为参数,然后返回List对象。要将数组转换为List对象-
创建一个数组或从用户那里读取它。
使用asList()Arrays类的方法将数组转换为列表对象。
打印列表对象的内容。
示例
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class ArrayToList {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array to be created ::");
int size = sc.nextInt();
String [] myArray = new String[size];
for(int i=0; i<myArray.length; i++){
System.out.println("Enter the element "+(i+1)+" (String) :: ");
myArray[i]=sc.next();
}
List<String> list = Arrays.asList(myArray);
System.out.println("Given array is converted to a list");
System.out.println("Contents of list ::"+list);
list.toArray(myArray);
}
}输出结果
Enter the size of the array to be created :: 4 Enter the element 1 (String) :: Java Enter the element 2 (String) :: JavaFX Enter the element 3 (String) :: WebGL Enter the element 4 (String) :: JoGL Given array is converted to a list Contents of list ::[Java, JavaFX, WebGL, JoGL]
热门推荐
2 成都训练狗狗
3 怎么训练小狗狗
4 训练狗狗软件
5 如何训练狗狗拉屎
9 狗狗负重训练
10 狗狗多大可以训练大小便
11 怎么训练狗狗萨摩耶
12 狗狗训练书
13 鞍山狗狗训练
14 训练狗狗用品
15 怎样才能训练狗狗听话
16 狗狗如何训练大小便
17 怎样训练狗狗睡狗窝
18 训练狗狗工具