Java Tutorial/Collections/Array Objects
Содержание
- 1 Arrays of Objects
- 2 Arrays of Strings: initial values determine the size of the array
- 3 Arrays of Strings: using "new" operator
- 4 Check if the given object is an array (primitve or native).
- 5 Checks whether two arrays are the same length, treating null arrays as length 0.
- 6 Checks whether two arrays are the same type taking into account multi-dimensional arrays.
- 7 Demonstrate String arrays.
- 8 Removes the element at the specified position from the specified array.
- 9 Removes the first occurrence of the specified element from the specified array.
- 10 Reverses the order of the given long type value array.
- 11 Turn an array of ints into a printable string.
Arrays of Objects
Arrays of Objects only stores references to the actual objects, and initially each reference is null unless explicitly initialized.
public class MainClass {
public static void main (String args[]) {
int array1[] = {1, 2, 3, 4, 5};
for(int i: array1){
System.out.println(i);
}
}
}
1 2 3 4 5
Arrays of Strings: initial values determine the size of the array
public class MainClass {
public static void main(String[] arg) {
String[] colors = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"};
for(String s: colors){
System.out.println(s);
}
}
}
red orange yellow green blue indigo violet
Arrays of Strings: using "new" operator
public class MainClass {
public static void main(String[] arg) {
String[] names = new String[5];
names[0] = "qwert";
names[1] = "yuiop";
names[2] = "asdfg";
names[3] = "hjkl";
names[4] = "zxcvb";
System.out.println(names[4]);
}
}
Check if the given object is an array (primitve or native).
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
public class Main {
/**
* Check if the given object is an array (primitve or native).
*
* @param obj Object to test.
* @return True of the object is an array.
*/
public static boolean isArray(final Object obj) {
if (obj != null)
return obj.getClass().isArray();
return false;
}
}
Checks whether two arrays are the same length, treating null arrays as length 0.
import java.lang.reflect.Array;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
/**
* @author Stephen Colebourne
* @author Moritz Petersen
* @author
* @author Maarten Coene
* @since 2.0
* @version $Id: ArrayUtils.java 632503 2008-03-01 00:21:52Z ggregory $
*/
public class Main {
// Is same length
//-----------------------------------------------------------------------
/**
* Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.
*
* Any multi-dimensional aspects of the arrays are ignored.
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(Object[] array1, Object[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
/**
* Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(long[] array1, long[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
/**
* Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(int[] array1, int[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
/**
* Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(short[] array1, short[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
/**
* Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(char[] array1, char[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
/**
* Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(byte[] array1, byte[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
/**
* Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(double[] array1, double[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
/**
* Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(float[] array1, float[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
/**
* Checks whether two arrays are the same length, treating
* <code>null</code> arrays as length <code>0</code>.
*
* @param array1 the first array, may be <code>null</code>
* @param array2 the second array, may be <code>null</code>
* @return <code>true</code> if length of arrays matches, treating
* <code>null</code> as an empty array
*/
public static boolean isSameLength(boolean[] array1, boolean[] array2) {
if ((array1 == null && array2 != null && array2.length > 0) ||
(array2 == null && array1 != null && array1.length > 0) ||
(array1 != null && array2 != null && array1.length != array2.length)) {
return false;
}
return true;
}
}
Checks whether two arrays are the same type taking into account multi-dimensional arrays.
import java.lang.reflect.Array;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
/**
* @author Stephen Colebourne
* @author Moritz Petersen
* @author
* @author Maarten Coene
* @since 2.0
* @version $Id: ArrayUtils.java 632503 2008-03-01 00:21:52Z ggregory $
*/
public class Main {
/**
* Checks whether two arrays are the same type taking into account
* multi-dimensional arrays.
*
* @param array1 the first array, must not be <code>null</code>
* @param array2 the second array, must not be <code>null</code>
* @return <code>true</code> if type of arrays matches
* @throws IllegalArgumentException if either array is <code>null</code>
*/
public static boolean isSameType(Object array1, Object array2) {
if (array1 == null || array2 == null) {
throw new IllegalArgumentException("The Array must not be null");
}
return array1.getClass().getName().equals(array2.getClass().getName());
}
}
Demonstrate String arrays.
class StringDemo3 {
public static void main(String args[]) {
String str[] = { "one", "two", "three" };
for (int i = 0; i < str.length; i++)
System.out.println("str[" + i + "]: " + str[i]);
}
}
Removes the element at the specified position from the specified array.
/* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.lang.reflect.Array;
/**
* Operations on arrays, primitive arrays (like <code>int[]</code>) and
* primitive wrapper arrays (like <code>Integer[]</code>).
*
* This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code>
* array input. However, an Object array that contains a <code>null</code>
* element may throw an exception. Each method documents its behaviour.
*
* @author Stephen Colebourne
* @author Moritz Petersen
* @author
* @author Maarten Coene
* @since 2.0
* @version $Id: ArrayUtils.java 632503 2008-03-01 00:21:52Z ggregory $
*/
public class Main {
/**
* Removes the element at the specified position from the specified array.
* All subsequent elements are shifted to the left (substracts one from
* their indices).
*
* This method returns a new array with the same elements of the input
* array except the element on the specified position. The component
* type of the returned array is always the same as that of the input
* array.
*
* If the input array is <code>null</code>, an IndexOutOfBoundsException
* will be thrown, because in that case no valid index can be specified.
*
* <pre>
* ArrayUtils.remove([1], 0) = []
* ArrayUtils.remove([2, 6], 0) = [6]
* ArrayUtils.remove([2, 6], 1) = [2]
* ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
* </pre>
*
* @param array the array to remove the element from, may not be <code>null</code>
* @param index the position of the element to be removed
* @return A new array containing the existing elements except the element
* at the specified position.
* @throws IndexOutOfBoundsException if the index is out of range
* (index < 0 || index >= array.length), or if the array is <code>null</code>.
* @since 2.1
*/
public static short[] remove(short[] array, int index) {
return (short[]) remove((Object) array, index);
}
/**
* Removes the element at the specified position from the specified array.
* All subsequent elements are shifted to the left (substracts one from
* their indices).
*
* This method returns a new array with the same elements of the input
* array except the element on the specified position. The component
* type of the returned array is always the same as that of the input
* array.
*
* If the input array is <code>null</code>, an IndexOutOfBoundsException
* will be thrown, because in that case no valid index can be specified.
*
* @param array the array to remove the element from, may not be <code>null</code>
* @param index the position of the element to be removed
* @return A new array containing the existing elements except the element
* at the specified position.
* @throws IndexOutOfBoundsException if the index is out of range
* (index < 0 || index >= array.length), or if the array is <code>null</code>.
* @since 2.1
*/
private static Object remove(Object array, int index) {
int length = getLength(array);
if (index < 0 || index >= length) {
throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
}
Object result = Array.newInstance(array.getClass().getComponentType(), length - 1);
System.arraycopy(array, 0, result, 0, index);
if (index < length - 1) {
System.arraycopy(array, index + 1, result, index, length - index - 1);
}
return result;
}
/**
* Finds the index of the given value in the array starting at the given index.
*
* This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.
*
* A negative startIndex is treated as zero. A startIndex larger than the array
* length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).
*
* @param array the array to search through for the object, may be <code>null</code>
* @param valueToFind the value to find
* @param startIndex the index to start searching at
* @return the index of the value within the array,
* {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
*/
public static int indexOf(short[] array, short valueToFind) {
if (array == null) {
return -1;
}
for (int i = 0; i < array.length; i++) {
if (valueToFind == array[i]) {
return i;
}
}
return -1;
}
/**
* Returns the length of the specified array.
* This method can deal with <code>Object</code> arrays and with primitive arrays.
*
* If the input array is <code>null</code>, <code>0</code> is returned.
*
* <pre>
* ArrayUtils.getLength(null) = 0
* ArrayUtils.getLength([]) = 0
* ArrayUtils.getLength([null]) = 1
* ArrayUtils.getLength([true, false]) = 2
* ArrayUtils.getLength([1, 2, 3]) = 3
* ArrayUtils.getLength(["a", "b", "c"]) = 3
* </pre>
*
* @param array the array to retrieve the length from, may be null
* @return The length of the array, or <code>0</code> if the array is <code>null</code>
* @throws IllegalArgumentException if the object arguement is not an array.
* @since 2.1
*/
public static int getLength(Object array) {
if (array == null) {
return 0;
}
return Array.getLength(array);
}
/**
* Shallow clones an array returning a typecast result and handling
* <code>null</code>.
*
* The objects in the array are not cloned, thus there is no special
* handling for multi-dimensional arrays.
*
* This method returns <code>null</code> for a <code>null</code> input array.
*
* @param array the array to shallow clone, may be <code>null</code>
* @return the cloned array, <code>null</code> if <code>null</code> input
*/
public static short[] clone(short[] array) {
if (array == null) {
return null;
}
return (short[]) array.clone();
}
}
Removes the first occurrence of the specified element from the specified array.
/* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.lang.reflect.Array;
/**
* Operations on arrays, primitive arrays (like <code>int[]</code>) and
* primitive wrapper arrays (like <code>Integer[]</code>).
*
* This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code>
* array input. However, an Object array that contains a <code>null</code>
* element may throw an exception. Each method documents its behaviour.
*
* @author Stephen Colebourne
* @author Moritz Petersen
* @author
* @author Maarten Coene
* @since 2.0
* @version $Id: ArrayUtils.java 632503 2008-03-01 00:21:52Z ggregory $
*/
public class Main {
/**
* Removes the first occurrence of the specified element from the
* specified array. All subsequent elements are shifted to the left
* (substracts one from their indices). If the array doesn"t contains
* such an element, no elements are removed from the array.
*
* This method returns a new array with the same elements of the input
* array except the first occurrence of the specified element. The component
* type of the returned array is always the same as that of the input
* array.
*
* <pre>
* ArrayUtils.removeElement(null, 1) = null
* ArrayUtils.removeElement([], 1) = []
* ArrayUtils.removeElement([1], 2) = [1]
* ArrayUtils.removeElement([1, 3], 1) = [3]
* ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
* </pre>
*
* @param array the array to remove the element from, may be <code>null</code>
* @param element the element to be removed
* @return A new array containing the existing elements except the first
* occurrence of the specified element.
* @since 2.1
*/
public static short[] removeElement(short[] array, short element) {
int index = indexOf(array, element);
if (index == -1) {
return clone(array);
}
return remove(array, index);
}
/**
* Removes the element at the specified position from the specified array.
* All subsequent elements are shifted to the left (substracts one from
* their indices).
*
* This method returns a new array with the same elements of the input
* array except the element on the specified position. The component
* type of the returned array is always the same as that of the input
* array.
*
* If the input array is <code>null</code>, an IndexOutOfBoundsException
* will be thrown, because in that case no valid index can be specified.
*
* <pre>
* ArrayUtils.remove([1], 0) = []
* ArrayUtils.remove([2, 6], 0) = [6]
* ArrayUtils.remove([2, 6], 1) = [2]
* ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
* </pre>
*
* @param array the array to remove the element from, may not be <code>null</code>
* @param index the position of the element to be removed
* @return A new array containing the existing elements except the element
* at the specified position.
* @throws IndexOutOfBoundsException if the index is out of range
* (index < 0 || index >= array.length), or if the array is <code>null</code>.
* @since 2.1
*/
public static short[] remove(short[] array, int index) {
return (short[]) remove((Object) array, index);
}
/**
* Removes the element at the specified position from the specified array.
* All subsequent elements are shifted to the left (substracts one from
* their indices).
*
* This method returns a new array with the same elements of the input
* array except the element on the specified position. The component
* type of the returned array is always the same as that of the input
* array.
*
* If the input array is <code>null</code>, an IndexOutOfBoundsException
* will be thrown, because in that case no valid index can be specified.
*
* @param array the array to remove the element from, may not be <code>null</code>
* @param index the position of the element to be removed
* @return A new array containing the existing elements except the element
* at the specified position.
* @throws IndexOutOfBoundsException if the index is out of range
* (index < 0 || index >= array.length), or if the array is <code>null</code>.
* @since 2.1
*/
private static Object remove(Object array, int index) {
int length = getLength(array);
if (index < 0 || index >= length) {
throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
}
Object result = Array.newInstance(array.getClass().getComponentType(), length - 1);
System.arraycopy(array, 0, result, 0, index);
if (index < length - 1) {
System.arraycopy(array, index + 1, result, index, length - index - 1);
}
return result;
}
/**
* Finds the index of the given value in the array starting at the given index.
*
* This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.
*
* A negative startIndex is treated as zero. A startIndex larger than the array
* length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).
*
* @param array the array to search through for the object, may be <code>null</code>
* @param valueToFind the value to find
* @param startIndex the index to start searching at
* @return the index of the value within the array,
* {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
*/
public static int indexOf(short[] array, short valueToFind) {
if (array == null) {
return -1;
}
for (int i = 0; i < array.length; i++) {
if (valueToFind == array[i]) {
return i;
}
}
return -1;
}
/**
* Returns the length of the specified array.
* This method can deal with <code>Object</code> arrays and with primitive arrays.
*
* If the input array is <code>null</code>, <code>0</code> is returned.
*
* <pre>
* ArrayUtils.getLength(null) = 0
* ArrayUtils.getLength([]) = 0
* ArrayUtils.getLength([null]) = 1
* ArrayUtils.getLength([true, false]) = 2
* ArrayUtils.getLength([1, 2, 3]) = 3
* ArrayUtils.getLength(["a", "b", "c"]) = 3
* </pre>
*
* @param array the array to retrieve the length from, may be null
* @return The length of the array, or <code>0</code> if the array is <code>null</code>
* @throws IllegalArgumentException if the object arguement is not an array.
* @since 2.1
*/
public static int getLength(Object array) {
if (array == null) {
return 0;
}
return Array.getLength(array);
}
/**
* Shallow clones an array returning a typecast result and handling
* <code>null</code>.
*
* The objects in the array are not cloned, thus there is no special
* handling for multi-dimensional arrays.
*
* This method returns <code>null</code> for a <code>null</code> input array.
*
* @param array the array to shallow clone, may be <code>null</code>
* @return the cloned array, <code>null</code> if <code>null</code> input
*/
public static short[] clone(short[] array) {
if (array == null) {
return null;
}
return (short[]) array.clone();
}
}
Reverses the order of the given long type value array.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
/**
* @author Stephen Colebourne
* @author Moritz Petersen
* @author
* @author Maarten Coene
* @since 2.0
* @version $Id: ArrayUtils.java 632503 2008-03-01 00:21:52Z ggregory $
*/
public class Main {
/**
* Reverses the order of the given array.
*
* This method does nothing for a <code>null</code> input array.
*
* @param array the array to reverse, may be <code>null</code>
*/
public static void reverse(long[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
long tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
}
Turn an array of ints into a printable string.
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
/*
Derby - Class org.apache.derby.iapi.util.PropertyUtil
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to you under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
public class Main {
/**
* Turn an array of ints into a printable string. Returns what"s returned
* in Java 5 by java.util.Arrays.toString(int[]).
*/
public static String stringify( int[] raw )
{
if ( raw == null ) { return "null"; }
StringBuffer buffer = new StringBuffer();
int count = raw.length;
buffer.append( "[ " );
for ( int i = 0; i < count; i++ )
{
if ( i > 0 ) { buffer.append( ", " ); }
buffer.append( raw[ i ] );
}
buffer.append( " ]" );
return buffer.toString();
}
}