Skip to main content

How To Convert ArrayList into Array in Java


In This Tutorial We will Learn How To Convert ArrayList Into Array In java. We can do this by calling toArray() method .in ArrayList class We have toArray() method ,by using this method we can convert ArrayList into Array in java.

Object[] toArray() method Returns an array containing all of the elements in this ArrayList in proper sequences.

code:


package com.practice;

import java.util.ArrayList;

public class ConvertArrayListToArray {

public static void main(String[] args) {
ArrayList<Integer> arraylist = new ArrayList<Integer>();
arraylist.add(10);
arraylist.add(20);
arraylist.add(30);
arraylist.add(40);
System.out.println("ArrayList Before conversion:"+arraylist);

Integer intArray[] = new Integer[arraylist.size()];

intArray = arraylist.toArray(intArray);

for(int x :intArray){
System.out.println(x);
}

}

}

output:


ArrayList Before conversion:[10, 20, 30, 40]
10
20
30
40


Popular posts from this blog

Java Primitive DataTypes

Java data types will tell us what type of data can variable hold. Ex: int x = 10; In the above variable, x can hold int data type value.so we must declare any variable with data type before we can use a variable.

Instance Variables in Java

Instance variables should declare with in the class but outside of any method or block or constructor.instance variables can accessible inside class all methods , blocks & constructors.

Angular 9 Date Pipe Example

Table of Contents what is pipe in angular? Angular Date Pipe Pre-defined Date Format Custom Date Formats what is pipe in angular? In Angular Pipes are used to transform data.Pipes will take input data and transforms input data in desired output. Symbol of Pipe : " | " Angular Date Pipe Angular DatePipe is one of built-in Pipes.DatePipe is used for Format a date value according to locale rules. Syntax {{value_expression| date [:format[:timezone[:locale]]]}} DatePipe avalibale in '@angular/common' module. Example //app.component.ts import   {   Component   }   from   '@angular/core' ; @ Component ({   selector:   'app-root',    template:   `{{dOB | date:'fullDate'}}`,    styleUrls:  ['./app.component.css'] }) expo...