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" }
/** * 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(); } });
private void toList(ArrayList<List<String>> lists) { ArrayList<String> e = new ArrayList<String>(); e.add(word); e.add(pos); if(parent==null){ e.add(String.valueOf(-1)); e.add("Root"); } else{ e.add(String.valueOf(parent.id)); e.add(relation); } lists.set(id, e); for (int i = 0; i < leftChilds.size(); i++) { leftChilds.get(i).toList(lists); } for (int i = 0; i < rightChilds.size(); i++) { rightChilds.get(i).toList(lists); } } //错误
@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)); }
/** * @param index Index of the stripe we need. * @return A lazy stripe copy from current stripes. */ private final ArrayList<HStoreFile> getStripeCopy(int index) { List<HStoreFile> stripeCopy = this.stripeFiles.get(index); ArrayList<HStoreFile> result = null; if (stripeCopy instanceof ImmutableList<?>) { result = new ArrayList<>(stripeCopy); this.stripeFiles.set(index, result); } else { result = (ArrayList<HStoreFile>)stripeCopy; } return result; }
/** * Removes element at index, but instead of copying all elements to the left, moves into the same slot the last * element. This avoids the copy costs, but spoils the list order. If index is the last element it is just removed. * * @param list to be modified. * @param index to be removed. * @param <T> element type. * @throws IndexOutOfBoundsException if index is out of bounds. */ public static <T> void fastUnorderedRemove(final ArrayList<T> list, final int index) { final int lastIndex = list.size() - 1; if (index != lastIndex) { list.set(index, list.remove(lastIndex)); } else { list.remove(index); } }
public void set(int index, int value) { int subListIndex = index / ALLOCATION_SIZE; if (subListIndex >= baseLists.size()) { for (int i = baseLists.size(); i <= subListIndex; ++i) { baseLists.add(null); } } int[] baseList = baseLists.get(subListIndex); if (baseList == null) { baseList = new int[ALLOCATION_SIZE]; baseLists.set(subListIndex, baseList); } baseList[index % ALLOCATION_SIZE] = value; if (index > maxIndex) { maxIndex = index; } }
@Override public SearchArgument build() { if (currentTree.size() != 1) { throw new IllegalArgumentException("Failed to end " + currentTree.size() + " operations."); } ExpressionTree optimized = pushDownNot(root); optimized = foldMaybe(optimized); optimized = flatten(optimized); optimized = convertToCNF(optimized); optimized = flatten(optimized); int leafReorder[] = new int[leaves.size()]; Arrays.fill(leafReorder, -1); int newLeafCount = compactLeaves(optimized, 0, leafReorder); optimized = rewriteLeaves(optimized, leafReorder); ArrayList<PredicateLeaf> leafList = new ArrayList<>(newLeafCount); // expand list to correct size for(int i=0; i < newLeafCount; ++i) { leafList.add(null); } // build the new list for(Map.Entry<PredicateLeaf, Integer> elem: leaves.entrySet()) { int newLoc = leafReorder[elem.getValue()]; if (newLoc != -1) { leafList.set(newLoc, elem.getKey()); } } return new SearchArgumentImpl(optimized, leafList); }
public static <V> ArrayList<V> convertToArray(Map<Integer, V> srcMap, int start) { Set<Integer> ids = srcMap.keySet(); Integer largestId = ids.stream().max(Integer::compareTo).get(); int end = largestId - start; ArrayList<V> result = new ArrayList<>(Collections.nCopies(end + 1, null)); // creates array[largestId+1] filled with nulls for (Map.Entry<Integer, V> entry : srcMap.entrySet()) { int id = entry.getKey(); if (id < start) { LOG.debug("Entry {} will be skipped it is too small {} ...", id, start); } else { result.set(id - start, entry.getValue()); } } return result; }
@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)); }
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(); } });