Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Hostgator 1 Penny

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


7,369 questions

10,368 answers

573 users

How to create a class that provide an array of integers with add, remove, insert, sort, show in Java

Payoneer - GET PAID BY MARKETPLACES & DIRECT CLIENTS WORLDWIDE - Quick, low-cost and secure solutions to pay and get paid globally


17 views
asked Jul 2, 2017 by avibootz
edited Jul 2, 2017 by avibootz

1 Answer

0 votes
// java code that create a class that provide an array of integers with add, 
// remove, insert, sort

package javaapplication1;

import java.util.Arrays;
import java.util.Random;

public class JavaApplication1 {
  
    private static final int DEFAULT_SIZE = 2;
    
    private int[] arr;
    int current = 0;
    
    public JavaApplication1() {
        this(DEFAULT_SIZE);
    }
    public JavaApplication1(int size) {
        if (size > 0)
            arr = new int[size];
        else
            arr = new int[DEFAULT_SIZE];
        init();
    }
    private void init() {
       Random rnd = new Random(); 
       for (int i = 0; i < arr.length; i++)
           arr[i] = rnd.nextInt(100);
    }
    private int size() {
      return arr.length;
    }
    public void add(int n) {
        resize();
        arr[arr.length - 1] = n;
    }
    private void resize() {
        int[] tmp = new int[arr.length + 1];
        
        System.arraycopy(arr, 0, tmp, 0, arr.length);
        
        arr = tmp;
    }
    private void reduce() {
        int[] tmp = new int[arr.length - 1];
        
        System.arraycopy(arr, 0, tmp, 0, arr.length - 1);
        
        arr = tmp;
    }
    @Override
    public String toString(){
        String result = "";
        
        for(int i = 0; i < arr.length; i++)
            result += arr[i] + ", ";

        return result;
    }
    public boolean equal(JavaApplication1 o) {
        boolean result = true;
        if(o == null)
            result = false;
        else if(this == o)
            result = true;
        else if(this.getClass() != o.getClass())
            result = false;
        else {
            int i = 0;
            while(i < arr.length && result) {
                result = this.arr[i] == o.arr[i];
                i++;
            }
        }
        return result;     
    }
    public void insert(int index, int n) {
        resize();
        for (int i = arr.length - 1; i > index; i--)
            arr[i] = arr[i - 1];
        arr[index] = n;
    }
    public void remove(int index) {
        for (int i = index; i < arr.length - 1; i++)
            arr[i] = arr[i + 1];
        reduce();
    }
    public void sort() {
        Arrays.sort(arr);
    }
    public void setValue(int i, int n) {
        arr[i ] = n;
    }
    public void show() {
         for (int i = 0; i < arr.length; i++)
           System.out.println(arr[i]);
    }
    public static void main(String[] args) {
        try
        {   
            JavaApplication1 arr1 = new JavaApplication1();
            JavaApplication1 arr2 = new JavaApplication1(3);
            JavaApplication1 arr3 = new JavaApplication1(0);
        
            System.out.println("arr1:");
            arr1.show();
            
            System.out.println("\narr2:");
            arr2.show();            
            
            System.out.println("\narr3:");
            arr3.show();
           
            System.out.println("\narr3:");
            arr3.add(100);
            arr3.show();
            
            System.out.println("\narr3:");
            String s = arr3.toString();
            System.out.println(s);
            
            System.out.println("\narr1.equal(arr2):");
            System.out.println(arr1.equal(arr2));
            
            JavaApplication1 arr4 = new JavaApplication1();
            System.out.println("\narr4:");
            arr4.show();
            
            arr4.setValue(0, 97);
            arr4.setValue(0, 63);
            
            System.out.println("\narr1.equal(arr4):");
            System.out.println(arr4.equal(arr4));
            
            arr3.insert(2, 333);
            System.out.println("\narr3:");
            arr3.show();
            
            arr3.remove(0);
            System.out.println("\narr3:");
            arr3.show();
                        
            arr3.sort();
            System.out.println("\narr3:");
            arr3.show();
            
            System.out.println("\narr3 size:");
            System.out.println(arr3.size());
        }
        catch(Exception e) {
            System.out.println(e);
        }
    }
}


/*
run:
 
arr1:
58
34

arr2:
69
73
24

arr3:
87
85

arr3:
87
85
100

arr3:
87, 85, 100, 

arr1.equal(arr2):
false

arr4:
97
6

arr1.equal(arr4):
true

arr3:
87
85
333
100

arr3:
85
333
100

arr3:
85
100
333

arr3 size:
3

*/

 




answered Jul 2, 2017 by avibootz
edited Jul 2, 2017 by avibootz
...