0

I have array list of strings. I want to check in specific moment if in this array I have more elements than my "i", if yes I want to remove that elements. For example. I have five elements in array. I choose element which index is four. I want to check if exist higher element(in this case that higher will be element which index is 5) and remove that element. If I choose 3 element I want to remove 4 and 5 element. I do something like that:

            for(int j = 0; j<descriptions.size();j++){
                if(!descriptions.get(i+1).isEmpty()){
                    descriptions.remove(i+1);
                }
            }

This solution work good when I choose 3 element and two elements was removed. But when I want choose 4 element I get index out of bound exception. How I can solve my problem?

user1302569
  • 7,131
  • 13
  • 46
  • 66

5 Answers5

2

I don't quite see the point of using for loop in your code.

What you probably want to do is to remove any items beyond i th element in the list.

The easiest way to do is to repeatedly remove the last element from the list.

Here's a sample code for reference:

while(descriptions.size() > i){
  descriptions.remove(descriptions.size()-1);
}
wns349
  • 1,266
  • 1
  • 10
  • 20
0

I have five elements in array. I choose element which index is four.

The fifth element is at index 4. if you want to choose the 4th element, it's index will be 3.

Modify your code as following:

int size = descriptions.size();
for(int j = size -1; j>choosenNum; j--) 
{
    descriptions.remove(j);
}
Azodious
  • 13,752
  • 1
  • 36
  • 71
0
        for(int i = descriptions.size()-1; i > indexToDeleteAfter; i--){
                descriptions.remove(i);
        }

Or, defer to ArrayList:

if(descriptions.size() - 1 > indexToDeleteAfter)
{
   descriptions.removeRange(indexToDeleteAfter + 1, descriptions.size() - 1);
}

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html#removeRange(int,%20int)

Blake Caldwell
  • 311
  • 2
  • 8
0
 public static void main(String[] args) {

 //list of string with 5 elements
 List<String> descriptions = new ArrayList<String>();
 descriptions.add("first");
 descriptions.add("second");
 descriptions.add("third");
 descriptions.add("4");
 descriptions.add("5");

 //the size you want to check for the list
 int i = 3;
 int howMuchToRemove = descriptions.size()-i;
 //if the list how more object from > i , we will remove objects from it 
 if (howMuchToRemove > 0)
     for (int j=0 ; j < howMuchToRemove ; j++)
         //remove the last object in the list
         descriptions.remove(descriptions.size()-1);

 System.out.println(descriptions.toString());
}
Nimrod007
  • 9,825
  • 8
  • 48
  • 71
0
 public static void main(String[] args) {
    ArrayList<String> list = new ArrayList<String>();
    list.add("a");
    list.add("a");
    list.add("a");
    list.add("a");
    list.add("a");
    indexToRemove(list, 5);

}

private static void indexToRemove(ArrayList<String> list, int index) {
    if (list.size() > index) {
        list.remove(index);
        System.out.println(index + "th item removed");
    } else
        System.out.println("Can't remove");

}

You mean a function which will remove the given index element? then try this.

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39