Thursday, October 8, 2015

java - How to concatenate two arrays

MainActivity.java

package com.cfsuman.me.javaexamples;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

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


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_ACTION_BAR);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set background color for action bar
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.RED));

        // Get widget reference from XML layout
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        TextView tv = (TextView) findViewById(R.id.tv);

        // Empty the TextView
        tv.setText("");

        // Initialize a new Array
        String[] redColors = new String[]{"Red", "Crimson", "Pink"};

        // Initialize another Array
        String[] blueColors = new String[]{"Blue", "AliceBlue"};

        // Iterate over redColors array
        tv.setText("Red colors : ");
        for(int i=0;i<redColors.length;i++){
            tv.setText(tv.getText() + redColors[i] + ", ");
        }

        // Iterate over blueColors array
        tv.setText(tv.getText() + "\nBlue colors : ");
        for(int i=0;i<blueColors.length;i++){
            tv.setText(tv.getText() + blueColors[i] + ", ");
        }

        // Concatenate two arrays using custom method
        String[] colors =concatenateTwoArrays(redColors, blueColors);

        // Iterate over redColors array
        tv.setText(tv.getText() + "\n\nAll colors : ");
        for(int i=0;i<colors.length;i++){
            tv.setText(tv.getText() + colors[i] + ", ");
        }

        // Initialize an Integer array
        Integer[] numbers = new Integer[]{1,2,3,4};

        // Iterate over numbers array
        tv.setText(tv.getText() + "\n\nNumbers : ");
        for(int i=0;i<numbers.length;i++){
            tv.setText(tv.getText() + "" + numbers[i] + ", ");
        }

        // Initialize another Integer array
        Integer[] numbersTenUp = new Integer[]{11,12,13};

        // Iterate over numbersTenUp array
        tv.setText(tv.getText() + "\nNumbers ten up : ");
        for(int i=0;i<numbersTenUp.length;i++){
            tv.setText(tv.getText() + "" + numbersTenUp[i] + ", ");
        }

        // Initialize another Integer array
        Integer[] numbersFiftyUp = new Integer[]{51,55};

        // Iterate over numbersFiftyUp array
        tv.setText(tv.getText() + "\nNumbers fifty up : ");
        for(int i=0;i<numbersFiftyUp.length;i++){
            tv.setText(tv.getText() + "" + numbersFiftyUp[i] + ", ");
        }

        // Initialize an ArrayList
        List<Integer> allNumbers = new ArrayList<>();

        // Add numbers array elements to list
        Collections.addAll(allNumbers,numbers);

        // Add numbersTenUp array elements to list
        Collections.addAll(allNumbers,numbersTenUp);

        // Add numbersFiftyUp array elements to list
        Collections.addAll(allNumbers,numbersFiftyUp);

        // Concatenate all the three Integer arrays
        // This is the way to concatenate multiple arrays
        Integer[] result = allNumbers.toArray(new Integer[allNumbers.size()]);

        // Iterate over concatenated Integer arrays
        tv.setText(tv.getText() + "\n\nAll numbers : ");
        for(int i=0;i<result.length;i++){
            tv.setText(tv.getText() + "" + result[i] + ", ");
        }
    }

    // Custom method to concatenate two String arrays
    protected String[] concatenateTwoArrays(String[] arrayFirst,String[] arraySecond){
        // Initialize an empty list
        List<String> both = new ArrayList<>();

        // Add first array elements to list
        Collections.addAll(both,arrayFirst);

        // Add another array elements to list
        Collections.addAll(both,arraySecond);

        // Convert list to array
        String[] result = both.toArray(new String[both.size()]);

        // Return the result
        return result;
    }
}
More android examples