JAVA   21

ArrayIntList1.java

Guest on 12th January 2022 05:20:07 PM

  1. public class ArrayIntList {
  2.     private int[] elementData;
  3.     private int size;
  4.  
  5.     public ArrayIntList() {
  6.         elementData = new int[100];
  7.         size = 0;
  8.     }
  9.  
  10.     public void add(int value) {
  11.         elementData[size] = value;
  12.         size++;
  13.     }
  14.  
  15.     public void print() {
  16.         if (size == 0) {
  17.             System.out.println("[]");
  18.         } else {
  19.             System.out.print("[" + elementData[0]);
  20.             for (int i = 1; i < size; i++)
  21.                 System.out.print(", " + elementData[i]);
  22.             System.out.println("]");
  23.         }
  24.     }
  25. }

Raw Paste


Login or Register to edit or fork this paste. It's free.