“Java Toarray” Kode Jawaban

Java Arraylist to Array

List<String> list = new ArrayList<>();
list.add("a");
list.add("ab");
list.add("abc");
list.add("abcd");
// convert
String[] array = list.toArray();
Dark Dove

ArrayList to Array Java

ArrayList<String> ArrayLister=new ArrayList<>(){ArrayLister.add("Hi")};
String[] array=ArrayLister.toArray(String[]);
BucketofJava

Java Toarray

import java.util.*;
 
public class Scaler {
 
   public static void main(String[] args) {
       ArrayList<Integer> 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);
   }
}
codelearner

Jawaban yang mirip dengan “Java Toarray”

Pertanyaan yang mirip dengan “Java Toarray”

Lebih banyak jawaban terkait untuk “Java Toarray” di Java

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya