fill Array - Java Collection Framework

Java examples for Collection Framework:Array Fill

Description

fill Array

Demo Code


//package com.java2s;

import java.util.Arrays;

public class Main {
    public static void main(String[] argv) throws Exception {
        String value = "java2s.com";
        int length = 2;
        System.out.println(java.util.Arrays.toString(fillArray(value,
                length)));//  w  ww.  j a  va2  s  .co m
    }

    public static String[] fillArray(String value, int length) {
        String[] result = new String[length];
        Arrays.fill(result, value);
        return result;
    }

    public static int[] fillArray(int value, int length) {
        int[] result = new int[length];
        Arrays.fill(result, value);
        return result;
    }

    public static String toString(Object[] array) {
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for (int i = 0; i < array.length; i++) {
            sb.append(array[i]);
            if (i < array.length - 1)
                sb.append(",");
        }
        sb.append("]");
        return sb.toString();
    }
}

Related Tutorials