Skip to main content

java - How to initialize an array

MainActivity.java

package com.cfsuman.me.javaexamples;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get widget reference from XML layout
        TextView tv = (TextView) findViewById(R.id.tv);

        // Declare a new int array
        int[] marks;

        /*
            Initialize the marks int array
            Also specify the initial length of marks array
            Allocate correct memory storage for the array elements
            Syntax : variableName = new Type[capacity]
        */
        marks = new int[3];

        // Put values to marks int array
        marks[0] =65;
        marks[1] =75;
        marks[2] =82;

        tv.setText("Int array : ");
        // Iterate over the marks int array
        for(int i=0;i<marks.length;i++){
            tv.setText(tv.getText() +""+ marks[i] + ", ");
        }

        /*
            Another way to initializing a new array
            Also allocate the correct memory storage for the array elements
            Syntax : Type[] variableName = new Type[capacity]
        */
        String[] colors = new String[4];
        colors[0] = "Black";
        colors[1] = "Gray";
        colors[2] = "Orange";
        colors[3] = "Magenta";

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

        /*
            Another way to initialize a new array
            This also add elements at the time of array initialization
            Initialize a float array
            Syntax : Type[] variableName = new Type[]{comma-delimited values}
         */
        float[] alpha = new float[]{0.10f,0.20f,0.30f,0.40f};

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

        /*
            Another way to initialize a new array
            Initialize a double array, data type double
            Syntax : Type[] variableName = {comma-delimited values};
         */
        double[] numbers = {10,20,30,40};

        tv.setText(tv.getText() + "\nDouble array : ");
        // Loop through the double array
        for(int i=0;i<numbers.length;i++){
            tv.setText(tv.getText() + "" + numbers[i] + ", ");
        }

        // Initialize a new boolean array with elements
        boolean[] status = new boolean[]{
                true,
                false,
                true,
                false
        };

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

Popular posts from this blog

Android Popup Window Example

activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity" android:background="#f5f1e0" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Popup Window" /> </RelativeLayout> res/layout/custom_layout.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rl_custom_layout" …

How to add a hint to Spinner in Android

activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" tools:context=".MainActivity" android:background="#60c4ff" > <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> spinner_item.xml <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:textStyle="italic" …

How to create a round/circle Button in Android

activity_main.xml code snippet <Button android:id ="@+id/push_button" android:layout_width="150dp" android:layout_height="150dp" android:text="Round Button" android:background="@drawable/button_bg_round" android:padding="15dp" /> res/drawable/button_bg_round.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="oval"> <stroke android:color="#1E90FF" android:width="5dp" /> <solid android:color="#87CEEB"/> <size android:width="150dp" android:height="150dp"/> </shape> </item> </selector> More android examples How to set Button fontHow to change Button font programmaticallyHow to set Button text style to bold and italic…