Tuesday, October 6, 2015

java - How to convert an array to a list

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 android.content.Context;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private String mTitle = "Java - How to convert array to list";

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

        mContext = getApplicationContext();
        getSupportActionBar().setTitle(mTitle);
        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 an int array
        int[] intArray = new int[]{1,2,3,4,5,6};

        // Initialize an Integer array with elements
        Integer[] integerArray = new Integer[]{1,2,3,4,5,6,7,8,9};

        // Initialize a new String array
        String[]  stringArray = new String[]{"Red","Green","Blue","Yellow","Black"};

        // Convert Integer array to list
        List<Integer> integerList = Arrays.asList(integerArray);

        // Convert String array to list
        List<String> stringList = Arrays.asList(stringArray);

        // Another way to convert Integer array to list
        ArrayList<Integer> secondIntegerList = new ArrayList<>(Arrays.asList(integerArray));

        // We applied a technique to get an Integer list from an int array
        List<Integer> thirdIntegerList = new ArrayList<>();
        for(int i=0;i<intArray.length;i++){thirdIntegerList.add(intArray[i]);}

        tv.setText(tv.getText() + "Integer list : ");
        // Iterate through Integer list
        for (int i=0;i<integerList.size();i++){
            tv.setText(tv.getText() + "" + integerList.get(i) + ", ");
        }

        tv.setText(tv.getText() + "\nSecond Integer list : ");
        // Iterate through second Integer list
        for (int i=0;i<secondIntegerList.size();i++){
            tv.setText(tv.getText()+ "" + secondIntegerList.get(i) + ", ");
        }

        tv.setText(tv.getText() + "\n\nThird Integer list (from int array): ");
        // Loop through third Integer list
        for (int i=0;i<thirdIntegerList.size();i++){
            tv.setText(tv.getText()+ "" + thirdIntegerList.get(i) + ", ");
        }

        tv.setText(tv.getText() + "\n\nString list : ");
        // Iterate through String list elements
        for (int i=0;i<stringList.size();i++){
            tv.setText(tv.getText()+ "" + stringList.get(i) + ", ");
        }
    }
}
More android examples