Wednesday, October 7, 2015

java - How to initialize an ArrayList

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.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Iterator;


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 ArrayList
        List<String> colors = new ArrayList<>();
        // Add some elements to ArrayList
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");

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

        // Initialize an Integer data type ArrayList
        List<Integer> numbers = new ArrayList<>();
        // Add some elements to ArrayList
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);

        // Iterate through numbers ArrayList
        tv.setText(tv.getText() + "\nNumbers : ");
        for(int i=0;i<numbers.size();i++){
            tv.setText(tv.getText() + "" + numbers.get(i) + ", ");
        }

        // Initialize an Boolean data type ArrayList
        List<Boolean> status = new ArrayList<>();
        // Add some elements to ArrayList
        status.add(true);
        status.add(false);
        status.add(Boolean.TRUE);
        status.add(Boolean.FALSE);

        // Iterate through status ArrayList
        tv.setText(tv.getText() + "\nStatus : ");
        for(int i=0;i<status.size();i++){
            tv.setText(tv.getText() + "" + status.get(i) + ", ");
        }

        // Single line ArrayList initialization with elements
        // Initialize an ArrayList with values
        List<String> students = Arrays.asList("Jenny","Jones","Innee","Lizu");

        // Iterate through students ArrayList
        tv.setText(tv.getText() + "\nStudents : ");
        Iterator it = students.iterator();
        while(it.hasNext()){
            tv.setText(tv.getText() + "" + it.next() + ", ");
        }

        // Initialize an Integer data type ArrayList with values
        List<Integer> marks = Arrays.asList(66,85,99);

        // Iterate through marks ArrayList
        tv.setText(tv.getText() + "\nMarks : ");
        for(int i=0;i<marks.size();i++){
            tv.setText(tv.getText() + "" + marks.get(i) + ", ");
        }

        // Initialize an ArrayList when you have only one element
        List<String> singleColor = Collections.singletonList("Yellow");

        // Iterate through single color ArrayList
        tv.setText(tv.getText() + "\nSingle Color : ");
        for(int i=0;i<singleColor.size();i++){
            tv.setText(tv.getText() + "" + singleColor.get(i) + ",");
        }
    }
}
More android examples