Guest User

Untitled

a guest
Jan 4th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  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("...");
Add Comment
Please, Sign In to add comment