java.util.ArrayList.set()方法的使用及代码示例

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

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

ArrayList.set介绍

[英]Replaces the element at the specified location in this ArrayListwith the specified object.
[中]用指定的对象替换此ArrayList中指定位置的元素。

代码示例

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: apache/flink

/**
 * 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);
}

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

private Value next() throws IOException {
  if (vIdx < vLen) {
    Value v = valList.get(vIdx);
    valList.set(vIdx, null);
    vIdx++;
    return v;
  } else {
    throw new IOException("Error in deserialization.");
  }
}

代码示例来源:origin: libgdx/libgdx

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();
  }
});

代码示例来源:origin: FudanNLP/fnlp

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);
  }
  
}
//错误

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

@Override
public synchronized E set(int index, E element) {
 ArrayList<E> newList = new ArrayList<>(list);
 E result = newList.set(index, element);
 Collections.sort(list, comparator);
 list = Collections.unmodifiableList(newList);
 return result;
}

代码示例来源: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: apache/hbase

/**
 * @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;
}

代码示例来源:origin: kilim/kilim

void setLabel(int pos, LabelNode l) {
  for (int i = pos - posToLabelMap.size() + 1; i >= 0; i--) {
    // pad with nulls ala perl
    posToLabelMap.add(null);
  }
  posToLabelMap.set(pos, l);
  labelToPosMap.put(l, pos);
}

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

void incr() {
  if (supportsStreaming) {
   rowNums.set(0,new IntWritable(nextRow++));
  } else {
   rowNums.add(new IntWritable(nextRow++));
  }
 }
}

代码示例来源:origin: real-logic/agrona

/**
 * 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);
  }
}

代码示例来源:origin: org.codehaus.jackson/jackson-mapper-asl

public JsonNode _set(int index, JsonNode value)
{
  if (_children == null || index < 0 || index >= _children.size()) {
    throw new IndexOutOfBoundsException("Illegal index "+index+", array size "+size());
  }
  return _children.set(index, value);
}

代码示例来源:origin: apache/incubator-druid

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;
 }
}

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

@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);
}

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

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;
}

代码示例来源: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: libgdx/libgdx

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();
  }
});

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

public void setData(int index, ByteBuffer data) {
  if (index >= 0) {
    while (this.data.size() <= index) {
      this.data.add(null);
    }
    this.data.set(index, data);
    setUpdateNeeded();
  } else {
    throw new IllegalArgumentException("index must be greater than or equal to 0.");
  }
}

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

void addRank() {
  if (supportsStreaming) {
   rowNums.set(0, new IntWritable(currentRank));
  } else {
   rowNums.add(new IntWritable(currentRank));
  }
 }
}

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

public boolean done() {
  Value v = valList.get(vIdx);
  if ("/array".equals(v.getType())) {
    valList.set(vIdx, null);
    vIdx++;
    return true;
  } else {
    return false;
  }
}
public void incr() {}

相关文章