Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: LGPL 

import java.util.*;

public class Main {
    public static int[] toIntArray(final List<Integer> list) {
        int[] array = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            array[i] = list.get(i);
        }
        return array;
    }

    public static int size(final Object[] array) {
        return null != array ? array.length : 0;
    }

    public static int size(final Collection collection) {
        return null != collection ? collection.size() : 0;
    }

    public static int size(final Map map) {
        return null != map ? map.size() : 0;
    }

    public static <T> T get(T[] array, int index) {
        if (array.length < index + 1) {
            return null;
        }
        return array[index];
    }

    public static <T> T get(final T[] array, final int index, final T defaultValue) {
        if (array.length < index + 1) {
            return defaultValue;
        }
        final T result = array[index];
        return null != result ? result : defaultValue; // array[index];
    }

    public static <T> T get(final Collection<T> collection, final int index) {
        if (collection.size() < index + 1) {
            return null;
        }
        int i = 0;
        for (T item : collection) {
            if (i == index) {
                return item;
            }
            i++;
        }
        return null;
    }
}