Convert Integer ArrayList to Integer Array in Java Android with example

How to change and show integer array on activity screen using looping control with textview.

In this tutorial we are simply converting String array list into string array with the use of arraylist.toArray() method. So here is the complete step by step tutorial for Convert Integer ArrayList to Integer Array in Java Android with example.

android-project-download-code-button

How to Convert Integer ArrayList to Integer Array in Java Android with example.

Code for MainActivity.java file.

 package com.convertintegerarraylisttointegerarray_android_examples.com;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends Activity {

 //Initializing Integer array list;
 List<Integer> IntegerArrayList = new ArrayList<>();
 
 //Initializing Integer array;
 Integer[] IntegerArray;
 
 //Initializing TextView
 TextView textview1,textview2;
 
 int i,j = 0;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 textview1 = (TextView)findViewById(R.id.textView1);
 textview2 = (TextView)findViewById(R.id.textView2);
 
 //Adding values to Integer array list.
 IntegerArrayList.add(1);
 IntegerArrayList.add(2);
 IntegerArrayList.add(3);
 IntegerArrayList.add(4);
 IntegerArrayList.add(5);
 IntegerArrayList.add(6);
 
 //Printing Integer array list on screen using loop.
 
 textview1.setText("Integer Array list : ");
 
 while ( i<IntegerArrayList.size() )
 {
 
 textview1.setText(textview1.getText() + " " + IntegerArrayList.get(i) + ", ");
 
 i++;
 
 }
 
 //Converting Integer array list into Integer array.
 IntegerArray = IntegerArrayList.toArray(new Integer[IntegerArrayList.size()]);
 
 textview2.setText("Convert into Integer Array : ");
 
 
 //Displaying converted Integer array on screen using textview.
 while ( j<IntegerArray.length )
 { 
 textview2.setText(textview2.getText() + " " + IntegerArray[j] + ", ");
 
 j++;
 
 }
 
 }

 }

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.convertintegerarraylisttointegerarray_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="65dp"
 android:text="Integer Array"
 android:gravity="center"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:gravity="center"
 android:text="Integer ArrayList"
 android:textAppearance="?android:attr/textAppearanceLarge" />
 
</RelativeLayout>

Screenshot:

Convert Integer ArrayList to Integer Array in Java Android with example

Click here to download Convert Integer ArrayList to Integer Array in Java Android with example project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *