Tuesday, October 6, 2015

Java - How to check if an array contains a certain value

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.Arrays;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private String mTitle = "Java - How to check if an array contains a certain value";

    @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};

        tv.setText(tv.getText() + "int array elements : ");
        // Loop through int array
        for (int i=0;i<intArray.length;i++){
            tv.setText(tv.getText() + "" + intArray[i] + ", ");
        }

        // Test int array contains value '2'
        // This does not work for primitive type array, such as int array
        // So we need to implement custom method to check array contains value
        boolean isIntArrayContains2 = Arrays.asList(intArray).contains(2);
        tv.setText(tv.getText() + "\nint array contains '2' : " +isIntArrayContains2);

        // Check int array contains value '5' by custom method
        boolean isIntArrayContains5 = intArrayContains(intArray, 5);
        tv.setText(tv.getText() + "\nint array contains '5' (custom method) : " +isIntArrayContains5);

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

        tv.setText(tv.getText() + "\n\nInteger array elements : ");
        // Loop through Integer array
        for (int i=0;i<integerArray.length;i++){
            tv.setText(tv.getText() + "" + integerArray[i] + ", ");
        }

        // Check Integer array contains value '2'
        boolean isIntegerArrayContains2 = Arrays.asList(integerArray).contains(2);
        tv.setText(tv.getText() + "\nInteger array contains '2' : " +isIntegerArrayContains2);

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

        tv.setText(tv.getText() + "\n\nString array elements : ");
        // Loop through String array
        for (int i=0;i<stringArray.length;i++){
            tv.setText(tv.getText() + "" + stringArray[i] + ", ");
        }

        // Test String array contains value 'Blue'
        boolean isStringArrayContainsBlue = Arrays.asList(stringArray).contains("Blue");
        tv.setText(tv.getText() + "\n\n String array contains 'Blue' : " +isStringArrayContainsBlue);
    }

    // Custom method to implement int array contains
    private boolean intArrayContains(int[] intArray,int value){
        boolean result = false;
        for (int i=0;i<intArray.length;i++){
            if(intArray[i]==value){
                result = true;
                break;
            }
        }
        return result;
    }
}
More android examples