Java Static List | Java example

Java Static List :

When we want to use multiple Strings or integers its not possible to create multiple variables everytime and store in them. Also even if you store them its very difficult to process them.

For example when we try to store 100 String’s  it’s not a good practice to create 100 variables. Because it also causes a lot of memory consumption as well.

In need of a good programming practice we need to make use of Java Static List.

 

Initialise a String list

public static String[] list;

 

We define String list as String[] and object name.

 

initialise the list and add values as below

list = new String[] { "value1","value2"... };

 

let’s add some data to list

list = new String[] {
        "One",
        "Two",
        "Three",
        "Four",
        "Five"
};

 

Using for loop we need to fetch the values of list and print them to screen.

 

for(int i = 0; i< list.length; i++){

    System.out.println(list[i]);
}

 

String List:

Declare a list of String and then print them using for loop.

 

Parse the list in for loop with index position ‘i’

list[i]

 

 

public static String[] list;

public static void main(String args[]){

    list = new String[] {
            "One",
            "Two",
            "Three",
            "Four",
            "Five"
    };

    for(int i = 0; i< list.length; i++){

        System.out.println(list[i]);
    }
}

 

Java Static List

 

Int List:

Same like String array we also declare and use int array

 

public static int[] list;

public static void main(String args[]){

    list = new int[] {
            1,
            2,
            3,
            4,
            5
    };

    for(int i = 0; i< list.length; i++){

        System.out.println(list[i]);
    }
}

 

Java Static List

 

Char List:

Here in this list we accept only one character per position as the name itself says char list.

 

public static char[] list;

public static void main(String args[]){

    list = new char[] {
            'a',
            'b',
            'c',
            'd',
            'e'
    };

    for(int i = 0; i< list.length; i++){

        System.out.println(list[i]);
    }
}

 

Java Static List

 

Float List:

For storing a list of float values we can create a list as below

public static float[] list;

public static void main(String args[]){

    list = new float[] {
            1.0f,
            2.0f,
            3.0f,
            4.0f,
            5.0f
    };

    for(int i = 0; i< list.length; i++){

        System.out.println(list[i]);
    }
}

 

Java Static List

 

If any query on Java Static List let us know in comment section below.

For more interesting updates do like and share this tutorial.

 

Show Buttons
Hide Buttons
Read previous post:
Java Calculator | Addition | Subtraction | Multiply | Divide

  In the previous java tutorial we have gone through a simple java calculator program.Now this tutorial is a extension...

Close