List to Array of Primitive Conversion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.list; import java.util.ArrayList; import java.util.List; /* * Here we will learn to convert List to Arrays. */ public class ListToArrayType { /* * Note*: Here in this code Autoboxing is done also generics is getting used. Because of generics * Casting is not required. */ public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); System.out.println("Values in List-"); for(int str: list) { System.out.println(str); } System.out.println("Converting list in Arrays"); Integer[] strArr = list.toArray(new Integer[0]); System.out.println("Values in String arrays"); for(int str : strArr) System.out.println(str); } } |
时间: 2024-10-06 20:44:35