Wednesday, October 7, 2015

java - How to iterate through 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.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");
        colors.add("Yellow");
        colors.add("Black");

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

        // Another way to Iterate through ArrayList
        tv.setText(tv.getText() + "\nColors : ");
        Iterator it = colors.iterator();
        while(it.hasNext()){
            tv.setText(tv.getText() + "" + it.next() + ", ");
        }

        // Another way to iterate through ArrayList
        tv.setText(tv.getText() + "\nColors : ");
        for(String s : colors){
            tv.setText(tv.getText() + s + ", ");
        }

        // Another way to iterate through ArrayList
        tv.setText(tv.getText() + "\nColors : ");
        int index = 0;
        while(colors.size()>index){
            tv.setText(tv.getText() + "" + colors.get(index) + ", ");
            index++;
        }

        // Iterate over an ArrayList and check a value
        // But, this will throw error if ArrayList contains null value
        for(String s : colors){
            if(s.equals("Blue")){
                tv.setText(tv.getText() + "\n\nWOW! Blue found.");
            }
        }

        // Adding an empty value to ArrayList
        colors.add("");
        // Adding a null value to ArrayList
        colors.add(null);
        // Adding an element to ArrayList
        colors.add("White");

        // Iterate over ArrayList and check a value
        String valueToCheck = "Black";
        for(String s : colors){
            // Better way to check equality when ArrayList contains null value
            if(valueToCheck.equals(s)){
                tv.setText(tv.getText() + "\nWOW! " + valueToCheck + " found.");
            }
        }
    }
}
More android examples