Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I am learning GoF Java Design Patterns and I want to see some real life examples of them. Can you guys point to some good usage of these Design Patterns.(preferably in Java's core libraries). Thank you
This is a simplified version of the code in question, one generic class uses another class with generic type parameters and needs to pass one of the generic types to a method with varargs parameters: class Assembler<X, Y> { void assemble(X container, Y... args) { ... } } class Component<T> { void useAssembler(T something) { Assembler<String, T> assembler = n...
Can a List<T> be initialized to contain a given number of nulls, where T is a type parameter of the class of which the list is a member? I sure can do it with a loop, but like to know whether it is possible without. List<T> myList = new ArrayList<T>(numEls); creates a list of the given capacity, but size 0, so myList.get(x) fails for all x, and so does, e.g. myList.set(numE...
Ok, I've been google'ing the web and I just can't seem to find any solution to my problem. I found lots of solutions, just not any that fit. I need to create an array of generics. But the generic type itself extends Comparable. When I try the following : public class Hash<T extends Comparable<String>> { private T[] hashTable; private int tableSize; Hash(int records, double...
I have classes that for processing primitive array input: CharArrayExtractor for char[], ByteArrayExtractor for byte[], IntegerArrayExtractor for int[], ... public void CharArrayExtractor { public List<Record> extract(char[] source) { List<Record> records = new ArrayList<Record>(); int recordStartFlagPos = -1; int recordEndFlagPos = -1; f...
Sorry I couldnt think of a more concise title. My question is why does the following piece of code work: public abstract class TObjectPool<T> { protected Object[] availableObjects; TObjectPool(int size){ availableObjects = new Object[size]; } protected class RenderElementPool extends TObjectPool<RenderElement>{ @Override public void fill() { for (int i = 0; i...
I have a hierarchy where Square, Triangle and Circle all extend from Shape. I have a working method: public void someMethod() { File file = new File("File_with_squares"); ThirdPartyClass foo = new ThirdPartyClass(); Square[] squares = foo.someMajicMethod(Square[].class,file); for (Square square: squares) square.draw(); } Now I want to make this method generic so that it c...
Usually the compiler generates code to perform boxing and unboxing. But what does the compiler, if the boxed values are not needed? Is the (Oracle standard) compiler smart enough to optimize it away? Take a look at this method: public static void requireInRange(int index, Object[] array) { if(index < 0 || index >= array.length) throw new IndexOutOfBoundsException(); } The ...
Basically the title says it all: Does anybody know why Java 1.6 has this behaviour: List<String> list = ArrayList<String>(); String[] arr = (String[]) list.toArray(); And I get a ClassCastException, because it returns Object[] and not String[]. I thought List<T>.toArray() should return T[] - no? Does anyone have an answer why this inconvenience exists in the language? ...
I have a Java object with some unknown structure. Now I want to output this structure (properties and their values) to log file. And of course I'm interested to do this in recursive mode. Are there any libraries that can help me?
Here's a copy of the interpreter commands: scala>val myTable = Array(Array(1)) res30: Array[Array[Int]] = Array(Array(1)) scala> myTable.map(_.map(_.toString)) res31: Array[Array[java.lang.String]] = Array(Array(1)) scala> var result = 0 result: Int = 0 scala> myTable.head res32: Array[Int] = Array(1) //note how applying this works scala> res32.map(elem => if(3> result...
I would like to take a passed List that I know is homogeneous and from it create an array of the same type as the elements within it. Something like... List<Object> lst = new ArrayList<Object>; lst.add(new Integer(3)); /// somewhere else ... assert(my_array instanceof Integer[]);
I need to be able to have an n-dimensional field where n is based on an input to the constructor. But I'm not even sure if that's possible. Is it?
Java Class java.lang.reflect.Array provides a set of tools for creating an array dynamically. However in addition to that it has a whole set of methods for accessing (get, set, and length) an array. I don't understand the point of this, since you can (and presumably would) cast your dynamically generated array as an array upon creation, which means you can use the normal array access (bracket...
In Java, do primitive types and arrays have a containing package? Probably not, but just want to be certain.
I'm extremely new to java. I'm currently learning by book and have stumbled across the following problem. I noticed that for example when learning about arrays they mention that you can call "arrayname.length" to get the length of that array. I'm trying to look in the online API documentation to see what public variables/methods is available. The documentation shows a lot of methods but nowher...
For reasons that I'd rather not go into, I have a bunch of parallel arrays and I'm curious what the most elegant way to go about making sure that they are all the same length would be. (if one of them is the wrong length then there must be data missing and the arrays won't align properly). I don't like the idea of doing... if(array1.length != array2.length || array1.length != array3.length et...
I am trying to make a Graph class with generic vertices and adjacency lists and I am getting a generic array creation error at line 10 of the following code. Earlier I was getting the same error at line 11 but it was solved by casting. But this error remains. What is wrong in the code? import java.util.HashSet; public class Graph<T> { private int numVertices; private T[] vertex...
I have a method that uses the varargs feature: void add(Animal ...); Now, instead of doing .add(dog, cat), I have an Animal list with unknown number of elements, List<Animal> i = new ArrayList<Animal>(); i.add(dog); i.add(cat); and want to call add with the elements of this list. I think I could use an array, but when I do .add(i.toArray()), it gives a compiler error. What i...
I want a function / data structure that can do this: func(int dim){ if(dim == 1) int[] array; else if (dim == 2) int[][] array; else if (dim == 3) int[][][] array; .. .. . } anyone know how?
Which is the best practice in this situation? I would like an un-initialized array of the same type and length as the original. public static <AnyType extends Comparable<? super AnyType>> void someFunction(AnyType[] someArray) { AnyType[] anotherArray = (AnyType[]) new Comparable[someArray.length]; ...or... AnyType[] anotherArray = (AnyType[]) new Object[someArray.le...
  /*
   * Copyright 1996-2006 Sun Microsystems, Inc.  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.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 package java.lang.reflect;

The Array class provides static methods to dynamically create and access Java arrays.

Array permits widening conversions to occur during a get or set operation, but throws an IllegalArgumentException if a narrowing conversion would occur.

Author(s):
Nakul Saraiya
 
 public final
 class Array {

    
Constructor. Class Array is not instantiable.
 
     private Array() {}

    
Creates a new array with the specified component type and length. Invoking this method is equivalent to creating an array as follows:
 int[] x = {length};
 Array.newInstance(componentType, x);
 

Parameters:
componentType the Class object representing the component type of the new array
length the length of the new array
Returns:
the new array
Throws:
java.lang.NullPointerException if the specified componentType parameter is null
java.lang.IllegalArgumentException if componentType is java.lang.Void.TYPE
java.lang.NegativeArraySizeException if the specified length is negative
 
     public static Object newInstance(Class<?> componentTypeint length)
         throws NegativeArraySizeException {
         return newArray(componentTypelength);
     }

    
Creates a new array with the specified component type and dimensions. If componentType represents a non-array class or interface, the new array has dimensions.length dimensions and componentType as its component type. If componentType represents an array class, the number of dimensions of the new array is equal to the sum of dimensions.length and the number of dimensions of componentType. In this case, the component type of the new array is the component type of componentType.

The number of dimensions of the new array must not exceed the number of array dimensions supported by the implementation (typically 255).

Parameters:
componentType the Class object representing the component type of the new array
dimensions an array of int representing the dimensions of the new array
Returns:
the new array
Throws:
java.lang.NullPointerException if the specified componentType argument is null
java.lang.IllegalArgumentException if the specified dimensions argument is a zero-dimensional array, or if the number of requested dimensions exceeds the limit on the number of array dimensions supported by the implementation (typically 255), or if componentType is java.lang.Void.TYPE.
java.lang.NegativeArraySizeException if any of the components in the specified dimensions argument is negative.
    public static Object newInstance(Class<?> componentTypeint... dimensions)
        return multiNewArray(componentTypedimensions);
    }

    
Returns the length of the specified array object, as an int.

Parameters:
array the array
Returns:
the length of the array
Throws:
java.lang.IllegalArgumentException if the object argument is not an array
    public static native int getLength(Object array)
        throws IllegalArgumentException;

    
Returns the value of the indexed component in the specified array object. The value is automatically wrapped in an object if it has a primitive type.

Parameters:
array the array
index the index
Returns:
the (possibly wrapped) value of the indexed component in the specified array
Throws:
java.lang.NullPointerException If the specified object is null
java.lang.IllegalArgumentException If the specified object is not an array
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
    public static native Object get(Object arrayint index)
Returns the value of the indexed component in the specified array object, as a boolean.

Parameters:
array the array
index the index
Returns:
the value of the indexed component in the specified array
Throws:
java.lang.NullPointerException If the specified object is null
java.lang.IllegalArgumentException If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
See also:
get(java.lang.Object,int)
    public static native boolean getBoolean(Object arrayint index)
Returns the value of the indexed component in the specified array object, as a byte.

Parameters:
array the array
index the index
Returns:
the value of the indexed component in the specified array
Throws:
java.lang.NullPointerException If the specified object is null
java.lang.IllegalArgumentException If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
See also:
get(java.lang.Object,int)
    public static native byte getByte(Object arrayint index)
Returns the value of the indexed component in the specified array object, as a char.

Parameters:
array the array
index the index
Returns:
the value of the indexed component in the specified array
Throws:
java.lang.NullPointerException If the specified object is null
java.lang.IllegalArgumentException If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
See also:
get(java.lang.Object,int)
    public static native char getChar(Object arrayint index)
Returns the value of the indexed component in the specified array object, as a short.

Parameters:
array the array
index the index
Returns:
the value of the indexed component in the specified array
Throws:
java.lang.NullPointerException If the specified object is null
java.lang.IllegalArgumentException If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
See also:
get(java.lang.Object,int)
    public static native short getShort(Object arrayint index)
Returns the value of the indexed component in the specified array object, as an int.

Parameters:
array the array
index the index
Returns:
the value of the indexed component in the specified array
Throws:
java.lang.NullPointerException If the specified object is null
java.lang.IllegalArgumentException If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
See also:
get(java.lang.Object,int)
    public static native int getInt(Object arrayint index)
Returns the value of the indexed component in the specified array object, as a long.

Parameters:
array the array
index the index
Returns:
the value of the indexed component in the specified array
Throws:
java.lang.NullPointerException If the specified object is null
java.lang.IllegalArgumentException If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
See also:
get(java.lang.Object,int)
    public static native long getLong(Object arrayint index)
Returns the value of the indexed component in the specified array object, as a float.

Parameters:
array the array
index the index
Returns:
the value of the indexed component in the specified array
Throws:
java.lang.NullPointerException If the specified object is null
java.lang.IllegalArgumentException If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
See also:
get(java.lang.Object,int)
    public static native float getFloat(Object arrayint index)
Returns the value of the indexed component in the specified array object, as a double.

Parameters:
array the array
index the index
Returns:
the value of the indexed component in the specified array
Throws:
java.lang.NullPointerException If the specified object is null
java.lang.IllegalArgumentException If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
See also:
get(java.lang.Object,int)
    public static native double getDouble(Object arrayint index)
Sets the value of the indexed component of the specified array object to the specified new value. The new value is first automatically unwrapped if the array has a primitive component type.

Parameters:
array the array
index the index into the array
value the new value of the indexed component
Throws:
java.lang.NullPointerException If the specified object argument is null
java.lang.IllegalArgumentException If the specified object argument is not an array, or if the array component type is primitive and an unwrapping conversion fails
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
    public static native void set(Object arrayint indexObject value)
Sets the value of the indexed component of the specified array object to the specified boolean value.

Parameters:
array the array
index the index into the array
z the new value of the indexed component
Throws:
java.lang.NullPointerException If the specified object argument is null
java.lang.IllegalArgumentException If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
See also:
set(java.lang.Object,int,java.lang.Object)
    public static native void setBoolean(Object arrayint indexboolean z)
Sets the value of the indexed component of the specified array object to the specified byte value.

Parameters:
array the array
index the index into the array
b the new value of the indexed component
Throws:
java.lang.NullPointerException If the specified object argument is null
java.lang.IllegalArgumentException If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
See also:
set(java.lang.Object,int,java.lang.Object)
    public static native void setByte(Object arrayint indexbyte b)
Sets the value of the indexed component of the specified array object to the specified char value.

Parameters:
array the array
index the index into the array
c the new value of the indexed component
Throws:
java.lang.NullPointerException If the specified object argument is null
java.lang.IllegalArgumentException If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
See also:
set(java.lang.Object,int,java.lang.Object)
    public static native void setChar(Object arrayint indexchar c)
Sets the value of the indexed component of the specified array object to the specified short value.

Parameters:
array the array
index the index into the array
s the new value of the indexed component
Throws:
java.lang.NullPointerException If the specified object argument is null
java.lang.IllegalArgumentException If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
See also:
set(java.lang.Object,int,java.lang.Object)
    public static native void setShort(Object arrayint indexshort s)
Sets the value of the indexed component of the specified array object to the specified int value.

Parameters:
array the array
index the index into the array
i the new value of the indexed component
Throws:
java.lang.NullPointerException If the specified object argument is null
java.lang.IllegalArgumentException If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
See also:
set(java.lang.Object,int,java.lang.Object)
    public static native void setInt(Object arrayint indexint i)
Sets the value of the indexed component of the specified array object to the specified long value.

Parameters:
array the array
index the index into the array
l the new value of the indexed component
Throws:
java.lang.NullPointerException If the specified object argument is null
java.lang.IllegalArgumentException If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
See also:
set(java.lang.Object,int,java.lang.Object)
    public static native void setLong(Object arrayint indexlong l)
Sets the value of the indexed component of the specified array object to the specified float value.

Parameters:
array the array
index the index into the array
f the new value of the indexed component
Throws:
java.lang.NullPointerException If the specified object argument is null
java.lang.IllegalArgumentException If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
See also:
set(java.lang.Object,int,java.lang.Object)
    public static native void setFloat(Object arrayint indexfloat f)
Sets the value of the indexed component of the specified array object to the specified double value.

Parameters:
array the array
index the index into the array
d the new value of the indexed component
Throws:
java.lang.NullPointerException If the specified object argument is null
java.lang.IllegalArgumentException If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion
java.lang.ArrayIndexOutOfBoundsException If the specified index argument is negative, or if it is greater than or equal to the length of the specified array
See also:
set(java.lang.Object,int,java.lang.Object)
    public static native void setDouble(Object arrayint indexdouble d)
    /*
     * Private
     */
    private static native Object newArray(Class componentTypeint length)
        throws NegativeArraySizeException;
    private static native Object multiNewArray(Class componentType,
        int[] dimensions)
New to GrepCode? Check out our FAQ X