001/* 002 * BioJava development code 003 * 004 * This code may be freely distributed and modified under the 005 * terms of the GNU Lesser General Public Licence. This should 006 * be distributed with the code. If you do not have a copy, 007 * see: 008 * 009 * http://www.gnu.org/copyleft/lesser.html 010 * 011 * Copyright for this code is held jointly by the individual 012 * authors. These should be listed in @author doc comments. 013 * 014 * For more information on the BioJava project and its aims, 015 * or to join the biojava-l mailing list, visit the home page 016 * at: 017 * 018 * http://www.biojava.org/ 019 * 020 */ 021package org.biojava.nbio.survival.cox.matrix; 022 023/************************************************************************* 024 * Compilation: javac StdArrayIO.java 025 * Execution: java StdArrayIO < input.txt 026 * 027 * A library for reading in 1D and 2D arrays of integers, doubles, 028 * and booleans from standard input and printing them out to 029 * standard output. 030 * 031 * % more tinyDouble1D.txt 032 * 4 033 * .000 .246 .222 -.032 034 * 035 * % more tinyDouble2D.txt 036 * 4 3 037 * .000 .270 .000 038 * .246 .224 -.036 039 * .222 .176 .0893 040 * -.032 .739 .270 041 * 042 * % more tinyBoolean2D.txt 043 * 4 3 044 * 1 1 0 045 * 0 0 0 046 * 0 1 1 047 * 1 1 1 048 * 049 * % cat tinyDouble1D.txt tinyDouble2D.txt tinyBoolean2D.txt | java StdArrayIO 050 * 4 051 * 0.00000 0.24600 0.22200 -0.03200 052 * 053 * 4 3 054 * 0.00000 0.27000 0.00000 055 * 0.24600 0.22400 -0.03600 056 * 0.22200 0.17600 0.08930 057 * 0.03200 0.73900 0.27000 058 * 059 * 4 3 060 * 1 1 0 061 * 0 0 0 062 * 0 1 1 063 * 1 1 1 064 * 065 *************************************************************************/ 066 067 068/** 069 * <i>Standard array IO</i>. This class provides methods for reading 070 * in 1D and 2D arrays from standard input and printing out to 071 * standard output. 072 * <p> 073 * For additional documentation, see 074 * <a href="http://introcs.cs.princeton.edu/22libary">Section 2.2</a> of 075 * <i>Introduction to Programming in Java: An Interdisciplinary Approach</i> 076 * by Robert Sedgewick and Kevin Wayne. 077 */ 078public class StdArrayIO { 079 080 081 082 /** 083 * Print an array of doubles to standard output. 084 * @param a 085 */ 086 public static void print(double[] a) { 087 int N = a.length; 088 System.out.println(N); 089 for (int i = 0; i < N; i++) { 090 // System.out.printf("%9.5f ", a[i]); 091 System.out.print(a[i] + " "); 092 } 093 System.out.println(); 094 } 095 096 097 098 099 /** 100 * Print the M-by-N array of doubles to standard output. 101 * @param a 102 */ 103 public static void print(double[][] a) { 104 int M = a.length; 105 int N = a[0].length; 106 System.out.println(M + "x" + N); 107 for (int i = 0; i < M; i++) { 108 for (int j = 0; j < N; j++) { 109 // System.out.printf("%9.5f ", a[i][j]); 110 System.out.print(a[i][j] + " "); 111 } 112 System.out.println(); 113 } 114 } 115 116 117 118 119 /** 120 * Print an array of ints to standard output. 121 * @param a 122 */ 123 public static void print(int[] a) { 124 int N = a.length; 125 System.out.println(N); 126 for (int i = 0; i < N; i++) { 127 System.out.printf("%9d ", a[i]); 128 } 129 System.out.println(); 130 } 131 132 133 134 135 /** 136 * Print the M-by-N array of ints to standard output. 137 * @param a 138 */ 139 public static void print(int[][] a) { 140 int M = a.length; 141 int N = a[0].length; 142 System.out.println(M + " " + N); 143 for (int i = 0; i < M; i++) { 144 for (int j = 0; j < N; j++) { 145 System.out.printf("%9d ", a[i][j]); 146 } 147 System.out.println(); 148 } 149 } 150 151 152 153 154 /** 155 * Print an array of booleans to standard output. 156 * @param a 157 */ 158 public static void print(boolean[] a) { 159 int N = a.length; 160 System.out.println(N); 161 for (int i = 0; i < N; i++) { 162 if (a[i]) System.out.print("1 "); 163 else System.out.print("0 "); 164 } 165 System.out.println(); 166 } 167 168 169 170 /** 171 * Print the M-by-N array of booleans to standard output. 172 * @param a 173 */ 174 public static void print(boolean[][] a) { 175 int M = a.length; 176 int N = a[0].length; 177 System.out.println(M + " " + N); 178 for (int i = 0; i < M; i++) { 179 for (int j = 0; j < N; j++) { 180 if (a[i][j]) System.out.print("1 "); 181 else System.out.print("0 "); 182 } 183 System.out.println(); 184 } 185 } 186 187 188 /** 189 * Test client. 190 * @param args 191 */ 192 public static void main(String[] args) { 193 194 195 } 196 197}