Thursday, October 8, 2015

java - How to convert ArrayList to Array

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.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("");

        // Initializing a new ArrayList
        List<String> stringList = new ArrayList<>();
        // Put some elements to it
        stringList.add("Red");
        stringList.add("Yellow");
        stringList.add("Crimson");
        stringList.add("Magenta");

        // Iterate over the ArrayList
        tv.setText(tv.getText() + "String list : ");
        for(int i=0;i<stringList.size();i++){
            tv.setText(tv.getText() + stringList.get(i) + ", ");
        }

        /*
            Convert the ArrayList to an Array
            Both array and list element data type is String
            So, it will convert a String data type ArrayList to String Array
        */
        String[] stringArray = stringList.toArray(new String[stringList.size()]);

        // Iterate over the converted String Array
        tv.setText(tv.getText() + "\nString array (converted) : ");
        for(int i=0;i<stringArray.length;i++){
            tv.setText(tv.getText() + stringArray[i] + ", ");
        }

        // Initialize an Integer ArrayList
        List<Integer> integerList = new ArrayList<>();
        // Put some number to the list
        integerList.add(1);
        integerList.add(2);
        integerList.add(3);
        integerList.add(4);
        integerList.add(5);

        // Iterate over the Integer ArrayList
        tv.setText(tv.getText() + "\n\nInteger list : ");
        for(int i=0;i<integerList.size();i++){
            tv.setText(tv.getText() + "" + integerList.get(i) + ", ");
        }

        // Convert Integer ArrayList to Integer Array
        Integer[] integerArray = integerList.toArray(new Integer[integerList.size()]);

        // Iterate over the Integer Array
        tv.setText(tv.getText() + "\nInteger array (converted) : ");
        for(int i=0;i<integerArray.length;i++){
            tv.setText(tv.getText() + "" + integerArray[i] + ", ");
        }

        // Initialize a Boolean ArrayList
        List<Boolean> booleanList = new ArrayList<>();
        // Add some Boolean value to it
        booleanList.add(true);
        booleanList.add(false);
        booleanList.add(Boolean.FALSE);
        booleanList.add(Boolean.TRUE);

        // Iterate over the Boolean ArrayList
        tv.setText(tv.getText() + "\n\nBoolean list : ");
        for(int i=0;i<booleanList.size();i++){
            tv.setText(tv.getText() + "" + booleanList.get(i) + ", ");
        }

        // Convert the Boolean list to Boolean Array
        Boolean[] booleanArray = booleanList.toArray(new Boolean[booleanList.size()]);

        // Loop through the converted Boolean Array
        tv.setText(tv.getText() + "\nBoolean array (converted) : ");
        for(int i=0;i<booleanArray.length;i++){
            tv.setText(tv.getText() + "" + booleanArray[i] + ", ");
        }
    }
}
More android examples