- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 add elements to the midpoint of an array in Java?
Apache commons provides a library named org.apache.commons.lang3 and, following is the maven dependency to add library to your project.
<dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.0</version> </dependency> </dependencies>
This package provides a class named ArrayUtils. You can add an element at a particular position in an array using the add() method of this class.
Example
import java.util.Arrays; import org.apache.commons.lang3.ArrayUtils; public class AddingElementToMidPoint { public static void main(String args[]) { int[] myArray = {23, 93, 30, 56, 92, 39}; int eleToAdd = 40; int position= myArray.length/2; int [] result = ArrayUtils.add(myArray, position, eleToAdd); System.out.println(Arrays.toString(result)); } }
Output
[23, 93, 30, 40, 56, 92, 39]
- Related Articles
- How to add new array elements at the beginning of an array in JavaScript?
- How to add elements to an Array using filters in Vue ?
- How To Find the Midpoint of a Line in Java?
- How to add items to an array in java dynamically?
- How to remove duplicate elements of an array in java?
- How to add items/elements to an existing jagged array in C#?
- How to reverse the elements of an array using stack in java?
- How to add together a subset of elements of an array in MongoDB aggregation?
- Can you assign an Array of 100 elements to an array of 10 elements in Java?
- How to add elements in Java CopyOnWriteArrayList?
- How to add elements in Java ArrayBlockingQueue?
- Java Program to Print the Elements of an Array
- How do I add an element to an array list in Java?
- How to sort an array of objects containing null elements in java?
- How to add elements to AbstractCollection class in Java?

Advertisements