Combine Merge two integer arrays in Android Java

How to concatenate two or multiple integer[] array and display print array elements on screen dynamically.

android-project-download-code-button

How to Combine Merge two integer arrays in Android Java.

Code for MainActivity.java file.

 package com.mergetwointegerarrays_android_examples.com;

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

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView arrayView1,arrayView2,arrayView3;
 
 // Initializing array 1
 Integer[] array1 = new Integer[]{1,2,3,4,5};

 // Initializing array 2
 Integer[] array2 = new Integer[]{6,7,8,9,0};
 
 // Initializing array 3
 
 Integer[] array3 ;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 arrayView1 = (TextView)findViewById(R.id.textView1);
 arrayView2 = (TextView)findViewById(R.id.textView2);
 arrayView3 = (TextView)findViewById(R.id.textView3);
 
 //Printing array 1 on screen.
 for(int i=0 ; i<array1.length ; i++){
 
 arrayView1.setText(arrayView1.getText() + " " + array1[i] + ", ");
 }
 
 //Printing array 2 on screen.
 for(int i=0 ; i<array2.length ; i++){
 
 arrayView2.setText(arrayView2.getText() +" " + array2[i] + ", ");
 }
 
 
 //Creating array list string.
 
 List<Integer> IntegerArrayList = new ArrayList<>();
 
 //adding array 1 to array 3.
 
 Collections.addAll(IntegerArrayList,array1);
 
 //adding array 2 to array 3.
 
 Collections.addAll(IntegerArrayList,array2);
 
 //converting array list to string array;
 
 array3 = IntegerArrayList.toArray(new Integer[IntegerArrayList.size()]);
 
 //Printing final result array on screen.
 
 for(int i=0 ; i<array3.length ; i++){
 
 arrayView3.setText(arrayView3.getText() +" " + array3[i] + ", ");
 }
 
 }

}

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.mergetwointegerarrays_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Array 2 :"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_above="@+id/textView2"
 android:layout_marginBottom="34dp"
 android:gravity="center"
 android:text="Array 1 :"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView2"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="30dp"
 android:text="Result : "
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

</RelativeLayout>

Screenshot:

Combine Merge two integer arrays in Android Java

Click here to download Combine Merge two integer arrays in Android Java project with source code.

Leave a Reply

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