Java Array Multiply multiply(String[] strings, String placeholder, String[] replacements)

Here you can find the source of multiply(String[] strings, String placeholder, String[] replacements)

Description

multiply

License

Apache License

Declaration

private static String[] multiply(String[] strings, String placeholder, String[] replacements) 

Method Source Code

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

import java.util.Iterator;

public class Main {
    public static String[] multiply(String string, Iterator placeholders, Iterator replacements) {
        String[] result = new String[] { string };
        while (placeholders.hasNext()) {
            result = multiply(result, (String) placeholders.next(), (String[]) replacements.next());
        }//from ww w . jav a 2s .c  o  m
        return result;
    }

    private static String[] multiply(String[] strings, String placeholder, String[] replacements) {
        String[] results = new String[replacements.length * strings.length];
        int n = 0;
        for (int i = 0; i < replacements.length; i++) {
            for (int j = 0; j < strings.length; j++) {
                results[n++] = replaceOnce(strings[j], placeholder, replacements[i]);
            }
        }
        return results;
    }

    public static String replaceOnce(String template, String placeholder, String replacement) {
        int loc = template.indexOf(placeholder);
        if (loc < 0) {
            return template;
        } else {
            return new StringBuffer(template.substring(0, loc)).append(replacement)
                    .append(template.substring(loc + placeholder.length())).toString();
        }
    }

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

Related

  1. multiply(float[] v, double factor)
  2. multiply(float[] v1, float[] v2)
  3. multiply(long[] x, long[] y, long[] z)
  4. multiply(long[] x, long[] y, long[] z)
  5. multiply(long[] x, long[] y, long[] z)
  6. multiplyAndSum(double[] r1, double[] r2)
  7. multiplyAndSum(final double[] d, final double scale)
  8. multiplyArray(float[] array, float num)
  9. multiplyByScalar(double scalar, double[] vector)