java.util.ArrayList.toArray()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(7.0k)|赞(0)|评价(0)|浏览(390)

本文整理了Java中java.util.ArrayList.toArray()方法的一些代码示例,展示了ArrayList.toArray()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ArrayList.toArray()方法的具体详情如下:
包路径:java.util.ArrayList
类名称:ArrayList
方法名:toArray

ArrayList.toArray介绍

[英]Returns a new array containing all elements contained in this ArrayList.
[中]返回包含此ArrayList中包含的所有元素的新数组。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Marshal the elements from the given enumeration into an array of the given type.
 * Enumeration elements must be assignable to the type of the given array. The array
 * returned will be a different instance than the array given.
 */
public static <A, E extends A> A[] toArray(Enumeration<E> enumeration, A[] array) {
  ArrayList<A> elements = new ArrayList<>();
  while (enumeration.hasMoreElements()) {
    elements.add(enumeration.nextElement());
  }
  return elements.toArray(array);
}

代码示例来源:origin: apache/flink

@SuppressWarnings("unchecked")
public Class<? extends Comparable<?>>[] getTypes() {
  return this.types.toArray(new Class[this.types.size()]);
}

代码示例来源:origin: redisson/redisson

/**
 * Returns exception chain starting from top up to root cause.
 */
public static Throwable[] getExceptionChain(Throwable throwable) {
  ArrayList<Throwable> list = new ArrayList<>();
  list.add(throwable);
  while ((throwable = throwable.getCause()) != null) {
    list.add(throwable);
  }
  Throwable[] result = new Throwable[list.size()];
  return list.toArray(result);
}

代码示例来源:origin: stanfordnlp/CoreNLP

@Override
public Object[] toArray() {
 Iterator<E> iter = iterator();
 ArrayList<Object> al = new ArrayList<>();
 while (iter.hasNext()) {
  al.add(iter.next());
 }
 return al.toArray();
}

代码示例来源:origin: redisson/redisson

public CtField[] getFields() {
  ArrayList alist = new ArrayList();
  getFields(alist, this);
  return (CtField[])alist.toArray(new CtField[alist.size()]);
}

代码示例来源:origin: spring-projects/spring-framework

@Test // SPR-14988
public void getMappingOverridesConsumesFromTypeLevelAnnotation() throws Exception {
  RequestMappingInfo requestMappingInfo = assertComposedAnnotationMapping(RequestMethod.GET);
  assertArrayEquals(new MediaType[]{MediaType.ALL}, new ArrayList<>(
      requestMappingInfo.getConsumesCondition().getConsumableMediaTypes()).toArray());
}

代码示例来源:origin: org.apache.lucene/lucene-core

/** Builds a {@link MultiPhraseQuery}. */
 public MultiPhraseQuery build() {
  int[] positionsArray = new int[this.positions.size()];
  for (int i = 0; i < this.positions.size(); ++i) {
   positionsArray[i] = this.positions.get(i);
  }
  Term[][] termArraysArray = termArrays.toArray(new Term[termArrays.size()][]);
  return new MultiPhraseQuery(field, termArraysArray, positionsArray, slop);
 }
}

代码示例来源:origin: apache/flink

@Override
  public void reduce(Iterable<Tuple2<Long, Long>> values, Collector<Tuple2<Long, Long[]>> out) {
    neighbors.clear();
    Long id = 0L;
    for (Tuple2<Long, Long> n : values) {
      id = n.f0;
      neighbors.add(n.f1);
    }
    out.collect(new Tuple2<Long, Long[]>(id, neighbors.toArray(new Long[neighbors.size()])));
  }
}

代码示例来源:origin: prestodb/presto

public static <T> Collector<T, ?, Object[][]> toDataProvider()
  {
    return Collector.of(
        ArrayList::new,
        (builder, entry) -> builder.add(new Object[] {entry}),
        (left, right) -> {
          left.addAll(right);
          return left;
        },
        builder -> builder.toArray(new Object[][] {}));
  }
}

代码示例来源:origin: apache/activemq

public Message[] getMessages() {
  ArrayList<Object> list = new ArrayList<Object>();
  for (Iterator<TxOperation> iter = operations.iterator(); iter.hasNext();) {
    TxOperation op = iter.next();
    if (op.operationType == TxOperation.ADD_OPERATION_TYPE) {
      list.add(op.data);
    }
  }
  Message rc[] = new Message[list.size()];
  list.toArray(rc);
  return rc;
}

代码示例来源:origin: stackoverflow.com

ArrayList<Integer> foo = new ArrayList<Integer>();
foo.add(1);
foo.add(1);
foo.add(2);
foo.add(3);
foo.add(5);
Integer[] bar = foo.toArray(new Integer[foo.size()]);
System.out.println("bar.length = " + bar.length);

代码示例来源:origin: neo4j/neo4j

private AnyValue[] iterationAsArray()
{
  ArrayList<AnyValue> values = new ArrayList<>();
  int size = 0;
  for ( AnyValue value : this )
  {
    values.add( value );
    size++;
  }
  return values.toArray( new AnyValue[size] );
}

代码示例来源:origin: apache/flink

public static void compareResultsByLinesInMemoryWithStrictOrder(String expectedResultStr,
                                String resultPath, String[] excludePrefixes) throws Exception {
  ArrayList<String> list = new ArrayList<>();
  readAllResultLines(list, resultPath, excludePrefixes, true);
  String[] result = list.toArray(new String[list.size()]);
  String[] expected = expectedResultStr.split("\n");
  Assert.assertEquals("Different number of lines in expected and obtained result.", expected.length, result.length);
  Assert.assertArrayEquals(expected, result);
}

代码示例来源:origin: spring-projects/spring-framework

@Test // SPR-14988
public void getMappingOverridesConsumesFromTypeLevelAnnotation() throws Exception {
  RequestMappingInfo requestMappingInfo = assertComposedAnnotationMapping(RequestMethod.GET);
  assertArrayEquals(new MediaType[]{MediaType.ALL}, new ArrayList<>(
      requestMappingInfo.getConsumesCondition().getConsumableMediaTypes()).toArray());
}

代码示例来源:origin: wildfly/wildfly

private static InterceptorList ofList(final ArrayList<EJBClientInterceptorInformation> value) {
  if (value.isEmpty()) {
    return EMPTY;
  } else if (value.size() == 1) {
    return value.get(0).getSingletonList();
  } else {
    return new InterceptorList(value.toArray(EJBClientInterceptorInformation.NO_INTERCEPTORS));
  }
}

代码示例来源:origin: apache/flink

@Override
  public void reduce(Iterable<Tuple2<Long, Long>> values, Collector<Tuple2<Long, Long[]>> out) {
    neighbors.clear();
    Long id = 0L;
    for (Tuple2<Long, Long> n : values) {
      id = n.f0;
      neighbors.add(n.f1);
    }
    out.collect(new Tuple2<Long, Long[]>(id, neighbors.toArray(new Long[neighbors.size()])));
  }
}

代码示例来源:origin: google/ExoPlayer

/**
 * Returns the {@link DataSpec} instances passed to {@link #open(DataSpec)} since the last call to
 * this method.
 */
public final DataSpec[] getAndClearOpenedDataSpecs() {
 DataSpec[] dataSpecs = new DataSpec[openedDataSpecs.size()];
 openedDataSpecs.toArray(dataSpecs);
 openedDataSpecs.clear();
 return dataSpecs;
}

代码示例来源:origin: apache/activemq

public MessageAck[] getAcks() {
  ArrayList<Object> list = new ArrayList<Object>();
  for (Iterator<TxOperation> iter = operations.iterator(); iter.hasNext();) {
    TxOperation op = iter.next();
    if (op.operationType == TxOperation.REMOVE_OPERATION_TYPE) {
      list.add(op.data);
    }
  }
  MessageAck rc[] = new MessageAck[list.size()];
  list.toArray(rc);
  return rc;
}

代码示例来源:origin: libgdx/libgdx

/** @return an array containing all the public fields of this class and its super classes. See {@link Class#getFields()}. */
public Field[] getFields () {
  if (allFields == null) {
    ArrayList<Field> allFieldsList = new ArrayList<Field>();
    Type t = this;
    while (t != null) {
      for (Field f : t.fields) {
        if (f.isPublic) allFieldsList.add(f);
      }
      t = t.getSuperclass();
    }
    allFields = allFieldsList.toArray(new Field[allFieldsList.size()]);
  }
  return allFields;
}

代码示例来源:origin: gocd/gocd

public GoTfsWorkspace[] queryWorkspaces(String workspaceName, String userName) {
  ArrayList<GoTfsWorkspace> goTfsWorkspaces = new ArrayList<>();
  Workspace[] workspaces = client.queryWorkspaces(workspaceName, userName, null);
  for (Workspace workspace : workspaces) {
    goTfsWorkspaces.add(new GoTfsWorkspace(workspace));
  }
  return goTfsWorkspaces.toArray(new GoTfsWorkspace[]{});
}

相关文章