All IT Courses 50% Off
JAVA Tutorials

Deleting of an Element from ArrayList in Java

Array List is analogous to the array whose size can be changed. The Array List class is available in the Java.util package and extends the List interface. Putting up and deleting of an element from the Array List is very easy by using its erected- in styles add() and remove(). Still, there’s further than one way of removing an element from the ArrayList that are as follows UsingArrayList.remove() system By indicator. 

By element Using iterator system using array list system() system. It also provides the two overloaded styles, i.e., spread( int  indicator) and remove( Object obj).

The spread( int indicator) system accepts the indicator of the object to be removed, and the spread( Object obj) system accepts the object to be removed. Let’s take an illustration to understand how the spread() system is used.  


public class RemoveMethod

All IT Courses 50% Off

{  public static void main( String() args)

{ / creating an ArrayList having  dereliction size 5  

ArrayList arr =  new ArrayList( 5); / Adding  rudiments to the ArrayList

arr. add(” Helen”);

arr. add(” Paul”); 

arr. add(” Elanie”);

arr. add(” Marco”); 

System. Out. Println(” The list of the size is”arr.size()); / Showing all the rudiments in the ArrayList  for( String name arr)

{ (” Name is” name);  / Removing element available at position 1 ( 1); 

(”  After removing the element the size of the ArrayList is”arr.size()); / Showing all the  rudiments in the ArrayList for( String name arr)


system. Out. Println(” Name is” name);    

() system still, the removeIf() system is best suited for this case, If we want to remove an element from the ArrayList which satisfy the predicate sludge. 

We pass the predicate sludge to that  system as an argument. 

How to create Test Cases for Exceptions in Java?    

In Java, creating test cases for exceptions isn’t a delicate task. The JUnit testing tool of Java provides a way to track the exception running of law. We can write the test cases to check whether the law throws an exception or not. In order to write as well as to run the test cases for an exception, we require the JUnit jar train in the method or we need to set up the JUnit  terrain in our system. 

The exceptions we need to follow the following way which will produce a class to be tested produce a test case class for testing which will execute the test case   produce a Class to be tested. We first produce a class that needs to be tested. We’ll  produce class ShowMessage class. In this class, we will produce two styles, i.e., show()  system and the finalMessage()  system. Using the constructor, we set the value to the communication property. In the show() system, we will publish the communication and  induce the peak by zero exception. 

In the finalMessage() system, we will concatenate the given communication with the “Hello!” string.  produce a Class for Test Case.  Now, we need to write the Java class for the test case name In this class, we will produce a msg property with a dereliction string communication and produce the object of the ShowMessage class by passing that dereliction string. We’ll produce a testshow()  system in which we will add the anticipated exception 

Arithmetic Exception.

In this system, we will call the show() system of the ShowMessage class by using its object. We’ll produce another test system, testfinalMessage(), in which we will concatenate the “Hello!” with the dereliction communication. In this communication, we will check the factual result with the anticipated result using the assertEquals() system. 

In this  system, we pass the class train of the law that contains the test cases. The result will be returning by the runClasses() system which will store into the result variable of type Result. There can be further than one failure, and to publish each failure, we can  reiterate the result using the circle.

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button