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

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

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

ArrayList.add介绍

[英]Inserts the specified object into this ArrayList at the specified location. The object is inserted before any previous element at the specified location. If the location is equal to the size of this ArrayList, the object is added at the end.
[中]在指定位置将指定对象插入此ArrayList。对象将插入到指定位置的任何上一个元素之前。如果位置等于此ArrayList的大小,则会在末尾添加对象。

代码示例

canonical example by Tabnine

private void usingArrayList() {
 ArrayList<String> list = new ArrayList<>(Arrays.asList("cat", "cow", "dog"));
 list.add("fish");
 int size = list.size(); // size = 4
 list.set(size - 1, "horse"); // replacing the last element to "horse"
 String removed = list.remove(1); // removed = "cow"
 String second = list.get(1); // second = "dog"
}

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

ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");

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

ArrayList aList = new ArrayList();
//Add elements to ArrayList object
aList.add("1");
aList.add("2");
aList.add("3");
aList.add("4");
aList.add("5");
Collections.reverse(aList);
System.out.println("After Reverse Order, ArrayList Contains : " + aList);

代码示例来源:origin: apache/incubator-dubbo

/**
 * Adds a list/map reference.
 */
@Override
public int addRef(Object ref) {
  if (_refs == null)
    _refs = new ArrayList();
  _refs.add(ref);
  return _refs.size() - 1;
}

代码示例来源: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: stackoverflow.com

class Test1 {
 public static void main(String[] s) {
  long st = System.currentTimeMillis();

  List<String> l0 = new ArrayList<String>() {{
   add("Hello");
   add("World!");
  }};

  List<String> l1 = new ArrayList<String>() {{
   add("Hello");
   add("World!");
  }};

  /* snip */

  List<String> l999 = new ArrayList<String>() {{
   add("Hello");
   add("World!");
  }};

  System.out.println(System.currentTimeMillis() - st);
 }
}

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

/**
 * Increases the result vector component at the specified position by the specified delta.
 */
private void updateResultVector(int position, int delta) {
  // inflate the vector to contain the given position
  while (this.resultVector.size() <= position) {
    this.resultVector.add(0);
  }
  // increment the component value
  final int component = this.resultVector.get(position);
  this.resultVector.set(position, component + delta);
}

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

@Test
public void testGenericListOfArrays() throws MalformedURLException {
  GenericBean<String> gb = new GenericBean<>();
  ArrayList<String[]> list = new ArrayList<>();
  list.add(new String[] {"str1", "str2"});
  gb.setListOfArrays(list);
  BeanWrapper bw = new BeanWrapperImpl(gb);
  bw.setPropertyValue("listOfArrays[0][1]", "str3 ");
  assertEquals("str3 ", bw.getPropertyValue("listOfArrays[0][1]"));
  assertEquals("str3 ", gb.getListOfArrays().get(0)[1]);
}

代码示例来源: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: libgdx/libgdx

/**
 * Adds a body to the group
 * 
 * @param argBody
 */
public void addBody(Body argBody) {
 bodies.add(argBody);
 if (bodies.size() == 1) {
  bodyA = argBody;
 }
 if (bodies.size() == 2) {
  bodyB = argBody;
 }
}

代码示例来源: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: 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: google/guava

static <T> List<T> defensiveCopy(Iterable<T> iterable) {
  ArrayList<T> list = new ArrayList<T>();
  for (T element : iterable) {
   list.add(checkNotNull(element));
  }
  return list;
 }
}

代码示例来源:origin: apache/incubator-dubbo

/**
 * Adds a list/map reference.
 */
@Override
public int addRef(Object ref) {
  if (_refs == null)
    _refs = new ArrayList();
  _refs.add(ref);
  return _refs.size() - 1;
}

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

ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");

String listString = "";

for (String s : list)
{
  listString += s + "\t";
}

System.out.println(listString);

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void accept(String s) {
    System.out.println(s);
    list.add(s);
  }
});

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

private static void addLdcW(CodeAttribute.LdcEntry ldcs, ArrayList jumps) {
  int where = ldcs.where;
  LdcW ldcw = new LdcW(where, ldcs.index);
  int s = jumps.size();
  for (int i = 0; i < s; i++)
    if (where < ((Branch)jumps.get(i)).orgPos) {
      jumps.add(i, ldcw);
      return;
    }
  jumps.add(ldcw);
}

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

@Test
public void testGenericListOfArraysWithElementConversion() throws MalformedURLException {
  GenericBean<String> gb = new GenericBean<>();
  ArrayList<String[]> list = new ArrayList<>();
  list.add(new String[] {"str1", "str2"});
  gb.setListOfArrays(list);
  BeanWrapper bw = new BeanWrapperImpl(gb);
  bw.registerCustomEditor(String.class, new StringTrimmerEditor(false));
  bw.setPropertyValue("listOfArrays[0][1]", "str3 ");
  assertEquals("str3", bw.getPropertyValue("listOfArrays[0][1]"));
  assertEquals("str3", gb.getListOfArrays().get(0)[1]);
}

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

@Override
protected void verifyResultsWhenReScaling(ListSink sink, int startElementCounter, int endElementCounter) throws Exception {
  ArrayList<Integer> list = new ArrayList<>();
  for (int i = startElementCounter; i <= endElementCounter; i++) {
    list.add(i);
  }
  Collections.sort(sink.values);
  Assert.assertArrayEquals(list.toArray(), sink.values.toArray());
}

相关文章