

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to move specific item in array list to the first item in Java?
To move an item from an ArrayList and add it to the first position you need to -
- Get the position (index) of the item using the indexOf() method of the ArrayList class.
- Remove it using the remove() method of the ArrayList class.
- Finally, add it to the index 0 using the add() method of the ArrayList class.
Example
import java.util.ArrayList; public class ArrayListSample { public static void main(String args[]) { ArrayList al = new ArrayList(); al.add("JavaFX"); al.add("HBase"); al.add("WebGL"); al.add("OpenCV"); System.out.println(al); String item = "WebGL"; int itemPos = al.indexOf(item); al.remove(itemPos); al.add(0, item ); System.out.println(al); } }
Output
[JavaFX, HBase, WebGL, OpenCV] [WebGL, JavaFX, HBase, OpenCV]
- Related Questions & Answers
- Java Program to select the first item in JList
- How to sort array by first item in subarray - JavaScript?
- Java Program to disable the first item on a JComboBox
- How to add a list item in HTML?
- Default to and select the first item in Tkinter Listbox
- Getting only the first item for an array property in MongoDB?
- Get the first and last item in an array using JavaScript?
- How to splice duplicate item in array JavaScript
- Find first duplicate item in array in linear time JavaScript
- How to add an item to a list in Kotlin?
- How to insert an item into an array at a specific index in javaScript?
- How to set the list-item marker type with JavaScript?
- How can I remove a specific item from an array in JavaScript
- How to directly modify a specific item in a TKinter listbox?
- How can I remove a specific item from an array JavaScript?
Advertisements