本文整理匯總了Java中java.lang.reflect.Array.newInstance方法的典型用法代碼示例。如果您正苦於以下問題:Java Array.newInstance方法的具體用法?Java Array.newInstance怎麽用?Java Array.newInstance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.reflect.Array
的用法示例。
在下文中一共展示了Array.newInstance方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: generateSupplier
import java.lang.reflect.Array; //導入方法依賴的package包/類
@Override
public Supplier<Object[]> generateSupplier(Random random) {
if (this.limiter == null) {
throw new LimiterConstructionException("Error creating ArrayLimiter : Missing internal Limiter");
}
return () -> {
try {
int count = range == 0 ? offset : random.nextInt(range) + offset;
Object[] objectArray = (Object[]) Array.newInstance(limiter.generateSupplier(random).get().getClass(), count);
for (int i = 0; i < count; i++) {
objectArray[i] = limiter.generateSupplier(random).get();
}
return objectArray;
} catch (Exception e) {
throw new PojoConstructionException("Failed to create object for ArrayLimiter: ", e);
}
};
}
示例2: toArray
import java.lang.reflect.Array; //導入方法依賴的package包/類
/**
* Returns an object array, populating the supplied array if possible.
* See <code>Collection</code> interface for full details.
*
* @param <T> the type of the elements in the collection
* @param array the array to use, populating if possible
* @return an array of all the elements in the collection
*/
@Override
@SuppressWarnings("unchecked")
public <T> T[] toArray(final T[] array) {
final int size = size();
Object[] result = null;
if (array.length >= size) {
result = array;
} else {
result = (Object[]) Array.newInstance(array.getClass().getComponentType(), size);
}
int offset = 0;
for (final Collection<E> item : all) {
for (final E e : item) {
result[offset++] = e;
}
}
if (result.length > size) {
result[size] = null;
}
return (T[]) result;
}
示例3: map
import java.lang.reflect.Array; //導入方法依賴的package包/類
/**
* Map a function over an array.
* @param f the function to apply to each element of the array
* @param from the input array
* @param to the output array
* @param <A> the element type of the input array
* @param <B> the element type of the output array
* @param <X> the exception type
* @return the output array
* @throws X the exception thrown by the function
*/
@SuppressWarnings("unchecked")
public static <A, B, X extends Exception> B[] map(F<A, B, X> f, A[] from, B[] to) throws X {
final int l = from.length;
if (to.length != l) {
final Class<?> type = to.getClass();
to = (type == Object[].class)
? (B[]) new Object[l]
: (B[]) Array.newInstance(type.getComponentType(), l);
}
for (int i = 0; i < from.length; ++i) {
to[i] = f.apply(from[i]);
}
return to;
}
示例4: createLeaf
import java.lang.reflect.Array; //導入方法依賴的package包/類
private Page<Integer, String> createLeaf( BTree<Integer, String> btree, long revision,
Tuple<Integer, String>... tuples )
{
InMemoryLeaf<Integer, String> leaf = new InMemoryLeaf<Integer, String>( btree );
int pos = 0;
leaf.setRevision( revision );
leaf.setNbElems( tuples.length );
leaf.setKeys( new KeyHolder[leaf.getNbElems()] );
leaf.values = ( InMemoryValueHolder<String>[] ) Array
.newInstance( InMemoryValueHolder.class, leaf.getNbElems() );
for ( Tuple<Integer, String> tuple : tuples )
{
leaf.setKey( pos, new KeyHolder<Integer>( tuple.getKey() ) );
leaf.values[pos] = new InMemoryValueHolder<String>( btree, tuple.getValue() );
pos++;
}
return leaf;
}
示例5: copyOfRange
import java.lang.reflect.Array; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public static <T, U> T[] copyOfRange(U[] original, int start, int end, Class<? extends T[]> newType) {
if (start > end) {
throw new IllegalArgumentException();
}
int originalLength = original.length;
if (start < 0 || start > originalLength) {
throw new ArrayIndexOutOfBoundsException();
}
int resultLength = end - start;
int copyLength = Math.min(resultLength, originalLength - start);
T[] result = (T[]) Array.newInstance(newType.getComponentType(), resultLength);
System.arraycopy(original, start, result, 0, copyLength);
return result;
}
示例6: getValues
import java.lang.reflect.Array; //導入方法依賴的package包/類
@Override
@SuppressWarnings("unchecked")
public T[] getValues(T[] array) {
int s = size;
if (s == 0) {
if (array.length != 0) {
array[0] = null;
}
return array;
}
List<Object> b = buffer;
Object o = b.get(s - 1);
if (NotificationLite.isComplete(o) || NotificationLite.isError(o)) {
s--;
if (s == 0) {
if (array.length != 0) {
array[0] = null;
}
return array;
}
}
if (array.length < s) {
array = (T[])Array.newInstance(array.getClass().getComponentType(), s);
}
for (int i = 0; i < s; i++) {
array[i] = (T)b.get(i);
}
if (array.length > s) {
array[s] = null;
}
return array;
}
示例7: toArray
import java.lang.reflect.Array; //導入方法依賴的package包/類
public Object[] toArray(Object[] a) {
if (fVector != null) {
return fVector.toArray(a);
}
if (a.length < fLength) {
Class arrayClass = a.getClass();
Class componentType = arrayClass.getComponentType();
a = (Object[]) Array.newInstance(componentType, fLength);
}
toArray0(a);
if (a.length > fLength) {
a[fLength] = null;
}
return a;
}
示例8: AbstractPage
import java.lang.reflect.Array; //導入方法依賴的package包/類
/**
* Internal constructor used to create Page instance used when a page is being copied or overflow
*/
@SuppressWarnings("unchecked")
// Cannot create an array of generic objects
protected AbstractPage( BTree<K, V> btree, long revision, int nbElems )
{
this.btree = btree;
this.revision = revision;
this.nbElems = nbElems;
this.keys = ( KeyHolder[] ) Array.newInstance( KeyHolder.class, nbElems );
}
示例9: duplicateArray
import java.lang.reflect.Array; //導入方法依賴的package包/類
/**
* Returns a duplicates of an array.
*/
public static Object duplicateArray(Object source) {
int size = Array.getLength(source);
Object newarray =
Array.newInstance(source.getClass().getComponentType(), size);
System.arraycopy(source, 0, newarray, 0, size);
return newarray;
}
示例10: read
import java.lang.reflect.Array; //導入方法依賴的package包/類
public <T> T read(ErlangMap data, Class<T> clazz) {
T instance = ReflectionUtils.createInstance(clazz);
List<PropertyManager> properties = ReflectionUtils.findProperties(instance, clazz);
for (PropertyManager property : properties) {
if (data.containsKey(property.getName())) {
Object obj = data.get(property.getName());
if (obj instanceof ErlangMap) {
obj = read((ErlangMap) obj, property.getSetterType());
} else if (obj instanceof ErlangList && property.getSetterType().isArray()) {
if (((ErlangList) obj).size() > 0) {
Object array = Array.newInstance(property.getSetterType().getComponentType(), ((ErlangList) obj).size());
for (int i = 0; i < ((ErlangList) obj).size(); i++) {
Object obj1 = ((ErlangList) obj).get(i);
if (obj1 != null)
Array.set(array, i, obj1 instanceof ErlangMap ? read((ErlangMap) obj1, property.getSetterType().getComponentType()) : obj1);
}
obj = array;
} else
obj = Array.newInstance(property.getSetterType().getComponentType(), 0);
} else if (obj == null && property.getSetterType().isArray()) {
obj = Array.newInstance(property.getSetterType().getComponentType(), 0);
}
property.setValue(obj);
}
}
return instance;
}
示例11: toArray
import java.lang.reflect.Array; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
int expectedModCount = modCount;
int size = size();
if (a.length < size)
a = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
Object[] tab = table;
int ti = 0;
for (int si = 0; si < tab.length; si += 2) {
Object key;
if ((key = tab[si]) != null) { // key present ?
// more elements than expected -> concurrent modification from other thread
if (ti >= size) {
throw new ConcurrentModificationException();
}
a[ti++] = (T) new AbstractMap.SimpleEntry<>(unmaskNull(key), tab[si + 1]);
}
}
// fewer elements than expected or concurrent modification from other thread detected
if (ti < size || expectedModCount != modCount) {
throw new ConcurrentModificationException();
}
// final null marker as per spec
if (ti < a.length) {
a[ti] = null;
}
return a;
}
示例12: toArray
import java.lang.reflect.Array; //導入方法依賴的package包/類
public Object toArray() {
Object result = Array.newInstance(valueClass, values.length);
for (int i = 0; i < values.length; i++) {
Array.set(result, i, values[i]);
}
return result;
}
示例13: toArray
import java.lang.reflect.Array; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type) {
List<? extends T> list = toList(iterable);
T[] array = (T[]) Array.newInstance(type, list.size());
return list.toArray(array);
}
示例14: _addEntryAtIndex
import java.lang.reflect.Array; //導入方法依賴的package包/類
private static <K,V> ConcurrentEntry<K,V>[] _addEntryAtIndex(
ConcurrentEntry<K,V>[] entries, ConcurrentEntry<K,V> entry, int insertIndex, int removeIndex)
{
int originalSize = entries.length;
int newSize = originalSize;
// if we haven't hit the LRU limit, increment the size
if (removeIndex < 0)
newSize++;
@SuppressWarnings("unchecked")
ConcurrentEntry<K,V>[] newEntries = (ConcurrentEntry<K,V>[])
Array.newInstance(entry.getClass(), newSize);
if (removeIndex >= 0)
{
if (removeIndex == insertIndex)
{
// inserting into same spot we removed, so just copy array
System.arraycopy(entries, 0, newEntries, 0, originalSize);
}
else
{
if (removeIndex < insertIndex)
{
// copy everything before the removeIndex
System.arraycopy(entries, 0, newEntries, 0, removeIndex);
// copy everything between the removeIndex and the insertIndex, shifting things down
System.arraycopy(entries, removeIndex + 1, newEntries, removeIndex, insertIndex - removeIndex - 1);
// copy everything from the entry index to the end
if (insertIndex < originalSize)
{
System.arraycopy(entries, insertIndex, newEntries, insertIndex, originalSize - insertIndex);
}
// we removed the entry before the insertion location, so decrement to account for this
insertIndex--;
}
else
{
// copy everything before the insertIndex
System.arraycopy(entries, 0, newEntries, 0, insertIndex);
// copy everything between the insertIndex and the removeIndex, shifting things up
System.arraycopy(entries, insertIndex, newEntries, insertIndex + 1, removeIndex - insertIndex);
int afterRemoveIndex = removeIndex + 1;
// copy everthing after the removeIndex
if (afterRemoveIndex < originalSize)
{
System.arraycopy(entries, afterRemoveIndex, newEntries, afterRemoveIndex, originalSize - afterRemoveIndex);
}
}
}
}
else
{
if ((insertIndex == 0) || (insertIndex == originalSize))
{
int destStart = (insertIndex == 0) ? 1 : 0;
System.arraycopy(entries, 0, newEntries, destStart, originalSize);
}
else
{
// copy everything before the insertIndex
System.arraycopy(entries, 0, newEntries, 0, insertIndex);
// copy everything after the insertIndex
System.arraycopy(entries, insertIndex, newEntries, insertIndex + 1, originalSize - insertIndex);
}
}
newEntries[insertIndex] = entry;
return newEntries;
}
示例15: SnappySortedList
import java.lang.reflect.Array; //導入方法依賴的package包/類
public SnappySortedList(Class<T> klass, RxSortedListCallback<T> callback, int initialCapacity) {
mTClass = klass;
mData = (T[]) Array.newInstance(klass, initialCapacity);
mCallback = callback;
mSize = 0;
}