Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Iterator;
import java.util.List;

public class Main {
    public static <T> void resize(List<T> list, int size, Class<T> clazz) {
        if (size == list.size()) {
            return;
        }
        if (size < list.size()) {
            Iterator<T> iter = list.iterator();
            for (int i = 1; iter.hasNext(); i++) {
                iter.next();
                if (i > size) {
                    iter.remove();
                }
            }
        } else {
            for (int i = 0; i <= size - list.size(); i++) {
                try {
                    list.add(clazz != null ? clazz.newInstance() : null);
                } catch (InstantiationException e) {
                    throw new RuntimeException(e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}