Advertisement
Guest User

Untitled

a guest
May 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. package series01;
  2.  
  3. public class StringArrayList {
  4.  
  5. public String[] elements = new String[1];
  6.  
  7. private int counter = 0;
  8.  
  9. public boolean isEmpty() {
  10. return (counter == 0);
  11. }
  12.  
  13. public int size() {
  14. return counter;
  15. }
  16.  
  17. public String get(int index) {
  18.  
  19. if (index >= counter || index < 0) {
  20. throw new IndexOutOfBoundsException("Fehler");
  21. }
  22. return elements[index];
  23. }
  24.  
  25. public void add(String str) {
  26.  
  27. if (counter == elements.length) {
  28. elements = Arrays.expandArray(elements);
  29. }
  30.  
  31. elements[counter] = str;
  32. counter++;
  33.  
  34. }
  35.  
  36. public void add(int index, String e) {
  37. if (index > size() || index < 0) {
  38. throw new IndexOutOfBoundsException();
  39. }
  40.  
  41. if (counter == elements.length) {
  42. elements = Arrays.expandArray(elements);
  43. // System.out.println("expand array");
  44. }
  45. if (index != size()) {
  46. elements = Arrays.shiftElements(elements, index);
  47. }
  48.  
  49. elements[index] = e;
  50.  
  51. counter++;
  52.  
  53. }
  54.  
  55. public void remove() {
  56. if (isEmpty())
  57. throw new IndexOutOfBoundsException("Liste ist leer");
  58.  
  59. for (int i = 0; i < size()-1; i++) {
  60. elements[i] = elements[i + 1];
  61.  
  62. }
  63. elements[size()] = null;
  64. counter--;
  65.  
  66. }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement