get Value from Array by index - Java Collection Framework

Java examples for Collection Framework:Array Index

Description

get Value from Array by index

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String[] array = new String[] { "1", "abc", "level", null,
                "java2s.com", "asdf 123" };
        int indexOf = 2;
        System.out.println(getValue(array, indexOf));
    }//from  w ww  .  j  a v a 2  s  .  com

    public static String getValue(String[] array, int indexOf) {
        return getValue(array, indexOf, null);
    }

    public static String getValue(String[] array, int indexOf,
            String defaultValue) {
        if (array.length - 1 >= indexOf) {
            return array[indexOf];
        }
        return defaultValue;
    }
}

Related Tutorials