OnJava8-Examples/arrays/FillingArrays.java

56 lines
1.5 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// arrays/FillingArrays.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-06-15 17:47:35 -07:00
// Using Arrays.fill()
import java.util.*;
2016-01-25 18:05:55 -08:00
import static onjava.ArrayShow.*;
2015-06-15 17:47:35 -07:00
public class FillingArrays {
public static void main(String[] args) {
int size = 6;
boolean[] a1 = new boolean[size];
byte[] a2 = new byte[size];
char[] a3 = new char[size];
short[] a4 = new short[size];
int[] a5 = new int[size];
long[] a6 = new long[size];
float[] a7 = new float[size];
double[] a8 = new double[size];
String[] a9 = new String[size];
Arrays.fill(a1, true);
2016-01-25 18:05:55 -08:00
show("a1", a1);
2015-06-15 17:47:35 -07:00
Arrays.fill(a2, (byte)11);
2016-01-25 18:05:55 -08:00
show("a2", a2);
2015-06-15 17:47:35 -07:00
Arrays.fill(a3, 'x');
2016-01-25 18:05:55 -08:00
show("a3", a3);
2015-06-15 17:47:35 -07:00
Arrays.fill(a4, (short)17);
2016-01-25 18:05:55 -08:00
show("a4", a4);
2015-06-15 17:47:35 -07:00
Arrays.fill(a5, 19);
2016-01-25 18:05:55 -08:00
show("a5", a5);
2015-06-15 17:47:35 -07:00
Arrays.fill(a6, 23);
2016-01-25 18:05:55 -08:00
show("a6", a6);
2015-06-15 17:47:35 -07:00
Arrays.fill(a7, 29);
2016-01-25 18:05:55 -08:00
show("a7", a7);
2015-06-15 17:47:35 -07:00
Arrays.fill(a8, 47);
2016-01-25 18:05:55 -08:00
show("a8", a8);
2015-06-15 17:47:35 -07:00
Arrays.fill(a9, "Hello");
2016-01-25 18:05:55 -08:00
show("a9", a9);
2015-06-15 17:47:35 -07:00
// Manipulating ranges:
Arrays.fill(a9, 3, 5, "World");
2016-01-25 18:05:55 -08:00
show("a9", a9);
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2016-07-22 14:45:35 -06:00
a1: [true, true, true, true, true, true]
a2: [11, 11, 11, 11, 11, 11]
a3: [x, x, x, x, x, x]
a4: [17, 17, 17, 17, 17, 17]
a5: [19, 19, 19, 19, 19, 19]
a6: [23, 23, 23, 23, 23, 23]
a7: [29.0, 29.0, 29.0, 29.0, 29.0, 29.0]
a8: [47.0, 47.0, 47.0, 47.0, 47.0, 47.0]
a9: [Hello, Hello, Hello, Hello, Hello, Hello]
a9: [Hello, Hello, Hello, World, World, Hello]
2015-09-07 11:44:36 -06:00
*/