“ArrayList to int Array Conversion di Java” Kode Jawaban

Konversi array int ke arraylist java

int[] ints = {1, 2, 3};
List<Integer> intList = new ArrayList<Integer>(ints.length);
for (int i : ints)
{
    intList.add(i);
}
Handsome Herring

ArrayList to int Array Conversion di Java

//use this if you don't want to use any methods to convert an
//arrayList(with the same size as required array) to a 'int[]' type.
//Also, this is only useful when you know the size of the array you're working with.

  int[] array = new int[size];
  for (int i = 0; i < array.length; i++) {
      ans[i] = arraylist.get(i);
  }
// array will be now of int[] type

// Also as @FineFinch pointed it could be done using :
    //only works in java 8 or later
    list.stream().mapToInt(i -> i).toArray();
night owl

Jawaban yang mirip dengan “ArrayList to int Array Conversion di Java”

Pertanyaan yang mirip dengan “ArrayList to int Array Conversion di Java”

Lebih banyak jawaban terkait untuk “ArrayList to int Array Conversion di Java” di Java

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya