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

x33g5p2x  于2022-01-16 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(208)

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

List.add介绍

[英]Inserts the specified object into this List at the specified location. The object is inserted before the current element at the specified location. If the location is equal to the size of this List, the object is added at the end. If the location is smaller than the size of this List, then all elements beyond the specified location are moved by one position towards the end of the List.
[中]在指定位置将指定对象插入此列表。将在指定位置的当前元素之前插入对象。如果位置等于此列表的大小,则对象将添加到末尾。如果该位置小于此列表的大小,则指定位置之外的所有元素都将向列表末尾移动一个位置。

代码示例

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

List<String> list = new ArrayList<String>();
//add some stuff
list.add("android");
list.add("apple");
String[] stringArray = list.toArray(new String[0]);

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

List<String> stockList = new ArrayList<String>();
stockList.add("stock1");
stockList.add("stock2");

String[] stockArr = new String[stockList.size()];
stockArr = stockList.toArray(stockArr);

for(String s : stockArr)
  System.out.println(s);

代码示例来源:origin: square/okhttp

@Override public List<InetAddress> lookup(String hostname) throws UnknownHostException {
 requestedHosts.add(hostname);
 List<InetAddress> result = hostAddresses.get(hostname);
 if (result != null) return result;
 throw new UnknownHostException();
}

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

List<String> strings = new ArrayList<String>()
strings.add("lol");
strings.add("cat");

Collections.sort(strings);
for (String s : strings) {
  System.out.println(s);
}
// Prints out "cat" and "lol"

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

@Test
public void startWithIterable() {
  List<String> li = new ArrayList<String>();
  li.add("alpha");
  li.add("beta");
  List<String> values = Observable.just("one", "two").startWith(li).toList().blockingGet();
  assertEquals("alpha", values.get(0));
  assertEquals("beta", values.get(1));
  assertEquals("one", values.get(2));
  assertEquals("two", values.get(3));
}

代码示例来源:origin: jenkinsci/jenkins

public RenderOnDemandClosure(JellyContext context, String attributesToCapture) {
  List<Script> bodyStack = new ArrayList<Script>();
  for (JellyContext c = context; c!=null; c=c.getParent()) {
    Script script = (Script) c.getVariables().get("org.apache.commons.jelly.body");
    if(script!=null) bodyStack.add(script);
  }
  this.bodyStack = bodyStack.toArray(new Script[bodyStack.size()]);
  assert !bodyStack.isEmpty();    // there must be at least one, which is the direct child of <l:renderOnDemand>
  Map<String,Object> variables = new HashMap<String, Object>();
  for (String v : Util.fixNull(attributesToCapture).split(","))
    variables.put(v.intern(),context.getVariable(v));
  // capture the current base of context for descriptors
  currentDescriptorByNameUrl = Descriptor.getCurrentDescriptorByNameUrl();
  this.variables = PackedMap.of(variables);
  Set<String> _adjuncts = AdjunctsInPage.get().getIncluded();
  this.adjuncts = new String[_adjuncts.size()];
  int i = 0;
  for (String adjunct : _adjuncts) {
    this.adjuncts[i++] = adjunct.intern();
  }
}

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

@SuppressWarnings("unchecked")
IEEE() {
  officers.put("president",pupin);
  List linv = new ArrayList();
  linv.add(tesla);
  officers.put("advisors",linv);
  Members2.add(tesla);
  Members2.add(pupin);
  reverse.add(officers);
}

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

@Override
public ResponseSpec onStatus(Predicate<HttpStatus> statusPredicate,
    Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) {
  if (this.statusHandlers.size() == 1 && this.statusHandlers.get(0) == DEFAULT_STATUS_HANDLER) {
    this.statusHandlers.clear();
  }
  this.statusHandlers.add(new StatusHandler(statusPredicate,
      (clientResponse, request) -> exceptionFunction.apply(clientResponse)));
  return this;
}

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

@Override
public V put(K key, V value) {
  list.remove(key);
  V v;
  if (maxSize > 0 && list.size() == maxSize) {
    //remove first
    K k = list.get(0);
    list.remove(0);
    v = map.remove(k);
  } else {
    v = null;
  }
  list.add(key);
  V result = map.put(key, value);
  if (v != null) {
    try {
      evictedListener.accept(v);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  return result;
}

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

private void processInsert(Row row) {
  // limit the materialized table
  if (materializedTable.size() - validRowPosition >= maxRowCount) {
    cleanUp();
  }
  materializedTable.add(row);
  rowPositionCache.put(row, materializedTable.size() - 1);
}

代码示例来源:origin: square/okhttp

/**
 * Removes a path segment. When this method returns the last segment is always "", which means
 * the encoded path will have a trailing '/'.
 *
 * <p>Popping "/a/b/c/" yields "/a/b/". In this case the list of path segments goes from ["a",
 * "b", "c", ""] to ["a", "b", ""].
 *
 * <p>Popping "/a/b/c" also yields "/a/b/". The list of path segments goes from ["a", "b", "c"]
 * to ["a", "b", ""].
 */
private void pop() {
 String removed = encodedPathSegments.remove(encodedPathSegments.size() - 1);
 // Make sure the path ends with a '/' by either adding an empty string or clearing a segment.
 if (removed.isEmpty() && !encodedPathSegments.isEmpty()) {
  encodedPathSegments.set(encodedPathSegments.size() - 1, "");
 } else {
  encodedPathSegments.add("");
 }
}

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

@Test
public void testGenericTypeNestingListOfMapOfInteger() throws Exception {
  List<Map<String, String>> list = new LinkedList<>();
  Map<String, String> map = new HashMap<>();
  map.put("testKey", "5");
  list.add(map);
  NestedGenericCollectionBean gb = new NestedGenericCollectionBean();
  BeanWrapper bw = new BeanWrapperImpl(gb);
  bw.setPropertyValue("listOfMapOfInteger", list);
  Object obj = gb.getListOfMapOfInteger().get(0).get("testKey");
  assertTrue(obj instanceof Integer);
  assertEquals(5, ((Integer) obj).intValue());
}

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

@Test
public void testGenericListOfMaps() throws MalformedURLException {
  GenericBean<String> gb = new GenericBean<>();
  List<Map<Integer, Long>> list = new LinkedList<>();
  list.add(new HashMap<>());
  gb.setListOfMaps(list);
  BeanWrapper bw = new BeanWrapperImpl(gb);
  bw.setPropertyValue("listOfMaps[0][10]", new Long(5));
  assertEquals(new Long(5), bw.getPropertyValue("listOfMaps[0][10]"));
  assertEquals(new Long(5), gb.getListOfMaps().get(0).get(10));
}

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

/** Regression test for bug found. */
public void testCorrectOrdering_regression() {
 MinMaxPriorityQueue<Integer> q = MinMaxPriorityQueue.create(ImmutableList.of(3, 5, 1, 4, 7));
 List<Integer> expected = ImmutableList.of(1, 3, 4, 5, 7);
 List<Integer> actual = new ArrayList<>(5);
 for (int i = 0; i < expected.size(); i++) {
  actual.add(q.pollFirst());
 }
 assertEquals(expected, actual);
}

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

private List<String> list(String... args) {
  List<String> list = new ArrayList<String>();
  for (String arg : args) {
    list.add(arg);
  }
  return list;
}

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

@Override
public void set(K key, @Nullable V value) {
  List<V> values = new LinkedList<>();
  values.add(value);
  this.targetMap.put(key, values);
}

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

protected HandshakeInterceptor[] getInterceptors() {
  List<HandshakeInterceptor> interceptors = new ArrayList<>(this.interceptors.size() + 1);
  interceptors.addAll(this.interceptors);
  interceptors.add(new OriginHandshakeInterceptor(this.allowedOrigins));
  return interceptors.toArray(new HandshakeInterceptor[0]);
}

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

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

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

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

  /* snip */

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

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

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

@Test
public void sortInstancesWithPriority() {
  List<Object> list = new ArrayList<>();
  list.add(new B2());
  list.add(new A2());
  AnnotationAwareOrderComparator.sort(list);
  assertTrue(list.get(0) instanceof A2);
  assertTrue(list.get(1) instanceof B2);
}

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

C() {
    ls = new ArrayList<>();
    ls.add("abc");
    ls.add("def");
    as = new String[] { "abc", "def" };
    ms = new HashMap<>();
    ms.put("abc", "xyz");
    ms.put("def", "pqr");
  }
}

相关文章