Guest User

Untitled

a guest
Jan 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. List l = new ArrayList(1);
  2. l.add("1");
  3. l.add("2");
  4.  
  5. /**
  6. * Increases the capacity of this <tt>ArrayList</tt> instance, if
  7. * necessary, to ensure that it can hold at least the number of elements
  8. * specified by the minimum capacity argument.
  9. *
  10. * @param minCapacity the desired minimum capacity
  11. */
  12. public void ensureCapacity(int minCapacity) {
  13. modCount++;
  14. int oldCapacity = elementData.length;
  15. if (minCapacity > oldCapacity) {
  16. Object oldData[] = elementData;
  17. int newCapacity = (oldCapacity * 3) / 2 + 1;
  18. if (newCapacity < minCapacity)
  19. newCapacity = minCapacity;
  20. // minCapacity is usually close to size, so this is a win:
  21. elementData = Arrays.copyOf(elementData, newCapacity);
  22. }
  23. }
  24.  
  25. /**
  26. * Appends the specified element to the end of this list.
  27. *
  28. * @param e element to be appended to this list
  29. * @return <tt>true</tt> (as specified by {@link Collection#add})
  30. */
  31. public boolean add(E e) {
  32. ensureCapacity(size + 1); // Increments modCount!!
  33. elementData[size++] = e;
  34. return true;
  35. }
  36.  
  37.  
  38. /**
  39. * Increases the capacity of this <tt>ArrayList</tt> instance, if
  40. * necessary, to ensure that it can hold at least the number of elements
  41. * specified by the minimum capacity argument.
  42. *
  43. * @param minCapacity the desired minimum capacity
  44. */
  45. public void ensureCapacity(int minCapacity) {
  46. modCount++;
  47. int oldCapacity = elementData.length;
  48. if (minCapacity > oldCapacity) {
  49. Object oldData[] = elementData;
  50. int newCapacity = (oldCapacity * 3)/2 + 1;
  51. if (newCapacity < minCapacity)
  52. newCapacity = minCapacity;
  53. // minCapacity is usually close to size, so this is a win:
  54. elementData = Arrays.copyOf(elementData, newCapacity);
  55. }
  56. }
  57.  
  58. public void ensureCapacity(int minCapacity) {
  59. modCount++;
  60. int oldCapacity = elementData.length;
  61. if (minCapacity > oldCapacity) {
  62. Object oldData[] = elementData;
  63. int newCapacity = (oldCapacity * 3)/2 + 1;
  64. if (newCapacity < minCapacity)
  65. newCapacity = minCapacity;
  66. // minCapacity is usually close to size, so this is a win:
  67. elementData = Arrays.copyOf(elementData, newCapacity);
  68. }
  69. }
  70.  
  71. int increment = size / 2;
  72. if (required > increment) {
  73. increment = required;
  74. }
  75. if (increment < 12) {
  76. increment = 12;
  77. }
Add Comment
Please, Sign In to add comment