Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3.  
  4. public class Numbers {
  5. private int[] arr;
  6. private int numE;
  7. private final int INIT_SIZE = 100;
  8.  
  9.  
  10.  
  11. public Numbers(int size){
  12. arr = new int[size];
  13. numE = 0;
  14. }
  15.  
  16. public Numbers(){
  17. arr = new int[100];
  18. numE = 0;
  19. }
  20.  
  21. public void add(int num){
  22. if(numE == arr.length-1){
  23. enlargeArray();
  24. }
  25.  
  26. arr[numE] = num;
  27. numE++;
  28.  
  29.  
  30. }
  31.  
  32. private void enlargeArray(){
  33. int[] temp = new int[(arr.length)*2];
  34. for(int i = 0; i < arr.length-1; i++){
  35. temp[i] = arr[i];
  36. }
  37. arr = temp;
  38. }
  39.  
  40. public void remove(int num){
  41. ArrayList<Integer> temp = new ArrayList<Integer>();
  42. for(int i = 0; i < numE; i++){
  43. if(arr[i] == num){
  44. numE--;
  45. } else temp.add(arr[i]);
  46. }
  47. temp.toArray(new int[arr.size]);
  48. arr = temp;
  49. }
  50.  
  51. public String toString(){
  52. System.out.print("[ ");
  53. for(int i = 0; i < numE; i++){
  54. System.out.print(arr[i]+" ");
  55. }
  56. System.out.print("]");
  57. //add it to a string like a normal program
  58. }
  59.  
  60.  
  61. public static void main(String[] args){
  62. Numbers N = new Numbers(3);
  63. N.add(4);
  64. N.toString();
  65. }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement