I'm practcing for my test and I couldn't solve these questions I spent hours trying to solve it but every time I get error

ArrayList<objClass> li = new ArrayList<>();
    public objClass RemoveAtPos(int pos){
// I'm trying to remove the object at the given pos , but first I want to check if the pos is out of range
//if it out of range then return null otherwise remove the object and return the object that was removed 
//here is what I did 


            if (pos >= li.size() || pos < 0) {
                return null;
            } else {
                li.remove(pos);

            }
               return li.get(pos);

// when I test the method I get IndexOutOfBoundsException 


}

//also in the same class I have


    public String[] getAllElementInArr() {
    //I'm not sure how to this how can I get all the element in the li and store it in String of array  


    }

Recommended Answers

All 10 Replies

Line 11. If you just removed the item at position pos, what are you getting here? Suppose pos was the last element, what will be in position pos after it is removed?

Line 25. Create an array of the right size, loop through the list copying elements to the array.

I think that you are getting the IndexOutOfBounds exception, because when you test your method you have forgotten to actually populate the array with elements. I also don't like your "if" statement. Why don't you just use a try catch block and catch the out of bound exception (this exception is thrown when you request an element with index that is greter than the lenght of the array) ?

Actually just noticed that first you are removing the element, then returning it ... (you can't do that cos you removed the element!!!)

            } else {
                li.remove(pos);
            }
               return li.get(pos);

As for your last method write a for loop like the one I am going to write for you. (not sure what type of objects you are storing in your array (what is objClass ??? you haven't shared it with us), but I will assume you can cast it to string, else you can use the .toString())

String[] myStringArray = null; //init the array to null or  a new ArrayList<>() idk..
for(objClass obj : li) {
    myStringArray.add((String) obj);
}
return myStringArray;

Thank you I understand what you mean for getAllElementInArr()
but I'm not sure what you mean about the remove ?

Line 11. If you just removed the item at position pos, what are you getting here? Suppose pos was the last element, what will be in position pos after it is removed?

Line 25. Create an array of the right size, loop through the list copying elements to the array.

Seldar:

Throwing and catching exceptions is an expensive business, and is usually reserved for genuine error situations. The if test would be the normal way to check for a valid index and avoid an exception.

If you must post code for people's homework you can avoid embarassment by trying it yourself first. The code you posted appears to expose a complete ignorance of how arrays are created and populated, and I'm sure you do really know better than that.

I'm not sure what you mean about the remove ?

suppose the list is {"a", "b"} and you remove(1). That leaves just {"a"}. Now you try to return get(1), but that doesn't exist any more - there's just one element left (element 0)

Thanx everyone I think I figured how to do it ^^

@JamesCherrill
What do you mean by "The code you posted appears to expose a complete ignorance of how arrays are created and populated" ? How would you create and populate an array ?
Thanks

PS. I am doing ppl's homeworks not only to help them but for my own benefit from the extra little bit of practise.

You init the array to null, then try to add things to it with add(...). That's two fundamental mistakes for a start. You need to init it with an actual size, as in in array = new String[size], then add values by array[index] = whatever.

I see, thank you. +1 vote on your post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.