Java example source code file (ArrayObjectMethods.java)
This example Java source code file (ArrayObjectMethods.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
The ArrayObjectMethods.java Java example source code
/*
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4906359 6239296
* @summary Basic test for content-based array object methods
* @author Josh Bloch, Martin Buchholz
*/
import java.util.*;
import java.io.*;
public class ArrayObjectMethods {
int[] sizes = {0, 10, 100, 200, 1000};
void test(String[] args) throws Throwable {
equal(Arrays.deepToString(null), "null");
equal(Arrays.deepToString(new Object[]{}), "[]");
equal(Arrays.deepToString(new Object[]{null}), "[null]");
equal(Arrays.deepToString(new Object[]{null, 1}), "[null, 1]");
equal(Arrays.deepToString(new Object[]{1, null}), "[1, null]");
equal(Arrays.deepToString(new Object[]{new Object[]{}, null}), "[[], null]");
{
Object[] a = {1, null};
a[1] = a;
equal(Arrays.deepToString(a), "[1, [...]]");
a[0] = a;
equal(Arrays.deepToString(a), "[[...], [...]]");
a[0] = a[1] = new Object[]{1, null, a};
equal(Arrays.deepToString(a), "[[1, null, [...]], [1, null, [...]]]");
}
for (int size : sizes) {
{
long[] a = Rnd.longArray(size);
equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());
equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());
}
{
int[] a = Rnd.intArray(size);
equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());
equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());
}
{
short[] a = Rnd.shortArray(size);
equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());
equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());
}
{
char[] a = Rnd.charArray(size);
equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());
equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());
}
{
byte[] a = Rnd.byteArray(size);
equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());
equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());
}
{
boolean[] a = Rnd.booleanArray(size);
equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());
equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());
}
{
double[] a = Rnd.doubleArray(size);
equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());
equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());
}
{
float[] a = Rnd.floatArray(size);
equal(Arrays.toString(a), PrimitiveArrays.asList(a).toString());
equal(Arrays.hashCode(a), PrimitiveArrays.asList(a).hashCode());
}
{
Object[] a = Rnd.flatObjectArray(size);
equal(Arrays.toString(a), Arrays.asList(a).toString());
equal(Arrays.deepToString(a), Arrays.asList(a).toString());
equal(Arrays.hashCode(a), Arrays.asList(a).hashCode());
}
if (size <= 200) {
Object[] a = Rnd.nestedObjectArray(size);
List aList = deepToList(a);
equal(Arrays.toString(a), Arrays.asList(a).toString());
equal(Arrays.deepToString(a), aList.toString());
equal(Arrays.deepHashCode(a), aList.hashCode());
equal(Arrays.hashCode(a), Arrays.asList(a).hashCode());
Object[] deepCopy = (Object[]) deepCopy(a);
check(Arrays.deepEquals(a, deepCopy));
check(Arrays.deepEquals(deepCopy, a));
// Make deepCopy != a
if (size == 0)
deepCopy = new Object[] {"foo"};
else if (deepCopy[deepCopy.length - 1] == null)
deepCopy[deepCopy.length - 1] = "baz";
else
deepCopy[deepCopy.length - 1] = null;
check(! Arrays.deepEquals(a, deepCopy));
check(! Arrays.deepEquals(deepCopy, a));
}
}
}
// Utility method to turn an array into a list "deeply," turning
// all primitives into objects
List<Object> deepToList(Object[] a) {
List<Object> result = new ArrayList