mardi 16 février 2016

How to write a remove method?

Write a method called get which will return an element from the given index. Returns -1 if the index is out-of-bounds.

Write a method called remove which will remove an element from the given index. Returns -1 if the index is out-of-bounds. Data should be shifted accordingly during removal.

//This is the get method 

public int get(int index){

    if(index < 0 || index >= size) {
       return -1;

 }else {
        return data[index];
    }

}

//This is the remove method

public int remove(int index){

    if(index < 0 || index >= size){
        return -1;

    }else {

        for(int i = 0; i < size-1; i++) {
            index[i] = index[i+1];
        }
    }

}

This is as far as I got. Not sure how to proceed with the code. I'd appreciate if someone could guide me through. Thank you!

Aucun commentaire:

Enregistrer un commentaire