October 03, 2017

Collection list to simple Array convert in java

/**
 * Sample java programs
 */
package com.belazy.misc;

import java.util.ArrayList;
import java.util.List;

/**
 * @author belazy
 *
 */
public class SampleArrays {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

List<Integer> sampleList = new ArrayList<Integer>();

for(int i=0;i<=10;i++){
sampleList.add(i);
}


/**
* Arrays
*/
Integer[] a= new Integer[sampleList.size()];
a= sampleList.toArray(a);
System.out.println("5 th element in the array "+a[5]);

}

}

A small description


Arrays are treated as object in JVM
Type of array - The elementType has to specified (say Integer)
The size of the array should be on the stack before the operation is performed
Example :-
Int[] javaArraySample = new int [12];

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments