java.util.ArrayList类的使用及代码示例

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

包路径:java.util.ArrayList
类名称:ArrayList

ArrayList介绍

[英]ArrayList is an implementation of List, backed by an array. All optional operations including adding, removing, and replacing elements are supported.

All elements are permitted, including null.

This class is a good choice as your default List implementation. Vector synchronizes all operations, but not necessarily in a way that's meaningful to your application: synchronizing each call to get, for example, is not equivalent to synchronizing the list and iterating over it (which is probably what you intended). java.util.concurrent.CopyOnWriteArrayList is intended for the special case of very high concurrency, frequent traversals, and very rare mutations.
[中]ArrayList是List的一个实现,由数组支持。支持所有可选操作,包括添加、删除和替换图元。
允许所有元素,包括null。
作为默认列表实现,该类是一个不错的选择。Vector同步所有操作,但不一定以对应用程序有意义的方式进行:例如,同步每个get调用并不等同于同步列表并对其进行迭代(这可能是您想要的)。JAVAutil。同时发生的CopyOnWriteArrayList用于非常高的并发性、频繁的遍历和非常罕见的突变的特殊情况。

代码示例

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: iluwatar/java-design-patterns

/**
 * Get all items
 */
public List<Item> getItems() {
 return new ArrayList<>(items);
}

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

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

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

@Override
public void add(int index, T element) {
  list.add(index, element);
  lazySet(list.size());
}

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

@Test(expected=IndexOutOfBoundsException.class)
public void testIndexOutOfBoundsException() {
  ArrayList emptyList = new ArrayList();
  Object o = emptyList.get(0);
}

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

@Test
public void testNoBackpressure() {
  ArrayList<Long> list = new ArrayList<Long>(Flowable.bufferSize() * 2);
  for (long i = 1; i <= Flowable.bufferSize() * 2 + 1; i++) {
    list.add(i);
  }
  Observable<Long> o = Observable.rangeLong(1, list.size());
  TestObserver<Long> to = new TestObserver<Long>();
  o.subscribe(to);
  to.assertValueSequence(list);
  to.assertTerminated();
}

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

@CollectionSize.Require(absent = CollectionSize.ZERO)
@CollectionFeature.Require(ALLOWS_NULL_VALUES)
public void testEquals_containingNull() {
 ArrayList<E> elements = new ArrayList<>(getSampleElements());
 elements.set(elements.size() / 2, null);
 collection = getSubjectGenerator().create(elements.toArray());
 List<E> other = new ArrayList<>(getSampleElements());
 assertFalse(
   "Two Lists should not be equal if exactly one of them has null at a given index.",
   getList().equals(other));
}

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

@CollectionSize.Require(absent = CollectionSize.ZERO)
public void testEquals_otherListWithDifferentElements() {
 ArrayList<E> other = new ArrayList<>(getSampleElements());
 other.set(other.size() / 2, getSubjectGenerator().samples().e3());
 assertFalse(
   "A List should not equal another List containing different elements.",
   getList().equals(other));
}

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

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

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

@Override
public boolean addAll(int index, Collection<? extends T> c) {
  boolean b = list.addAll(index, c);
  lazySet(list.size());
  return b;
}

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

@Override
public boolean remove(Object o) {
  boolean b = list.remove(o);
  lazySet(list.size());
  return b;
}

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

@Override
public boolean removeAll(Collection<?> c) {
  boolean b = list.removeAll(c);
  lazySet(list.size());
  return b;
}

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

@Override
public boolean retainAll(Collection<?> c) {
  boolean b = list.retainAll(c);
  lazySet(list.size());
  return b;
}

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

@Override
public T get(int index) {
  return list.get(index);
}

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

@Test
public void testNoBackpressure() {
  ArrayList<Integer> list = new ArrayList<Integer>(Flowable.bufferSize() * 2);
  for (int i = 1; i <= Flowable.bufferSize() * 2 + 1; i++) {
    list.add(i);
  }
  Observable<Integer> o = Observable.range(1, list.size());
  TestObserver<Integer> to = new TestObserver<Integer>();
  o.subscribe(to);
  to.assertValueSequence(list);
  to.assertTerminated();
}

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

@Override
  public void accept(ArrayList<Integer> a, Integer b) throws Exception {
    a.add(b);
  }
}).toFlowable());

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

@Override
public boolean addAll(Collection<? extends T> c) {
  boolean b = list.addAll(c);
  lazySet(list.size());
  return b;
}

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

@Override
public T remove(int index) {
  T v = list.remove(index);
  lazySet(list.size());
  return v;
}

代码示例来源: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: 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;
 }
}

相关文章

微信公众号

最新文章

更多