Java Array to CSV toCSV(int[][] values)

Here you can find the source of toCSV(int[][] values)

Description

to CSV

License

Apache License

Declaration

public static String toCSV(int[][] values) 

Method Source Code

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

import java.util.List;

public class Main {
    public static String toCSV(int[][] values) {
        String vs[][] = new String[values.length][];
        for (int i = 0; i < values.length; i++) {
            vs[i] = new String[values[i].length];
            for (int j = 0; j < values[i].length; j++) {
                vs[i][j] = "" + values[i][j];
            }/*from w w w  .ja  va  2  s .  co  m*/
        }
        return toCSV(vs);
    }

    /**
     * 
     * @param values
     * @return
     */
    public static String toCSV(String[][] values) {
        StringBuilder builder = new StringBuilder();
        for (int y = 0; y < values.length; y++) {
            for (int x = 0; x < values[y].length; x++) {
                builder.append(values[y][x]);
                if (x != values[y].length - 1) {
                    builder.append(",");
                }
            }
            if (y != values.length - 1) {
                builder.append("\n");
            }
        }
        return builder.toString();
    }

    public static String toCSV(List<String[]> values, char separator) {
        StringBuilder builder = new StringBuilder();
        for (int y = 0; y < values.size(); y++) {
            for (int x = 0; x < values.get(y).length; x++) {
                builder.append(values.get(y)[x]);
                if (x != values.get(y).length - 1) {
                    builder.append(separator);
                }
            }
            if (y != values.size() - 1) {
                builder.append("\n");
            }
        }
        return builder.toString();
    }

    public static String toCSV(String[] values, char separator) {
        StringBuilder builder = new StringBuilder();

        for (int x = 0; x < values.length; x++) {
            builder.append(values[x]);
            if (x != values.length - 1) {
                builder.append(separator);
            }
        }
        return builder.toString();
    }
}

Related

  1. ArrayToCsv(Object[] line)
  2. ArrayToCSV(String[] array)
  3. arrayToCsvString(String[] array, char delimiter)
  4. toCSV(double[] as)
  5. toCSV(Object[] objs)
  6. toCSVString(String[] strArray)