Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to insert an element into an array at a specific index in Java

3 Answers

0 votes
import java.util.Arrays;

public class MyClass {
    private static int[] InsertElement(int[] arr, int value, int index) {
        int[] tmp = new int[arr.length + 1];
 
        for (int i = 0; i < index; i++) {
            tmp[i] = arr[i];
        }
 
        tmp[index] = value;
 
        for (int i = index + 1; i <= arr.length; i++) {
            tmp[i] = arr[i - 1];
        }
 
        return tmp;
    }
    
    public static void main(String args[]) { 
        int[] arr = { 4, 0, 7, 1, 8 };
        int value =  9;
        int index = 2;
 
        arr = InsertElement(arr, value, index);
        
        System.out.println(Arrays.toString(arr));
    }
}
 
 
 
 
/*
run:
 
[4, 0, 9, 7, 1, 8]

*/

 



answered Mar 16, 2023 by avibootz
0 votes
import java.util.Arrays;

public class MyClass {
    private static int[] InsertElement(int[] arr, int value, int index) {
        int[] tmp = new int[arr.length + 1];
 
        System.arraycopy(arr, 0, tmp, 0, index);
        
        tmp[index] = value;
        
        System.arraycopy(arr, index, tmp, index + 1, arr.length - index);
 
        return tmp;
    }
    
    public static void main(String args[]) { 
        int[] arr = { 4, 0, 7, 1, 8 };
        int value =  9;
        int index = 2;
 
        arr = InsertElement(arr, value, index);
        
        System.out.println(Arrays.toString(arr));
    }
}
 
 
 
 
/*
run:
 
[4, 0, 9, 7, 1, 8]

*/

 



answered Mar 16, 2023 by avibootz
0 votes
import java.util.Arrays;
import java.util.stream.IntStream;

public class MyClass {
    private static int[] InsertElement(int[] arr, int value, int index) {
        return IntStream.range(0, arr.length + 1)
                    .map(i -> {
                            if (i < index) {
                                return arr[i];
                            }
                            else if (i == index) {
                                return value;
                            }
                            else {
                                return arr[i - 1];
                            }
                        })
                        .toArray();
    }
    
    public static void main(String args[]) { 
        int[] arr = { 4, 0, 7, 1, 8 };
        int value =  9;
        int index = 2;
 
        arr = InsertElement(arr, value, index);
        
        System.out.println(Arrays.toString(arr));
    }
}
 
 
 
 
/*
run:
 
[4, 0, 9, 7, 1, 8]

*/

 



answered Mar 16, 2023 by avibootz
...