Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jan 4th, 2013  |  syntax: None  |  size: 0.94 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ArrayList<String> list = new ArrayList<String>();
  2. list.add(0, "0-element");
  3. list.add(1, "1-element");
  4. list.add(2, "2-element");
  5.        
  6. ArrayList<String> list = new ArrayList<String>();
  7. list.add(1, "1-element");  // IndexOutOfBoundsException
  8. list.add(2, "2-element");
  9. list.add(0, "0-element");
  10.        
  11. if (pos>=list.size()) list.add(element);
  12. else list.add(pos, element);
  13.        
  14. public void add(int index, E element) {
  15.     if (index > size || index < 0)
  16.         throw new IndexOutOfBoundsException(
  17.         "Index: "+index+", Size: "+size);
  18.  
  19.     ensureCapacity(size+1);  // Increments modCount!!
  20.     System.arraycopy(elementData, index, elementData, index + 1,
  21.              size - index);
  22.     elementData[index] = element;
  23.     size++;
  24. }
  25.        
  26. list.add("element1");
  27. list.add("element2");
  28. list.add("element3);
  29.        
  30. Throws:
  31. IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())
  32.        
  33. List<String> list = new ArrayList<String>();
  34. list.add("...");