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" }
// Substitute appropriate type. ArrayList<...> a = new ArrayList<...>(); // Add elements to list. // Generate an iterator. Start just after the last element. ListIterator li = a.listIterator(a.size()); // Iterate in reverse. while(li.hasPrevious()) { System.out.println(li.previous()); }
public class SortedList<E> extends AbstractList<E> { private ArrayList<E> internalList = new ArrayList<E>(); // Note that add(E e) in AbstractList is calling this one @Override public void add(int position, E e) { internalList.add(e); Collections.sort(internalList, null); } @Override public E get(int i) { return internalList.get(i); } @Override public int size() { return internalList.size(); } }
@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)); }
protected ArrayList<Integer> scanForPronouns(ArrayList<Pair<Integer, Integer>> nonQuoteRuns) { ArrayList<Integer> pronounList = new ArrayList<>(); for(int run_index = 0; run_index < nonQuoteRuns.size(); run_index++) pronounList.addAll(scanForPronouns(nonQuoteRuns.get(run_index))); return pronounList; }
/** * 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); }
public void mouseDragged (MouseEvent event) { if (dragIndex == -1 || dragIndex == 0 || dragIndex == percentages.size() - 1) return; float percent = (event.getX() - gradientX) / (float)gradientWidth; percent = Math.max(percent, percentages.get(dragIndex - 1) + 0.01f); percent = Math.min(percent, percentages.get(dragIndex + 1) - 0.01f); percentages.set(dragIndex, percent); repaint(); } });
public static void main(String [] argv) throws Exception { if (argv.length != 1) { log.info("Usage: java AceDomReader <APF file>"); System.exit(1); } File f = new File(argv[0]); AceDocument doc = parseDocument(f); System.out.println("Processed ACE document:\n" + doc); ArrayList<ArrayList<AceRelationMention>> r = doc.getAllRelationMentions(); System.out.println("size: " + r.size()); } }
@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()]))); } }
/** * 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); }
@Test public void testFirstGroupsCompleteAndParentSlowToThenEmitFinalGroupsWhichThenSubscribesOnAndDelaysAndThenCompletes() throws InterruptedException { System.err.println("----------------------------------------------------------------------------------------------"); final CountDownLatch first = new CountDownLatch(2); // there are two groups to first complete final ArrayList<String> results = new ArrayList<String>(); Flowable.unsafeCreate(new Publisher<Integer>() { System.out.println("Results: " + results); assertEquals(6, results.size());
@Test public void testStart() { Observable<String> os = OBSERVABLE_OF_5_INTEGERS .zipWith(OBSERVABLE_OF_5_INTEGERS, new BiFunction<Integer, Integer, String>() { @Override public String apply(Integer a, Integer b) { return a + "-" + b; } }); final ArrayList<String> list = new ArrayList<String>(); os.subscribe(new Consumer<String>() { @Override public void accept(String s) { System.out.println(s); list.add(s); } }); assertEquals(5, list.size()); assertEquals("1-1", list.get(0)); assertEquals("2-2", list.get(1)); assertEquals("5-5", list.get(4)); }
@Test public void testStartEmptyObservables() { Observable<String> o = Observable.zip(Observable.<Integer> empty(), Observable.<String> empty(), new BiFunction<Integer, String, String>() { @Override public String apply(Integer t1, String t2) { return t1 + "-" + t2; } }); final ArrayList<String> list = new ArrayList<String>(); o.subscribe(new Consumer<String>() { @Override public void accept(String s) { System.out.println(s); list.add(s); } }); assertEquals(0, list.size()); }