Source of Arrays2.java


  1: //: com:bruceeckel:util:Arrays2.java
  2: // From 'Thinking in Java, 2nd ed.' by Bruce Eckel
  3: // www.BruceEckel.com. See copyright notice in CopyRight.txt.
  4: // A supplement to java.util.Arrays, to provide
  5: // additional useful functionality when working
  6: // with arrays. Allows any array to be printed,
  7: // and to be filled via a user-defined 
  8: // "generator" object.
  9: package com.bruceeckel.util;
 10: import java.util.*;

 12: public class Arrays2 {
 13:   private static void
 14:   start(int from, int to, int length) {
 15:     if(from != 0 || to != length)
 16:       System.out.print("["+ from +":"+ to +"] ");
 17:     System.out.print("(");
 18:   }
 19:   private static void end() {
 20:     System.out.println(")");
 21:   }
 22:   public static void print(Object[] a) {
 23:     print(a, 0, a.length);
 24:   }
 25:   public static void 
 26:   print(String msg, Object[] a) {
 27:     System.out.print(msg + " ");
 28:     print(a, 0, a.length);
 29:   }
 30:   public static void 
 31:   print(Object[] a, int from, int to){
 32:     start(from, to, a.length);
 33:     for(int i = from; i < to; i++) {
 34:       System.out.print(a[i]);
 35:       if(i < to -1)
 36:         System.out.print(", ");
 37:     }
 38:     end();
 39:   }
 40:   public static void print(boolean[] a) {
 41:       print(a, 0, a.length);
 42:   }
 43:   public static void 
 44:   print(String msg, boolean[] a) {
 45:     System.out.print(msg + " ");
 46:     print(a, 0, a.length);
 47:   }
 48:   public static void 
 49:   print(boolean[] a, int from, int to) {
 50:     start(from, to, a.length);
 51:     for(int i = from; i < to; i++) {
 52:       System.out.print(a[i]);
 53:       if(i < to -1)
 54:         System.out.print(", ");
 55:     }
 56:     end();
 57:   }
 58:   public static void print(byte[] a) {
 59:       print(a, 0, a.length);
 60:   }
 61:   public static void 
 62:   print(String msg, byte[] a) {
 63:     System.out.print(msg + " ");
 64:     print(a, 0, a.length);
 65:   }
 66:   public static void 
 67:   print(byte[] a, int from, int to) {
 68:     start(from, to, a.length);
 69:     for(int i = from; i < to; i++) {
 70:       System.out.print(a[i]);
 71:       if(i < to -1)
 72:         System.out.print(", ");
 73:     }
 74:     end();
 75:   }
 76:   public static void print(char[] a) {
 77:       print(a, 0, a.length);
 78:   }
 79:   public static void 
 80:   print(String msg, char[] a) {
 81:     System.out.print(msg + " ");
 82:     print(a, 0, a.length);
 83:   }
 84:   public static void 
 85:   print(char[] a, int from, int to) {
 86:     start(from, to, a.length);
 87:     for(int i = from; i < to; i++) {
 88:       System.out.print(a[i]);
 89:       if(i < to -1)
 90:         System.out.print(", ");
 91:     }
 92:     end();
 93:   }
 94:   public static void print(short[] a) {
 95:       print(a, 0, a.length);
 96:   }
 97:   public static void 
 98:   print(String msg, short[] a) {
 99:     System.out.print(msg + " ");
100:     print(a, 0, a.length);
101:   }
102:   public static void 
103:   print(short[] a, int from, int to) {
104:     start(from, to, a.length);
105:     for(int i = from; i < to; i++) {
106:       System.out.print(a[i]);
107:       if(i < to - 1)
108:         System.out.print(", ");
109:     }
110:     end();
111:   }
112:   public static void print(int[] a) {
113:       print(a, 0, a.length);
114:   }
115:   public static void 
116:   print(String msg, int[] a) {
117:     System.out.print(msg + " ");
118:     print(a, 0, a.length);
119:   }
120:   public static void 
121:   print(int[] a, int from, int to) {
122:     start(from, to, a.length);
123:     for(int i = from; i < to; i++) {
124:       System.out.print(a[i]);
125:       if(i < to - 1)
126:         System.out.print(", ");
127:     }
128:     end();
129:   }
130:   public static void print(long[] a) {
131:     print(a, 0, a.length);
132:   }
133:   public static void 
134:   print(String msg, long[] a) {
135:     System.out.print(msg + " ");
136:     print(a, 0, a.length);
137:   }
138:   public static void 
139:   print(long[] a, int from, int to) {
140:     start(from, to, a.length);
141:     for(int i = from; i < to; i++) {
142:       System.out.print(a[i]);
143:       if(i < to - 1)
144:         System.out.print(", ");
145:     }
146:     end();
147:   }
148:   public static void print(float[] a) {
149:       print(a, 0, a.length);
150:   }
151:   public static void 
152:   print(String msg, float[] a) {
153:     System.out.print(msg + " ");
154:     print(a, 0, a.length);
155:   }
156:   public static void 
157:   print(float[] a, int from, int to) {
158:     start(from, to, a.length);
159:     for(int i = from; i < to; i++) {
160:       System.out.print(a[i]);
161:       if(i < to - 1)
162:         System.out.print(", ");
163:     }
164:     end();
165:   }
166:   public static void print(double[] a) {
167:       print(a, 0, a.length);
168:   }
169:   public static void 
170:   print(String msg, double[] a) {
171:     System.out.print(msg + " ");
172:     print(a, 0, a.length);
173:   }
174:   public static void 
175:   print(double[] a, int from, int to){
176:     start(from, to, a.length);
177:     for(int i = from; i < to; i++) {
178:       System.out.print(a[i]);
179:       if(i < to - 1)
180:         System.out.print(", ");
181:     }
182:     end();
183:   }
184:   // Fill an array using a generator:
185:   public static void 
186:   fill(Object[] a, Generator gen) {
187:       fill(a, 0, a.length, gen);
188:   }
189:   public static void 
190:   fill(Object[] a, int from, int to, 
191:        Generator gen){
192:     for(int i = from; i < to; i++)
193:       a[i] = gen.next();
194:   }
195:   public static void 
196:   fill(boolean[] a, BooleanGenerator gen) {
197:       fill(a, 0, a.length, gen);
198:   }
199:   public static void 
200:   fill(boolean[] a, int from, int to,
201:        BooleanGenerator gen) {
202:     for(int i = from; i < to; i++)
203:       a[i] = gen.next();
204:   }
205:   public static void 
206:   fill(byte[] a, ByteGenerator gen) {
207:       fill(a, 0, a.length, gen);
208:   }
209:   public static void 
210:   fill(byte[] a, int from, int to, 
211:        ByteGenerator gen) {
212:     for(int i = from; i < to; i++)
213:       a[i] = gen.next();
214:   }
215:   public static void 
216:   fill(char[] a, CharGenerator gen) {
217:       fill(a, 0, a.length, gen);
218:   }
219:   public static void 
220:   fill(char[] a, int from, int to, 
221:        CharGenerator gen) {
222:     for(int i = from; i < to; i++)
223:       a[i] = gen.next();
224:   }
225:   public static void 
226:   fill(short[] a, ShortGenerator gen) {
227:       fill(a, 0, a.length, gen);
228:   }
229:   public static void 
230:   fill(short[] a, int from, int to, 
231:        ShortGenerator gen) {
232:     for(int i = from; i < to; i++)
233:       a[i] = gen.next();
234:   }
235:   public static void 
236:   fill(int[] a, IntGenerator gen) {
237:       fill(a, 0, a.length, gen);
238:   }
239:   public static void 
240:   fill(int[] a, int from, int to, 
241:        IntGenerator gen) {
242:     for(int i = from; i < to; i++)
243:       a[i] = gen.next();
244:   }
245:   public static void 
246:   fill(long[] a, LongGenerator gen) {
247:       fill(a, 0, a.length, gen);
248:   }
249:   public static void 
250:   fill(long[] a, int from, int to, 
251:        LongGenerator gen) {
252:     for(int i = from; i < to; i++)
253:       a[i] = gen.next();
254:   }
255:   public static void 
256:   fill(float[] a, FloatGenerator gen) {
257:       fill(a, 0, a.length, gen);
258:   }
259:   public static void 
260:   fill(float[] a, int from, int to, 
261:        FloatGenerator gen) {
262:     for(int i = from; i < to; i++)
263:       a[i] = gen.next();
264:   }
265:   public static void 
266:   fill(double[] a, DoubleGenerator gen) {
267:       fill(a, 0, a.length, gen);
268:   }
269:   public static void 
270:   fill(double[] a, int from, int to,
271:        DoubleGenerator gen){
272:     for(int i = from; i < to; i++)
273:       a[i] = gen.next();
274:   }
275:   private static Random r = new Random();
276:   public static class RandBooleanGenerator 
277:   implements BooleanGenerator {
278:     public boolean next() { 
279:       return r.nextBoolean();
280:     }
281:   }
282:   public static class RandByteGenerator 
283:   implements ByteGenerator {
284:     public byte next() { 
285:       return (byte)r.nextInt();
286:     }
287:   }
288:   static String ssource = 
289:     "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
290:     "abcdefghijklmnopqrstuvwxyz";
291:   static char[] src = ssource.toCharArray();
292:   public static class RandCharGenerator 
293:   implements CharGenerator {
294:     public char next() {
295:       int pos = Math.abs(r.nextInt());
296:       return src[pos % src.length];
297:     }
298:   }
299:   public static class RandStringGenerator
300:   implements Generator {
301:     private int len;
302:     private RandCharGenerator cg = 
303:       new RandCharGenerator();
304:     public RandStringGenerator(int length) {
305:       len = length;
306:     }
307:     public Object next() {
308:       char[] buf = new char[len];
309:       for(int i = 0; i < len; i++)
310:         buf[i] = cg.next();
311:       return new String(buf);
312:     }
313:   }
314:   public static class RandShortGenerator 
315:   implements ShortGenerator {
316:     public short next() { 
317:       return (short)r.nextInt();
318:     }
319:   }
320:   public static class RandIntGenerator 
321:   implements IntGenerator {
322:     private int mod = 10000;
323:     public RandIntGenerator() {}
324:     public RandIntGenerator(int modulo) {
325:       mod = modulo;
326:     }
327:     public int next() { 
328:       return r.nextInt() % mod; 
329:     }
330:   }
331:   public static class RandLongGenerator 
332:   implements LongGenerator {
333:     public long next() { return r.nextLong(); }
334:   }
335:   public static class RandFloatGenerator 
336:   implements FloatGenerator {
337:     public float next() { return r.nextFloat(); }
338:   }
339:   public static class RandDoubleGenerator 
340:   implements DoubleGenerator {
341:     public double next() {return r.nextDouble();}
342:   }
343: } ///:~