当前位置: 首页>>代码示例>>Java>>正文


Java Array.newInstance方法代码示例

本文整理汇总了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);
        }
    };
}
 
开发者ID:Bekreth,项目名称:jetedge,代码行数:19,代码来源:ArrayLimiter.java

示例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;
}
 
开发者ID:funkemunky,项目名称:HCFCore,代码行数:31,代码来源:CompositeCollection.java

示例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;
}
 
开发者ID:typemeta,项目名称:funcj,代码行数:28,代码来源:FunctorsGenEx.java

示例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;
}
 
开发者ID:apache,项目名称:directory-mavibot,代码行数:21,代码来源:InMemoryBTreeTest.java

示例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;
}
 
开发者ID:pooyafaroka,项目名称:PlusGram,代码行数:16,代码来源:AnimatorSetProxy.java

示例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;
}
 
开发者ID:akarnokd,项目名称:RxJava3-preview,代码行数:37,代码来源:ReplaySubject.java

示例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;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:StringListImpl.java

示例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 );
}
 
开发者ID:apache,项目名称:directory-mavibot,代码行数:13,代码来源:AbstractPage.java

示例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;
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:14,代码来源:ArrayUtil.java

示例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;
}
 
开发者ID:austinv11,项目名称:ETF-Java,代码行数:28,代码来源:Mapper.java

示例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;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:29,代码来源:IdentityHashMap.java

示例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;
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:8,代码来源:ArrayWritable.java

示例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);
}
 
开发者ID:mware-solutions,项目名称:memory-graph,代码行数:7,代码来源:IterableUtils.java

示例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;
}
 
开发者ID:apache,项目名称:myfaces-trinidad,代码行数:81,代码来源:CopyOnWriteArrayMap.java

示例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;
}
 
开发者ID:tranngoclam,项目名称:fast-list,代码行数:7,代码来源:SnappySortedList.java


注:本文中的java.lang.reflect.Array.newInstance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。