📜  java toarray - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:09.663000             🧑  作者: Mango

代码示例1
import java.util.*;
 
public class Scaler {
 
   public static void main(String[] args) {
       ArrayList list = new ArrayList<>();
       list.add(1);
       list.add(2);
       list.add(3);
       list.add(4);
       //Implementing the first way to convert list to array in java
       Object[] res1Arr=list.toArray();
       //Implementing the second way to convert list to array in java
       Integer[] res2Arr = new Integer[4];
       list.toArray(res2Arr);
       //Printing the first array
       System.out.println("Printing 'res1Arr':");
           for(Object obj: res1Arr)
           //Typecasting required for the first method
               System.out.println((Integer)obj);
       //Printing the second array
       System.out.println("Printing 'res2Arr':");
           for(Integer i: res2Arr)
               System.out.println(i);
   }
}