About 2,170,000 results
Open links in new tab
  1. How to Add an Element to an Array in Java? - GeeksforGeeks

    Apr 22, 2025 · There are two different approaches we can use to add an element to an Array. The approaches are listed below: 1. Adding an Element Using a New Array. The first approach is that we can create a new array whose size is one element larger than the old size. Approach: Create a new array of size n+1, where n is the size of the original array.

  2. How to Insert an element at a specific position in an Array in Java

    Mar 12, 2024 · In this article, we will see how to insert an element in an array in Java. Given an array arr of size n, this article tells how to insert an element x in this array arr at a specific position pos.

  3. java - How to add new elements to an array? - Stack Overflow

    Mar 12, 2023 · Use a List<String>, such as an ArrayList<String>. It's dynamically growable, unlike arrays (see: Effective Java 2nd Edition, Item 25: Prefer lists to arrays). //.... If you insist on using arrays, you can use java.util.Arrays.copyOf to allocate a bigger array to accomodate the additional element. This is really not the best solution, though.

  4. Java Program To Insert An Element In Array | Programs

    Apr 17, 2025 · We will discuss a couple of methods on how to insert an element in an array at a specified position. The compiler has been added so that you can execute the programs yourself, alongside suitable examples and sample outputs added.

  5. java - How to add an element to Array and shift indexes

    The most simple way of doing this is to use an ArrayList<Integer> and use the add (int, T) method.

  6. Inserting Elements in an ArrayArray Operations - GeeksforGeeks

    Nov 7, 2024 · Inserting an element at a given position in an array involves shifting the elements from the specified position onward one index to the right to make an empty space for the new element.

  7. How To Add an Element To an Array in Java - CodeGym

    Nov 18, 2020 · One of the most common ways to add more elements to an array is by creating a new, larger, array from scratch, putting the elements of the old ones and adding new elements.

  8. Java Program To Insert an Element at Specified Position in an Array

    Mar 5, 2021 · In this tutorial, we will learn how to add an element to a given position in an array. The easiest way to do this is by shifting the elements and then inserting the element at a specific position.

  9. Java Program to Insert an Element in an Array - Sanfoundry

    Here is the source code of the Java Program to Insert an Element in a Specified Position in a Given Array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below. Scanner s = new Scanner (System. in); n = s. nextInt(); a [i] = s. nextInt(); pos = s. nextInt(); x = s. nextInt();

  10. Insert an element into an array at a specific index in Java

    Mar 27, 2024 · This post will discuss how to insert an element into an array at the specified index in Java. The insertion should shift the element currently at that index and any subsequent elements to the right by one position.

Refresh