Answers for "how to add element to end of array java"

0

how to add element to end of array java

public static void main(String[] args) {
    int[] array = new int[] {1, 2, 3};
    System.out.println(Arrays.toString(array));
    array = Arrays.copyOf(array, array.length + 1); //create new array from old array and allocate one more element
    array[array.length - 1] = 4;
    System.out.println(Arrays.toString(array));
}
Posted by: Guest on November-27-2021

Code answers related to "how to add element to end of array java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language