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

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

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

ArrayList.get介绍

[英]Returns the element at the specified position in this list.
[中]返回此列表中指定位置的元素。

代码示例

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: stackoverflow.com

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

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

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

}

代码示例来源: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: stackoverflow.com

ViewPager pager = /* get my ViewPager */;
// assume this actually has stuff in it
final ArrayList<String> titles = new ArrayList<String>();

FragmentManager fm = getSupportFragmentManager();
pager.setAdapter(new FragmentStatePagerAdapter(fm) {
  public int getCount() {
    return titles.size();
  }

  public Fragment getItem(int position) {
    MyFragment fragment = new MyFragment();
    fragment.setTitle(titles.get(position));
    return fragment;
  }

  public int getItemPosition(Object item) {
    MyFragment fragment = (MyFragment)item;
    String title = fragment.getTitle();
    int position = titles.indexOf(title);

    if (position >= 0) {
      return position;
    } else {
      return POSITION_NONE;
    }
  }
});

代码示例来源:origin: scouter-project/scouter

public void printCRUD() {
  do {
    System.out.print("type:" + sqlNode.type.toString() + "-->");
    for (int i = 0; i < sqlNode.tableList.size(); i++) {
      System.out.print(sqlNode.tableList.get(i) + " ");
    }
    System.out.println();
    sqlNode = sqlNode.nextNode;
  } while (sqlNode != null);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testGenericListOfArrays() throws MalformedURLException {
  GenericBean<String> gb = new GenericBean<>();
  ArrayList<String[]> list = new ArrayList<>();
  list.add(new String[] {"str1", "str2"});
  gb.setListOfArrays(list);
  BeanWrapper bw = new BeanWrapperImpl(gb);
  bw.setPropertyValue("listOfArrays[0][1]", "str3 ");
  assertEquals("str3 ", bw.getPropertyValue("listOfArrays[0][1]"));
  assertEquals("str3 ", gb.getListOfArrays().get(0)[1]);
}

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

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

代码示例来源:origin: typ0520/fastdex

@Test
public void testDiff2() throws Throwable {
  if (!isDir(source_set1) || !isDir(source_set2) || !isDir(source_set11) || !isDir(source_set22)) {
    System.err.println("Test-env not init!!");
    return;
  }
  SourceSetSnapshoot now = new SourceSetSnapshoot(new File(workDir),source_set1);
  now.serializeTo(new FileOutputStream(new File(workDir,"snapshoot.json")));
  SourceSetSnapshoot old = (SourceSetSnapshoot) SourceSetSnapshoot.load(new File(workDir,"snapshoot.json"),SourceSetSnapshoot.class);
  JavaDirectorySnapshoot javaDirectorySnapshoot = new ArrayList<>(old.directorySnapshootSet).get(0);
  FileNode fileNode = new ArrayList<>(javaDirectorySnapshoot.nodes).get(0);
  fileNode.lastModified = System.currentTimeMillis();
  SourceSetDiffResultSet resultSet = (SourceSetDiffResultSet) now.diff(old);
  assertEquals(resultSet.changedJavaFileDiffInfos.size(),1);
  System.out.println(resultSet);
}

代码示例来源:origin: ksoichiro/Android-ObservableScrollView

private void removeFixedViewInfo(View v, ArrayList<FixedViewInfo> where) {
  int len = where.size();
  for (int i = 0; i < len; ++i) {
    FixedViewInfo info = where.get(i);
    if (info.view == v) {
      where.remove(i);
      break;
    }
  }
}

代码示例来源:origin: rey5137/material

@Override
public void registerListener(OnThemeChangedListener listener) {
  boolean exist = false;
  for(int i = mListeners.size() - 1; i >= 0; i--){
    WeakReference<OnThemeChangedListener> ref = mListeners.get(i);
    if(ref.get() == null)
      mListeners.remove(i);
    else if(ref.get() == listener)
      exist = true;
  }
  if(!exist)
    mListeners.add(new WeakReference<>(listener));
}

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

public void print() {
  int i;
  for (i = 1; i <= size; i++)
    System.out.println(scores[i] + " " +datas.get(i).toString());
  System.out.println();
}

代码示例来源:origin: facebook/stetho

public synchronized boolean unregister(PathMatcher path, HttpHandler handler) {
 int index = mPathMatchers.indexOf(path);
 if (index >= 0) {
  if (handler == mHttpHandlers.get(index)) {
   mPathMatchers.remove(index);
   mHttpHandlers.remove(index);
   return true;
  }
 }
 return false;
}

代码示例来源:origin: daniulive/SmarterStreaming

private ArrayList<Map<String, Object>> getMapData(ArrayList<ArrayList<String>> list) {
  ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
  if (list == null)
    return data;
  for (int i = 0; i < list.size(); i++) {
    Map<String, Object> item = new HashMap<String, Object>();
    item.put("ItemFileName", list.get(i).get(0));
    data.add(item);
  }
  return data;
}

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

private List<Cue> getDisplayCues() {
 List<Cue> displayCues = new ArrayList<>();
 for (int i = 0; i < cueBuilders.size(); i++) {
  Cue cue = cueBuilders.get(i).build();
  if (cue != null) {
   displayCues.add(cue);
  }
 }
 return displayCues;
}

代码示例来源:origin: scouter-project/scouter

public void printCRUD() {
  do {
    System.out.print("type:" + sqlNode.type.toString() + "-->");
    for (int i = 0; i < sqlNode.tableList.size(); i++) {
      System.out.print(sqlNode.tableList.get(i) + " ");
    }
    System.out.println();
    sqlNode = sqlNode.nextNode;
  } while (sqlNode != null);
}

代码示例来源:origin: spring-projects/spring-framework

@Test(expected = IndexOutOfBoundsException.class)
  public void verifyJUnitExpectedException() {
    new ArrayList<>().get(1);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testGenericListOfArraysWithElementConversion() throws MalformedURLException {
  GenericBean<String> gb = new GenericBean<>();
  ArrayList<String[]> list = new ArrayList<>();
  list.add(new String[] {"str1", "str2"});
  gb.setListOfArrays(list);
  BeanWrapper bw = new BeanWrapperImpl(gb);
  bw.registerCustomEditor(String.class, new StringTrimmerEditor(false));
  bw.setPropertyValue("listOfArrays[0][1]", "str3 ");
  assertEquals("str3", bw.getPropertyValue("listOfArrays[0][1]"));
  assertEquals("str3", gb.getListOfArrays().get(0)[1]);
}

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

@Override
State shift(Object type) {
  if (_state == TYPE) {
    if (type instanceof String) {
      _typeDefList.add((String) type);
    } else if (type instanceof Integer) {
      int iValue = (Integer) type;
      if (iValue >= 0 && iValue < _typeDefList.size())
        type = _typeDefList.get(iValue);
    }
    printObject("map " + type + " (#" + _refId + ")");
    _state = VALUE;
    return this;
  } else
    throw new IllegalStateException();
}

相关文章