1 Create a method that takes an array of Pet as an argument. Call the method, creating the argument dynamically. Demonstrate that ordinary aggregate array intitialization doesn't work in this case.Discover the only situations where ordinary aggregate array initialization works, and where dynamic aggregate array initialization is redundant.
package job;
import java.util.*;
import typeinfo.pets.*;
class A {
public String toString() {
return "A Object";
}
}
public class Main {
public static void test(Pet[] a) {
System.out.println(Arrays.asList(a));
}
// Generic version:
public static void test(T[] t) {
System.out.println(Arrays.asList(t));
}
// int version:
public static void test(int[] ia) {
System.out.println(Arrays.toString(ia));
}
public static void main(String[] args) {
System.out.println("For objects, e.g., Pets:");
// Array is created and initialized (aggregate initialization)
// but all elements are null, not Pets:
test(new Pet[3]);
// Dynamic aggregate initialization works;
// elements are now Pets:
test(new Pet[]{
new Pet(), new Pet()});
// Aggregate initialization this way works:
Pet[] a = {new Pet(),
new Pet(), new Pet()};
test(a);
// Elements initialized to null:
Pet[] bsa = new Pet[2];
test(bsa);
bsa = a;
test(bsa);
System.out.println("-------------");
System.out.println("For primitives, e.g., int:");
// Dynamic aggregate initialization works:
test(new int[]{new Integer(0), new Integer(0)});
// But may be considered redundant, since
// aggregate initialization works:
// elements initialized to zero (not null):
test(new int[2]);
// Ordinary aggregate initialization this way also works:
int[] ia = {1, 2, 3,};
test(ia);
}
}
2 Write a method that takes an int argument and returns a array of that size filled BerylliumSpheres.
package job;
import java.util.*;
class B{
}
class A {
private static final B[] pp = {new B(), new B(), new B()};
static B[] p(int i) {
boolean[] b = new boolean[i];
B[] d = new B[i];
Random rand = new Random(22);
int t = 0;
for (int j = 0; j < i; j++) {
do
t = rand.nextInt(d.length);
while (b[t]);
d[j] = pp[t];
}
return d;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(Arrays.toString(A.p(2)));
}
}
3 Write a method that creates and initializes a two-dimensional array of double.The size of the array is determined by the arguments of the method, and the initialization values are a range determined by beginning and ending values that are also arguments of the method. Create a second method that will print the array generated by the first method. In main() test the methods by creating and printing several different sizes of arrays.
package job;
import java.util.*;
class A {
double[][] f(int size1, int size2, double start, double end) {
double[][] d = new double[size1][size2];
for (int i = 0; i < size1; i++)
for (int j = 0; j < size2; j++)
d[i][j] = start + ((double) j / (size2 - 1)) * (((double) j + (double) i) / (size2 - 1 + i)) * (end - start);
return d;
}
void g(int size1, int size2, double start, double end) {
System.out.println(Arrays.deepToString(f(size1, size2, start, end)));
}
}
public class Main {
public static void main(String[] args) {
A a = new A();
a.g(2, 4, 3, 19);
}
}
4 Repeat the previous exercise for a three-dimensional array.
package job;
import java.util.*;
class A {
double[][] f(int size1, int size2, double start, double end) {
double[][] d = new double[size1][size2];
for (int i = 0; i < size1; i++)
for (int j = 0; j < size2; j++)
d[i][j] = start + ((double) j / (size2 - 1)) * (((double) j + (double) i) / (size2 - 1 + i)) * (end - start);
return d;
}
void g(int size1, int size2, double start, double end) {
System.out.println(Arrays.deepToString(f(size1, size2, start, end)));
}
}
public class Maim{
public static void main(String[] args) {
A a=new A();
a.g(2,4,3,19);
}
}
5 Demonstrate that multidimensional arrays of non-primitives are automatically initialized to null.
package job;
import java.util.*;
class A {
double[][] f(int size1, int size2, double start, double end) {
double[][] d = new double[size1][size2];
return d;
}
void g(int size1, int size2, double start, double end) {
System.out.println(Arrays.deepToString(f(size1, size2, start, end)));
}
}
public class Main {
public static void main(String[] args) {
A a = new A();
a.g(2, 4, 3, 19);
}
}
6 略
7 略
8 Demonstrate the assertions in the previous paragraph.
package job;
import java.util.*;
public class Main {
public static void main(String[] args) {
Object[] o = new Object[]{new String("fsd"), new Integer(23)};
System.out.println(o[0].getClass());
System.out.println(o[1].getClass());
}
}
9 略
10 略
11 Show that autoboxing doesn't work with arrays.
package job;
import java.util.*;
public class Main {
public static void main(String[] args) {
Integer[] a = new Integer[]{1, 2, 4, 6};
System.out.println(Arrays.toString(a));
int[] b=a;//Error:(11, 17) java: 不兼容的类型: java.lang.Integer[]无法转换为int[]
}
}
12 Create an initialized array of double using CountingGenerator. Print the results.
package job;
import java.util.*;
public class Main {
public static void main(String[] args) {
System.out.println(Arrays.toString(Generated.array(Double.class, new CountingGenerator.Double(), 5)));
}
}
13 Fill a String using CountingGenerator.Character.
package job;
import java.util.*;
public class Main {
public static void main(String[] args) {
System.out.println(Arrays.toString(Generated.array(Double.class, new CountingGenerator.Double(), 5)));
}
}
14 Create an array of each primitive type, then fill each array by using CountingGenerator. Print each array.
package job;
import java.util.*;
public class Main {
public static void main(String[] args) {
CountingGenerator.Double cd = new CountingGenerator.Double();
Double[] d = new Double[5];
for (int i = 0; i < d.length; i++) {
d[i] = cd.next();
}
System.out.println(Arrays.toString(d));
}
}
15 Modify ContainerComparison.java by creating a net.mindview.util.Generator for // BerylliumSphere, and change main() to use that net.mindview.util.Generator// with Generated.array().
略
16 Starting with CountingGenerator.java, create a SkipGenerator class that produces new values by incrementing according to a constructor argument. Modify TestArrayGeneration.java to show that your new class works correctly.
略
17 Create and test a Generator for BigDecimal, and ensure that it works with the Generated methods.
略
18 Create and fill and array of BerylliumSphere. Copy this array to a new array and show that it's a shallow copy.
package job;
import java.util.*;
public class Main{
public static void main(String[] args) {
Pet[] p=new Pet[]{new Cat(),new Dog(),new Pug()};
Pet[] m=new Pet[]{new Pet(),new Pet(),new Pet()};
System.arraycopy(p,0,m,0,p.length);
System.out.println(Arrays.toString(p));
System.out.println(Arrays.toString(m));
}
}
19 Create a class with an int field that's initialized from a constructor argument. Create two arrays of these objects, using identical intitialization values for each array, and show that Arrays.equals() says that they are unequal. Add an equals() method to your class to fix the problem.
package job;
import java.util.*;
class A {
public int n;
A(int n) {
this.n = n;
}
public boolean equals(Object b) {
if (b.getClass().getSimpleName() == "A")
return (((A) b).n == this.n);
return false;
}
}
public class Main {
public static void main(String[] args) {
A[] a = {new A(1)};
A[] b = {new A(1)};
System.out.println(Arrays.equals(a, b));
}
}
20 Demonstrate deepEquals() for multidimensional arrays.
package job;
import java.util.*;
public class Main {
public static void main(String[] args) {
int[][][] a = {{{2, 3}, {5, 7}}, {{3, 7, 9}, {7, 9, 2}, {5}}};
int[][][] b = {{{2, 3}, {5, 7}}, {{3, 7, 9}, {7, 9, 2}, {5}}};
System.out.println(Arrays.deepEquals(a, b));
}
}
21 Try to sort an array of the objects in Exercise 18. Implement Comparable to fix the problem. Now create a Comparator to sort.
package job;
import java.util.*;
class CompBerylliumSphere implements Comparable {
private static long count;
protected final long id = count++;
public int compareTo(CompBerylliumSphere c2) {
return (this.id < c2.id ? -1 : (this.id == c2.id ? 0 : 1));
}
public String toString() {
return "BerylliumSphere " + id;
}
}
class AComparator implements Comparator{
public int compare(CompBerylliumSphere c1, CompBerylliumSphere c2){
return (c2.id
22 Show that the results of performing a binarySearch() on an unsorted array are unpredictable.
package job;
import java.util.*;
class CompBerylliumSphere
implements Comparable {
private static long count;
protected final long id = count++;
public int compareTo(CompBerylliumSphere c2) {
return (this.id < c2.id ? -1 : (this.id == c2.id ? 0 : 1));
}
public String toString() {
return "BerylliumSphere " + id;
}
}
class AComparator implements Comparator {
public int compare(CompBerylliumSphere c1, CompBerylliumSphere c2) {
return (c2.id < c1.id ? -1 : (c2.id == c1.id ? 0 : 1));
}
}
public class Main {
public static void main(String[] args) {
CompBerylliumSphere c = new CompBerylliumSphere();
CompBerylliumSphere[] m = new CompBerylliumSphere[]{new CompBerylliumSphere(), new CompBerylliumSphere(), new CompBerylliumSphere(), new CompBerylliumSphere(), c, new CompBerylliumSphere(), new CompBerylliumSphere()};
//Arrays.sort(m,new AComparator());
System.out.println(Arrays.binarySearch(m, c/*,new AComparator()*/));
System.out.println(Arrays.toString(m));
}
}
23 Create an array of Integer fill it with random int values (using autoboxing), and sort it into reverse order using a Comparator.
package job;
import java.util.*;
public class Main {
public static void main(String[] args) {
Integer[] ig = new Integer[5];
Generated.array(ig, new RandomGenerator.Integer());
Arrays.sort(ig, Collections.reverseOrder());
System.out.println(Arrays.toString(ig));
}
}
24 Show that the class from Exercise 19 can be searched.
package job;
import java.util.*;
class A implements Comparable {
public int n;
A(int n) {
this.n = n;
}
public boolean equals(Object b) {
if (b.getClass().getSimpleName() == "A") return (((A) b).n == this.n);
return false;
}
public int compareTo(A rv) {
return (this.n < rv.n ? -1 : (this.n == rv.n ? 0 : 1));
}
}
public class Main {
public static void main(String[] args) {
A mm = new A(5);
A[] a = {new A(1), new A(3), mm, new A(5), new A(7)};
//A[] b={new A(1)};
//print(Arrays.equals(a,b));
Arrays.sort(a);
System.out.println(Arrays.binarySearch(a, mm));
}
}