# Introduction to algorithms using java

public class Algo {

private int[] arrayList = new int[20];
private int arraySize = 10;

public void arrayGenerator(){
for (int i =0; i<arraySize; i++){
//generate random numbers between 10 and 19
arrayList[i]= (int) (Math.random()*10)+10;
}
}

public void printArray(){
System.out.println("----------");
for (int i=0; i<arraySize; i++){
System.out.print("| " + i +" | ");
System.out.println(arrayList[i] + " |");
System.out.println("----------");
}
}

public static void main(String[] args) {
Algo con = new Algo();
con.arrayGenerator();
con.printArray();

}
}

Line 3 and 4 basically creates a new array, with an initial size of 20, we later go ahead and limit the fields to be populated to 10. that would do for this tutorial ðŸ™‚

Line 7 declares a method called arrayGenerator, arrayGenerator when called, fills the array with random numbers. and finally line 15 displays the contents of the array calling the method printArray

instantiating our class on line 26, we call the arrayGenerator method before printing the values of the array. below is a screenshot of what you should expect.

##### Getting an element by it’s index
public int getValueByIndex(int index){
if(index < arraySize){
return arrayList[index];
}
return 0;
}

It’s important you ensure that the search index being passed to the method is within the array size. (remember we set a limit of 10!) passing a search index of 11 upwards would lead to a error. we only return the value at the given index if it is less than the agreed array size if(index < arraySize)… running algo.getValueByIndex(4) should give you 17. 17 is the value whose index is 4. refer to the screenshot above.

##### Confirm if a given value is present
public boolean confirmValue(int value){
boolean status = false;
for(int i=0; i<arraySize; i++){
if (arrayList[i] == value){
status = true;
}
}

return status;
}

We create a method that returns True or False (denoted by public boolean methodName..). Notice I did not bother checking the size of the incoming argument, any given integer value is perfectly fine. We loop through the array 10 times. For each iteration, we compare the value of each element in our array (arrayList[i]) with that of the given value. this comparison is done in a if statement. unless both values match, the value of status remains false! Once a match is found, we set status equal to True.

run algo.confirmValue(17) //returns True

##### Delete a value by it’s index
public void deleteByIndex(int index){
if(index < arraySize){
for(int i = index; i<(arraySize-1); i++){
arrayList[i]= arrayList[i+1];
}

arraySize--;
}

}

Following the same ol rules, you want to ensure the index being passed is within the agreed array size (arraySize is 10). Let’s imagine a user passes 4, i becomes 4, we are going to loop through the array for as long as 4 the index is less than 9. here we go

while i=4, i<(arrayList-1) returns true, because 4 is less than 9, then we go into the loop, arrayList[4] which has the value 17 is our target, that's what we want to delete. arrayList[i] = arrayList[i+1] is the same as arrayList[4] = arrayList[4+1], this is how we delete the particular index, we simply overwrite it, we take the 5th element and place its value into the 4th element, the next time we loop again we encounter i++, this increments i by 1, so i becomes 5. arrayList[i] = arrayList[i+1] becomes arrayList[5] = arrayList[5+1] so we take the value of the 6th element (arrayList[5+1]) and place it in the 5th element (arrayList[5]), this way we ensure that other elements get pushed up into the array. get it? and finally we run arrayList--. since we deleted an array, the size no longer becomes 10 but 9, so the new size is arrayList-- (which means 9)

##### Inserting a value into the array
public void addValue(int value){
if(arraySize < 20){
arrayList[arraySize]=value;
arraySize ++;
}
}

This is quiet easy, when adding an element into the array, we check to see if the arraySize is less than 20, remember private int[] arrayList = new int[20];. the maximum number of elements our array can take is 20. once this condition is met, we proceed by passing the arraySize as the index. this way we are certain that the element appears as the last element in the array. and finally we increase the array size i.e arraySize ++;

Posted in Java, Programming and tagged .