Tabnine Logo
ArrayList
Code IndexAdd Tabnine to your IDE (free)

How to use
ArrayList
in
java.util

Best Java code snippets using java.util.ArrayList (Showing top 20 results out of 436,545)

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;
 }
}
java.utilArrayList

Javadoc

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.

Most used methods

  • <init>
  • add
  • size
    Returns the number of elements in this ArrayList.
  • get
    Returns the element at the specified position in this list.
  • toArray
    Returns an array containing all of the elements in this list in proper sequence (from first to last
  • addAll
    Adds the objects in the specified collection to this ArrayList.
  • remove
    Removes the first occurrence of the specified element from this list, if it is present. If the list
  • clear
    Removes all elements from this ArrayList, leaving it empty.
  • isEmpty
    Returns true if this list contains no elements.
  • iterator
    Returns an iterator over the elements in this list in proper sequence.The returned iterator is fail-
  • contains
    Searches this ArrayList for the specified object.
  • set
    Replaces the element at the specified position in this list with the specified element.
  • contains,
  • set,
  • indexOf,
  • clone,
  • subList,
  • stream,
  • ensureCapacity,
  • trimToSize,
  • removeAll,
  • toString

Popular in Java

  • Reactive rest calls using spring rest template
  • setScale (BigDecimal)
  • runOnUiThread (Activity)
  • notifyDataSetChanged (ArrayAdapter)
  • FileOutputStream (java.io)
    An output stream that writes bytes to a file. If the output file exists, it can be replaced or appen
  • PrintStream (java.io)
    Fake signature of an existing Java class.
  • UnknownHostException (java.net)
    Thrown when a hostname can not be resolved.
  • Timestamp (java.sql)
    A Java representation of the SQL TIMESTAMP type. It provides the capability of representing the SQL
  • Executor (java.util.concurrent)
    An object that executes submitted Runnable tasks. This interface provides a way of decoupling task s
  • BoxLayout (javax.swing)
  • From CI to AI: The AI layer in your organization
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now