Array Implementation
CustomArray.java

package com.mugil.alg;

public class CustomArray {
	private int[] arrNum = new int[50];
	private int arrSize = 10;
	
	public static void main(String[] args) {
		CustomArray objCustomArray = new CustomArray();
		System.out.println("---------------");
		System.out.println("Array Initialized");
		objCustomArray.initializeArray();
		objCustomArray.printArray();
		
		System.out.println("---------------");
		System.out.println("Search for No(Index) - " + objCustomArray.getNumIndex(8));
		
		System.out.println("---------------");
		System.out.println("Delete Called");
		objCustomArray.deleteArrayByIndex(5);		
		objCustomArray.printArray();
		
		
		System.out.println("---------------");
		System.out.println("Insert Called");
		objCustomArray.insertArray(20);		
		objCustomArray.printArray();
		
		
	}
	
	public void initializeArray()
	{	
		for (int i = 0; i < arrSize; i++) {
			arrNum[i]  = (int)(Math.random()*10); 
		}
	}
	
	public void printArray()
	{
		
		System.out.print("Array - ");
		for (int i = 0; i < getArraySize(); i++) {
			System.out.print(arrNum[i]);
			System.out.print(" ");
		}		
		System.out.println();
	}
	
	
	//Linear Search
	//Use this if you want to find all the
	//all the places where the element Occurs
	public String getNumIndex(int pNum)
	{
		String NumPOS = "";
		
		for (int i = 0; i < getArraySize(); i++) {
			if(pNum == arrNum[i])
			{
				NumPOS +=  i + " ";
			}
		}
		
		if(NumPOS == "")
		 NumPOS = "None";
		
		return NumPOS;
	}
	
	
	public int getArraySize()
	{
		return arrSize;
	}
	
	public void deleteArrayByIndex(int pPOS)
	{
		
		for (int i = pPOS; i < getArraySize(); i++) {
				arrNum[i] = arrNum[i+1]; 
		}
		
		arrSize--;
	}
	
	public void insertArray(int pNum)
	{
		arrNum[arrSize] = pNum;		
		arrSize++;	
	}
}

Comments are closed.