A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.w3resource.com/java-tutorial/arraylist/arraylist_remove-object-o.php below:

Website Navigation


Java remove first character from arraylist Method

Java ArrayList.remove(Object o) MethodLast update on August 19 2022 21:51:25 (UTC/GMT +8 hours) public boolean remove(Object o)

This method is used to remove the first occurrence of the specified element from this list, if it is present. If the element is not present within the list, it is unchanged.

Package: java.util

Java Platform: Java SE 8

Syntax:

remove(Object o)

Parameters:

Name Description o Element to be removed from this list, if present

Return Value:

true if this list contained the specified element.

Return Value Type: boolean

Pictorial presentation of ArrayList.remove(Object o) Method

Example: ArrayList.remove(Object o) Method

The following example creates an ArrayList with a capacity of 7 elements ('White' and 'Red 'colors added twice in the list). After adding 7 colors we have removed the first occurrence of "Red" and "White" colors.

import java.util.*;

public class test {
   public static void main(String[] args) {
      
    // create an empty array list with an initial capacity
    ArrayList<String> color_list = new ArrayList<String>(7);

    // use add() method to add values in the list
    color_list.add("White");
    color_list.add("Black");
    color_list.add("Red");
    color_list.add("White");
	color_list.add("Yellow");
	color_list.add("Red");
    color_list.add("White");
  

	// Print out the colors in the ArrayList
    System.out.println("****Color list****");
	for (int i = 0; i < 7; i++)
      {
         System.out.println(color_list.get(i).toString());
      }
 
	// Removes first occurrence of "Red" and "White" colors 
     color_list.remove("White"); 
	 color_list.remove("Red"); 
	 
	// Fresh Color list
    System.out.println("****Fresh Color list****");
	for (int i = 0; i < 5; i++)
      {
         System.out.println(color_list.get(i).toString());
      }
  }
}  
 

Output:

F:\java>javac test.java

F:\java>java test
****Color list****
White
Black
Red
White
Yellow
Red
White
****Fresh Color list****
Black
White
Yellow
Red
White

Java Code Editor:

Previous:remove(int index) Method
Next:clear Method


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4