Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Wednesday, December 6, 2017

Java ArrayList remove Example: How to remove by index, by Value/Object, for a specific range


How to remove a value from ArrayList in java with example programs.

In this post, we will learn how to program to remove elements from a ArrayList in java. Removing value can be done in three ways.

1) By index
2) By value or Object
3) For a given specific range

ArrayList api provides various methods to do remove operations.

java-arraylist-remove






Removing value by index:


Syntax: public E remove(int index)

Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Example:


package com.adeepdrive.arraylist;

import java.util.ArrayList;
import java.util.List;

public class ArrayListRemoveIndexExample {
 public static void main(String[] args) {

  List fruits = new ArrayList();
  fruits.add("Apple");
  fruits.add("Banana");
  fruits.add("Orange");
  System.out.println("Before deleting: " + fruits);
  System.out.println("Removing index 1 value by using remove(int index) method");

  fruits.remove(1);
  System.out.println("After deleting : " + fruits);
 }
}

Output:


Before deleting: [Apple, Banana, Orange]
Removing index 1 value by using remove(int index) method
After deleting : [Apple, Orange]


First added three fruit names to fruits list, Next, invoked remove with index. Removed index 1 value (List index starts from 0 as Arrays) from list which holds value Banana. We can see the output.

Read article on How to iterate String Array in java.

remove(index) method returns deleted value from list.

String removedValue = fruits.remove(1);
System.out.println("Removed value: "+removedValue);


Output:

Removed value: Banana

If the index is outside range of 0 and size-1 of list then it throws IndexOutOfBoundsException.

Removing by Value or Object:

 

Syntax: public boolean remove(Object o)


  1. Removes the first occurrence of the specified element from this list, if it is present.
  2. If the list does not contain the element, it is unchanged.
  3. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

Example:



package com.java.w3schools.arraylist;

import java.util.ArrayList;
import java.util.List;

public class ArrayListRemoveValueExample {
 public static void main(String[] args) {

  List colors = new ArrayList();
  colors.add("White");
  colors.add("Red");
  colors.add("Green");
  colors.add("Yellow");
  colors.add("Green");
  System.out.println("Before deleting: " + colors);
  System.out.println("Removing Red color value by using remove(Object value) method");

  boolean isRemoved = colors.remove("Red");
  System.out.println("Removed Status: " + isRemoved);
  System.out.println("After deleting : " + colors);
 }
}

Output:


Before deleting: [White, Red, Green, Yellow, Green]
Removing Red colour value by using remove(Object value) method
Removed Status: true
After deleting : [White, Green, Yellow, Green]

It loops the array and finds the first match for deletion. Next, shifts the next elements internally using System.arrayCopy method.

Duplicate values Scenario:


If to be removed value is present at multiple indexes, it removes the first occurrence and keep the remaining occurrences in list.
In above program "Green" appears twice, if we remove it then first Green will be deleted and second Green remains in list.


boolean isRemoved = colors.remove("Green");
System.out.println("Removed Status: " + isRemoved);
System.out.println("After deleting : " + colors);

Output:


Removed Status: true
After deleting : [White, Red, Yellow, Green]

If the value does not exist, it returns false and list is unchanged.

  boolean isRemoved = colors.remove("Gray"); 
  System.out.println("Removed Status: " + isRemoved); 
  System.out.println("After deleting : " + colors);

Output:


Removed Status: false
After deleting : [White, Red, Green, Yellow, Green]

Removing for a specific range:

 

Syntax: protected void removeRange(int fromIndex, int toIndex)

  1. Removes from this list all of the elements whose index is between fromIndex(inclusive) and toIndex(exclusive).
  2. Shifts any succeeding elements to the left (reduces their index).
  3. This call shortens the list by (toIndex - fromIndex) elements.
  4. If toIndex==fromIndex, this operation has no effect.

removeRange method is protected:

 

Protected methods are accessible in only within package or sub classes. removeRange is protected so directly through ArrayList object we can not call this method. So, We have extended ArrayList in the following program.

Example:

package com.java.w3schools.arraylist;

import java.util.ArrayList;
import java.util.List;

public class ArrayListRemoveRangeExample  extends ArrayList{
 public static void main(String[] args) {

  ArrayListRemoveRangeExample numbers = new ArrayListRemoveRangeExample();
  numbers.add(1); // index 0
  numbers.add(2); // index 1
  numbers.add(3); // index 2
  numbers.add(4); // index 3
  numbers.add(5); // index 4
  numbers.add(6); // index 5
  numbers.add(7); // index 6
  numbers.add(8); // index 7
  numbers.add(9); // index 8
  numbers.add(10); // index 9

  System.out.println("Before deleting: " + numbers);
  System.out.println("Removing range from index 2 to 5 by using removeRange(int fromIndex, int toIndex) method");

  numbers.removeRange(2, 5);
  System.out.println("After deleting : " + numbers);
 }
}

Output:


Before deleting: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Removing range from index 2 to 5 by using removeRange(int fromIndex, int toIndex) method
After deleting : [1, 2, 6, 7, 8, 9, 10]

If indexes are out of range then IndexOutOfBoundsException will be thrown.

If you like this post, Please share with your friends and leave your questions in comments section.


No comments:

Post a Comment

Please do not add any spam links in the comments section.